AvroUtils { public static org.apache.avro.Schema toAvroSchema( Schema beamSchema, @Nullable String name, @Nullable String namespace) { final String schemaName = Strings.isNullOrEmpty(name) ? "topLevelRecord" : name; final String schemaNamespace = namespace == null ? "" : namespace; String childNamespace = !"".equals(schemaNamespace) ? schemaNamespace + "." + schemaName : schemaName; List<org.apache.avro.Schema.Field> fields = Lists.newArrayList(); for (Schema.Field field : beamSchema.getFields()) { org.apache.avro.Schema.Field recordField = toAvroField(field, childNamespace); fields.add(recordField); } return org.apache.avro.Schema.createRecord(schemaName, null, schemaNamespace, false, fields); } private  AvroUtils(); static Schema.Field toBeamField(org.apache.avro.Schema.Field field); static org.apache.avro.Schema.Field toAvroField(Schema.Field field, String namespace); static Schema toBeamSchema(org.apache.avro.Schema schema); static org.apache.avro.Schema toAvroSchema(
      Schema beamSchema, @Nullable String name, @Nullable String namespace); static org.apache.avro.Schema toAvroSchema(Schema beamSchema); static Row toBeamRowStrict(GenericRecord record, @Nullable Schema schema); static GenericRecord toGenericRecord(
      Row row, org.apache.avro.@Nullable Schema avroSchema); @SuppressWarnings("unchecked") static SerializableFunction<T, Row> getToRowFunction(
      Class<T> clazz, org.apache.avro.@Nullable Schema schema); @SuppressWarnings("unchecked") static SerializableFunction<Row, T> getFromRowFunction(Class<T> clazz); static @Nullable Schema getSchema(
      Class<T> clazz, org.apache.avro.@Nullable Schema schema); static SerializableFunction<GenericRecord, Row> getGenericRecordToRowFunction(
      @Nullable Schema schema); static SerializableFunction<Row, GenericRecord> getRowToGenericRecordFunction(
      org.apache.avro.@Nullable Schema avroSchema); static SchemaCoder<T> schemaCoder(TypeDescriptor<T> type); static SchemaCoder<T> schemaCoder(Class<T> clazz); static SchemaCoder<GenericRecord> schemaCoder(org.apache.avro.Schema schema); static SchemaCoder<T> schemaCoder(Class<T> clazz, org.apache.avro.Schema schema); static SchemaCoder<T> schemaCoder(AvroCoder<T> avroCoder); static List<FieldValueTypeInformation> getFieldTypes(Class<T> clazz, Schema schema); static List<FieldValueGetter> getGetters(Class<T> clazz, Schema schema); static SchemaUserTypeCreator getCreator(Class<T> clazz, Schema schema); @SuppressWarnings("unchecked") static @Nullable Object convertAvroFieldStrict(
      @Nullable Object value,
      @Nonnull org.apache.avro.Schema avroSchema,
      @Nonnull Schema.FieldType fieldType);  }
@Test public void testFromBeamSchema() { Schema beamSchema = getBeamSchema(); org.apache.avro.Schema avroSchema = AvroUtils.toAvroSchema(beamSchema); assertEquals(getAvroSchema(), avroSchema); }
@Test public void testAvroSchemaFromBeamSchemaCanBeParsed() { org.apache.avro.Schema convertedSchema = AvroUtils.toAvroSchema(getBeamSchema()); org.apache.avro.Schema validatedSchema = new org.apache.avro.Schema.Parser().parse(convertedSchema.toString()); assertEquals(convertedSchema, validatedSchema); }
@Test public void testAvroSchemaFromBeamSchemaWithFieldCollisionCanBeParsed() { Schema contact = new Schema.Builder() .addField(Field.of("name", FieldType.STRING)) .addField( Field.of( "address", FieldType.row( new Schema.Builder() .addField(Field.of("street", FieldType.STRING)) .addField(Field.of("city", FieldType.STRING)) .build()))) .build(); Schema contactMultiline = new Schema.Builder() .addField(Field.of("name", FieldType.STRING)) .addField( Field.of( "address", FieldType.row( new Schema.Builder() .addField(Field.of("street", FieldType.array(FieldType.STRING))) .addField(Field.of("city", FieldType.STRING)) .build()))) .build(); Schema beamSchema = new Schema.Builder() .addField(Field.of("home", FieldType.row(contact))) .addField(Field.of("work", FieldType.row(contactMultiline))) .addField(Field.of("address", FieldType.row(contact))) .addField(Field.of("topLevelRecord", FieldType.row(contactMultiline))) .build(); org.apache.avro.Schema convertedSchema = AvroUtils.toAvroSchema(beamSchema); org.apache.avro.Schema validatedSchema = new org.apache.avro.Schema.Parser().parse(convertedSchema.toString()); assertEquals(convertedSchema, validatedSchema); }