BigQueryUtils { @Experimental(Kind.SCHEMAS) public static Row toBeamRow(GenericRecord record, Schema schema, ConversionOptions options) { List<Object> valuesInOrder = schema.getFields().stream() .map( field -> { try { return convertAvroFormat(field.getType(), record.get(field.getName()), options); } catch (Exception cause) { throw new IllegalArgumentException( "Error converting field " + field + ": " + cause.getMessage(), cause); } }) .collect(toList()); return Row.withSchema(schema).addValues(valuesInOrder).build(); }  @Experimental(Kind.SCHEMAS) static TableSchema toTableSchema(Schema schema); @Experimental(Kind.SCHEMAS) static Schema fromTableSchema(TableSchema tableSchema); @Experimental(Kind.SCHEMAS) static org.apache.avro.Schema toGenericAvroSchema(
      String schemaName, List<TableFieldSchema> fieldSchemas); static final BigQueryIO.TypedRead.ToBeamRowFunction<TableRow> tableRowToBeamRow(); static final BigQueryIO.TypedRead.FromBeamRowFunction<TableRow> tableRowFromBeamRow(); static SerializableFunction<Row, TableRow> toTableRow(); static SerializableFunction<T, TableRow> toTableRow(
      SerializableFunction<T, Row> toRow); @Experimental(Kind.SCHEMAS) static Row toBeamRow(GenericRecord record, Schema schema, ConversionOptions options); static TableRow convertGenericRecordToTableRow(
      GenericRecord record, TableSchema tableSchema); static TableRow toTableRow(Row row); @Experimental(Kind.SCHEMAS) static Row toBeamRow(Schema rowSchema, TableRow jsonBqRow); @Experimental(Kind.SCHEMAS) static Row toBeamRow(Schema rowSchema, TableSchema bqSchema, TableRow jsonBqRow); static Object convertAvroFormat(
      FieldType beamFieldType, Object avroValue, BigQueryUtils.ConversionOptions options);  }
@Test public void testToBeamRow_flat() { Row beamRow = BigQueryUtils.toBeamRow(FLAT_TYPE, BQ_FLAT_ROW); assertEquals(FLAT_ROW, beamRow); }
@Test public void testToBeamRow_null() { Row beamRow = BigQueryUtils.toBeamRow(FLAT_TYPE, BQ_NULL_FLAT_ROW); assertEquals(NULL_FLAT_ROW, beamRow); }
@Test public void testToBeamRow_array() { Row beamRow = BigQueryUtils.toBeamRow(ARRAY_TYPE, BQ_ARRAY_ROW); assertEquals(ARRAY_ROW, beamRow); }
@Test public void testToBeamRow_row() { Row beamRow = BigQueryUtils.toBeamRow(ROW_TYPE, BQ_ROW_ROW); assertEquals(ROW_ROW, beamRow); }
@Test public void testToBeamRow_array_row() { Row beamRow = BigQueryUtils.toBeamRow(ARRAY_ROW_TYPE, BQ_ARRAY_ROW_ROW); assertEquals(ARRAY_ROW_ROW, beamRow); }
@Test public void testToBeamRow_avro_array_row() { Row flatRowExpected = Row.withSchema(AVRO_FLAT_TYPE).addValues(123L, 123.456, "test", false).build(); Row expected = Row.withSchema(AVRO_ARRAY_TYPE).addValues((Object) Arrays.asList(flatRowExpected)).build(); GenericData.Record record = new GenericData.Record(AvroUtils.toAvroSchema(AVRO_ARRAY_TYPE)); GenericData.Record flat = new GenericData.Record(AvroUtils.toAvroSchema(AVRO_FLAT_TYPE)); flat.put("id", 123L); flat.put("value", 123.456); flat.put("name", "test"); flat.put("valid", false); record.put("rows", Arrays.asList(flat)); Row beamRow = BigQueryUtils.toBeamRow( record, AVRO_ARRAY_TYPE, BigQueryUtils.ConversionOptions.builder().build()); assertEquals(expected, beamRow); }
@Test public void testToBeamRow_avro_array_array_row() { Row flatRowExpected = Row.withSchema(AVRO_FLAT_TYPE).addValues(123L, 123.456, "test", false).build(); Row arrayRowExpected = Row.withSchema(AVRO_ARRAY_TYPE).addValues((Object) Arrays.asList(flatRowExpected)).build(); Row expected = Row.withSchema(AVRO_ARRAY_ARRAY_TYPE) .addValues((Object) Arrays.asList(arrayRowExpected)) .build(); GenericData.Record arrayRecord = new GenericData.Record(AvroUtils.toAvroSchema(AVRO_ARRAY_TYPE)); GenericData.Record flat = new GenericData.Record(AvroUtils.toAvroSchema(AVRO_FLAT_TYPE)); GenericData.Record record = new GenericData.Record(AvroUtils.toAvroSchema(AVRO_ARRAY_ARRAY_TYPE)); flat.put("id", 123L); flat.put("value", 123.456); flat.put("name", "test"); flat.put("valid", false); arrayRecord.put("rows", Arrays.asList(flat)); record.put("array_rows", Arrays.asList(arrayRecord)); Row beamRow = BigQueryUtils.toBeamRow( record, AVRO_ARRAY_ARRAY_TYPE, BigQueryUtils.ConversionOptions.builder().build()); assertEquals(expected, beamRow); }