Using Avrocoder for Custom Types with Station Wagons

I am trying to use AvroCoder to serialize a custom type that is passed to PCollections in my pipeline. The user type has a common field (which is currently a string). When I run the pipeline, I get an AvroTypeException, as shown below, due to a common field. Is building and transferring AvroSchema to the facility the only way around this?

Exception in thread "main" org.apache.avro.AvroTypeException: Unknown type: T
 at org.apache.avro.specific.SpecificData.createSchema(SpecificData.java:255)
 at org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:514)
 at org.apache.avro.reflect.ReflectData.createFieldSchema(ReflectData.java:593)
 at org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:472)
 at org.apache.avro.specific.SpecificData.getSchema(SpecificData.java:189)
 at com.google.cloud.dataflow.sdk.coders.AvroCoder.of(AvroCoder.java:116)

I also applied my registry code for reference.

pipelineCoderRegistry.registerCoder(GenericTypeClass.class, new CoderFactory() {
    @Override
    public Coder<?> create(List<? extends Coder<?>> componentCoders) {
        return AvroCoder.of(GenericTypeClass.class);
    }

    @Override
    public List<Object> getInstanceComponents(Object value) {
        return Collections.singletonList(((GenericTypeClass<Object>) value).key);
    }
});
+4
source share
1 answer

, CoderFactory, Avros ReflectData , AvroCoder , , . AVRO-1571. . fooobar.com/questions/1611688/....

GenericTypeClass<T> T, , . :

- T GenericTypeClass<T>, :

class GenericTypeClass<T> {
  // Avro requires a no-args constructor
  public GenericTypeClass() {}

  @AvroSchema("[\"string\", \"int\", ...]")
  private T genericField;
}

, JSON T.

- AvroCoder CoderFactory AvroCoder.of(Class, Schema).

pipelineCoderRegistry.registerCoder(GenericTypeClass.class, new CoderFactory() {
  @Override
  public Coder<?> create(List<? extends Coder<?>> componentCoders) {
      return AvroCoder.of(
          GenericTypeClass.class
          schemaFromCoder(componentCoders.get(0)));
  }

  ...
});

Coder<T> T. POJO, ReflectData. .

+7

Source: https://habr.com/ru/post/1611686/


All Articles