BigQueryAvroUtils { static TableRow convertGenericRecordToTableRow(GenericRecord record, TableSchema schema) { return convertGenericRecordToTableRow(record, schema.getFields()); }    }
@Test public void testConvertGenericRecordToTableRow() throws Exception { TableSchema tableSchema = new TableSchema(); tableSchema.setFields(fields); BigDecimal birthdayMoney = new BigDecimal("123456789.123456789"); Schema birthdayMoneySchema = Schema.create(Type.BYTES); LogicalType birthdayMoneyLogicalType = LogicalTypes.decimal(birthdayMoney.precision(), birthdayMoney.scale()); byte[] birthdayMoneyBytes = new Conversions.DecimalConversion() .toBytes(birthdayMoney, birthdayMoneySchema, birthdayMoneyLogicalType) .array(); List<Schema.Field> avroFields = new ArrayList<>(); for (Schema.Field field : AvroCoder.of(Bird.class).getSchema().getFields()) { Schema schema = field.schema(); if ("birthdayMoney".equals(field.name())) { schema = Schema.createUnion( Schema.create(Type.NULL), birthdayMoneyLogicalType.addToSchema(birthdayMoneySchema)); } avroFields.add(new Schema.Field(field.name(), schema, field.doc(), field.defaultValue())); } Schema avroSchema = Schema.createRecord(avroFields); { GenericRecord record = new GenericData.Record(avroSchema); record.put("number", 5L); TableRow convertedRow = BigQueryAvroUtils.convertGenericRecordToTableRow(record, tableSchema); TableRow row = new TableRow().set("number", "5").set("associates", new ArrayList<TableRow>()); assertEquals(row, convertedRow); TableRow clonedRow = convertedRow.clone(); assertEquals(convertedRow, clonedRow); } { GenericRecord record = new GenericData.Record(avroSchema); byte[] soundBytes = "chirp,chirp".getBytes(StandardCharsets.UTF_8); ByteBuffer soundByteBuffer = ByteBuffer.wrap(soundBytes); soundByteBuffer.rewind(); record.put("number", 5L); record.put("quality", 5.0); record.put("birthday", 5L); record.put("birthdayMoney", ByteBuffer.wrap(birthdayMoneyBytes)); record.put("flighted", Boolean.TRUE); record.put("sound", soundByteBuffer); record.put("anniversaryDate", new Utf8("2000-01-01")); record.put("anniversaryDatetime", new String("2000-01-01 00:00:00.000005")); record.put("anniversaryTime", new Utf8("00:00:00.000005")); record.put("geoPositions", new String("LINESTRING(1 2, 3 4, 5 6, 7 8)")); TableRow convertedRow = BigQueryAvroUtils.convertGenericRecordToTableRow(record, tableSchema); TableRow row = new TableRow() .set("number", "5") .set("birthday", "1970-01-01 00:00:00.000005 UTC") .set("birthdayMoney", birthdayMoney.toString()) .set("quality", 5.0) .set("associates", new ArrayList<TableRow>()) .set("flighted", Boolean.TRUE) .set("sound", BaseEncoding.base64().encode(soundBytes)) .set("anniversaryDate", "2000-01-01") .set("anniversaryDatetime", "2000-01-01 00:00:00.000005") .set("anniversaryTime", "00:00:00.000005") .set("geoPositions", "LINESTRING(1 2, 3 4, 5 6, 7 8)"); TableRow clonedRow = convertedRow.clone(); assertEquals(convertedRow, clonedRow); assertEquals(row, convertedRow); } { Schema subBirdSchema = AvroCoder.of(Bird.SubBird.class).getSchema(); GenericRecord nestedRecord = new GenericData.Record(subBirdSchema); nestedRecord.put("species", "other"); GenericRecord record = new GenericData.Record(avroSchema); record.put("number", 5L); record.put("associates", Lists.newArrayList(nestedRecord)); record.put("birthdayMoney", ByteBuffer.wrap(birthdayMoneyBytes)); TableRow convertedRow = BigQueryAvroUtils.convertGenericRecordToTableRow(record, tableSchema); TableRow row = new TableRow() .set("associates", Lists.newArrayList(new TableRow().set("species", "other"))) .set("number", "5") .set("birthdayMoney", birthdayMoney.toString()); assertEquals(row, convertedRow); TableRow clonedRow = convertedRow.clone(); assertEquals(convertedRow, clonedRow); } }