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 2022/09/30 20:20:51 UTC

[GitHub] [beam] kkdoon commented on a diff in pull request #22718: Support custom avro DatumReader when reading from BigQuery #22717

kkdoon commented on code in PR #22718:
URL: https://github.com/apache/beam/pull/22718#discussion_r984923030


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -573,6 +577,60 @@ public static TypedRead<TableRow> readTableRowsWithSchema() {
             BigQueryUtils.tableRowFromBeamRow());
   }
 
+  @VisibleForTesting
+  static class GenericDatumTransformer<T> implements DatumReader<T> {
+    private final SerializableFunction<SchemaAndRecord, T> parseFn;
+    private final TableSchema tableSchema;
+    private final GenericDatumReader<T> reader;
+    private org.apache.avro.Schema writerSchema;
+
+    public GenericDatumTransformer(
+        SerializableFunction<SchemaAndRecord, T> parseFn,
+        TableSchema tableSchema,
+        org.apache.avro.Schema writer,
+        org.apache.avro.Schema reader) {
+      this.parseFn = parseFn;
+      this.tableSchema = tableSchema;
+      this.setSchema(writer);
+      this.reader = new GenericDatumReader<>(this.writerSchema, reader);
+    }
+
+    @Override
+    public void setSchema(org.apache.avro.Schema schema) {
+      this.writerSchema = schema;
+    }
+
+    @Override
+    public T read(T reuse, Decoder in) throws IOException {
+      GenericRecord record = (GenericRecord) this.reader.read(reuse, in);
+      return parseFn.apply(new SchemaAndRecord(record, tableSchema));
+    }
+  }
+
+  @VisibleForTesting
+  private static class DatumReaderWrapper<T> implements DatumReader<T> {
+    private final DatumReader<T> reader;
+    private org.apache.avro.Schema writerSchema;
+
+    public DatumReaderWrapper(
+        org.apache.avro.Schema writerSchema,
+        org.apache.avro.Schema readerSchema,
+        AvroSource.DatumReaderFactory<T> factory) {
+      this.setSchema(writerSchema);
+      this.reader = factory.apply(this.writerSchema, readerSchema);
+    }
+
+    @Override
+    public void setSchema(org.apache.avro.Schema schema) {
+      this.writerSchema = schema;

Review Comment:
   good catch



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -573,6 +577,60 @@ public static TypedRead<TableRow> readTableRowsWithSchema() {
             BigQueryUtils.tableRowFromBeamRow());
   }
 
+  @VisibleForTesting
+  static class GenericDatumTransformer<T> implements DatumReader<T> {
+    private final SerializableFunction<SchemaAndRecord, T> parseFn;
+    private final TableSchema tableSchema;
+    private final GenericDatumReader<T> reader;
+    private org.apache.avro.Schema writerSchema;
+
+    public GenericDatumTransformer(
+        SerializableFunction<SchemaAndRecord, T> parseFn,
+        TableSchema tableSchema,
+        org.apache.avro.Schema writer,
+        org.apache.avro.Schema reader) {
+      this.parseFn = parseFn;
+      this.tableSchema = tableSchema;
+      this.setSchema(writer);
+      this.reader = new GenericDatumReader<>(this.writerSchema, reader);
+    }
+
+    @Override
+    public void setSchema(org.apache.avro.Schema schema) {
+      this.writerSchema = schema;
+    }
+
+    @Override
+    public T read(T reuse, Decoder in) throws IOException {
+      GenericRecord record = (GenericRecord) this.reader.read(reuse, in);
+      return parseFn.apply(new SchemaAndRecord(record, tableSchema));
+    }
+  }
+
+  @VisibleForTesting
+  private static class DatumReaderWrapper<T> implements DatumReader<T> {
+    private final DatumReader<T> reader;
+    private org.apache.avro.Schema writerSchema;
+
+    public DatumReaderWrapper(
+        org.apache.avro.Schema writerSchema,
+        org.apache.avro.Schema readerSchema,
+        AvroSource.DatumReaderFactory<T> factory) {
+      this.setSchema(writerSchema);
+      this.reader = factory.apply(this.writerSchema, readerSchema);
+    }
+
+    @Override
+    public void setSchema(org.apache.avro.Schema schema) {
+      this.writerSchema = schema;

Review Comment:
   good catch



-- 
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