You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/06/29 14:23:57 UTC

[GitHub] [beam] MiguelAnzoWizeline commented on a change in pull request #14586: [BEAM-8933] Add support for reading Arrow over the BigQuery Storage API

MiguelAnzoWizeline commented on a change in pull request #14586:
URL: https://github.com/apache/beam/pull/14586#discussion_r660670124



##########
File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryStorageArrowReader.java
##########
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.bigquery;
+
+import com.google.cloud.bigquery.storage.v1.ArrowSchema;
+import com.google.cloud.bigquery.storage.v1.ReadRowsResponse;
+import com.google.cloud.bigquery.storage.v1.ReadSession;
+import java.io.IOException;
+import java.io.InputStream;
+import javax.annotation.Nullable;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.beam.sdk.extensions.arrow.ArrowConversion;
+import org.apache.beam.sdk.extensions.arrow.ArrowConversion.RecordBatchRowIterator;
+import org.apache.beam.sdk.schemas.utils.AvroUtils;
+import org.apache.beam.sdk.values.Row;
+
+@SuppressWarnings("nullness")
+class BigQueryStorageArrowReader implements BigQueryStorageReader {
+
+  private org.apache.beam.sdk.schemas.Schema arrowBeamSchema;
+  private @Nullable RecordBatchRowIterator recordBatchIterator;
+  private long rowCount;
+  private ArrowSchema protoSchema;
+  private @Nullable RootAllocator alloc;
+
+  BigQueryStorageArrowReader(ReadSession readSession) throws IOException {
+    protoSchema = readSession.getArrowSchema();
+    InputStream input = protoSchema.getSerializedSchema().newInput();
+    this.arrowBeamSchema =
+        ArrowConversion.ArrowSchemaTranslator.toBeamSchema(
+            ArrowConversion.arrowSchemaFromInput(input));
+    this.rowCount = 0;
+    this.alloc = null;
+  }
+
+  @Override
+  public void processReadRowsResponse(ReadRowsResponse readRowsResponse) throws IOException {
+    com.google.cloud.bigquery.storage.v1.ArrowRecordBatch recordBatch =
+        readRowsResponse.getArrowRecordBatch();
+    rowCount = recordBatch.getRowCount();
+    this.alloc = new RootAllocator(Long.MAX_VALUE);
+    InputStream input = protoSchema.getSerializedSchema().newInput();
+    Schema arrowSchema = ArrowConversion.arrowSchemaFromInput(input);
+    this.recordBatchIterator =
+        ArrowConversion.rowsFromSerializedRecordBatch(
+            arrowSchema, recordBatch.getSerializedRecordBatch().newInput(), this.alloc);
+  }
+
+  @Override
+  public long getRowCount() {
+    return rowCount;
+  }
+
+  @Override
+  public GenericRecord readSingleRecord() throws IOException {
+    if (recordBatchIterator == null) {
+      throw new IOException("Not Initialized");
+    }
+    Row row = recordBatchIterator.next();
+    return AvroUtils.toGenericRecord(row, null);

Review comment:
       Issue created [here](https://issues.apache.org/jira/browse/BEAM-12551)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org