在Java类中读取BigQuery表数据(Pojo)

时间:2019-05-23 16:43:39

标签: google-bigquery google-cloud-dataflow apache-beam

我需要使用数据流从Bigquery中读取表数据,而不是使用数据/将数据存储到TableRow类中。我想将数据存储在Java Pojo类中,请问有什么方法可以直接将数据映射到Pojo中。

方法2:

GenericRecord s = schemaAndRecord.getRecord();

            org.apache.avro.Schema s1 = s.getSchema();
            for (Field f : s1.getFields()) {
                counter++;      
                mapping.put(f.name(), null==s.get(f.name())?null:String.valueOf(s.get(counter)));
                if(f.name().equalsIgnoreCase("reason_code_id")) {
                    BigDecimal numericValue =
                            new Conversions.DecimalConversion()
                                .fromBytes((ByteBuffer)s.get(f.name()) , Schema.create(s1.getType()), s1.getLogicalType());
                    System.out.println("Numeric Con"+numericValue);
                }
                else {
                        System.out.println("Else Condition "+f.name());
                }
            }
            ```

Facing Issue:

2019-05-24 (14:10:37) org.apache.avro.AvroRuntimeException: Can't create a: RECORD

1 个答案:

答案 0 :(得分:0)

BigQueryIO#read(SerializableFunction)允许使用现有的任何Avro到POJO转换库/功能。

例如,我正在使用此blog post中的代码:

private static <T> T mapRecordToObject(GenericRecord record, T object) {
  Assert.notNull(record, "record must not be null");
  Assert.notNull(object, "object must not be null");
  final Schema schema = ReflectData.get().getSchema(object.getClass());

  Assert.isTrue(schema.getFields().equals(record.getSchema().getFields()), "Schema fields didn’t match");
  record.getSchema().getFields().forEach(d -> PropertyAccessorFactory.forDirectFieldAccess(object).setPropertyValue(d.name(), record.get(d.name()) == null ? record.get(d.name()) : record.get(d.name()).toString()));
  return object;
}

PCollection<MyType> data = pipeline.apply(
BigQueryIO
  .read(new SerializableFunction<SchemaAndRecord, MyType>() {
    public MyType apply(SchemaAndRecord schemaAndRecord) {
      return mapRecordToObject(schemaAndRecord.getRecord(), new MyType());
    }
  })
  .from("mydataset:mytable"));

博客文章中的代码假定使用avro模式生成POJO。