From c13a0cc4fbc34b31e677991cc6b8ed09ace8c325 Mon Sep 17 00:00:00 2001 From: Anant Damle Date: Wed, 9 Jun 2021 00:54:51 +0800 Subject: [PATCH] [BEAM-12460] Provide a simpler interface to convert Beam Row to GenericRecord. --- .../java/org/apache/beam/sdk/schemas/utils/AvroUtils.java | 8 ++++++++ .../org/apache/beam/sdk/schemas/utils/AvroUtilsTest.java | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java index 0835f9b84450..3a10692173ab 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java @@ -369,6 +369,14 @@ public static Row toBeamRowStrict(GenericRecord record, @Nullable Schema schema) return builder.build(); } + /** + * Convert from a Beam Row to an AVRO GenericRecord. The Avro Schema is inferred from the Beam + * schema on the row. + */ + public static GenericRecord toGenericRecord(Row row) { + return toGenericRecord(row, null); + } + /** * Convert from a Beam Row to an AVRO GenericRecord. If a Schema is not provided, one is inferred * from the Beam schema on the row. diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/AvroUtilsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/AvroUtilsTest.java index c1096cacdaf3..835acb3ef067 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/AvroUtilsTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/AvroUtilsTest.java @@ -721,6 +721,13 @@ public void testBeamRowToGenericRecord() { assertEquals(getGenericRecord(), genericRecord); } + @Test + public void testBeamRowToGenericRecordInferSchema() { + GenericRecord genericRecord = AvroUtils.toGenericRecord(getBeamRow()); + assertEquals(getAvroSchema(), genericRecord.getSchema()); + assertEquals(getGenericRecord(), genericRecord); + } + @Test public void testRowToGenericRecordFunction() { SerializableUtils.ensureSerializable(AvroUtils.getRowToGenericRecordFunction(NULL_SCHEMA));