Uploaded image for project: 'Beam'
  1. Beam
  2. BEAM-13854

Document casting trick for Avro value serializer in KafkaIO

Details

    Description

      Consider we want to write Avro values to Kafka with for example the following code:

      KafkaIO.Write<String, GenericRecord> stringsToKafka =
       KafkaIO.<String, GenericRecord>write()
        .withBootstrapServers(bootstrapServers)
        .withTopic(topic)
        .withKeySerializer(StringSerializer.class)
        .withValueSerializer(KafkaAvroSerializer.class)
        .withProducerConfigUpdates(producerConfigUpdates);

       The KafkaAvroSerializer.class argument can't be passed as would normally be the case in Producer option: value.serializer=io.confluent.kafka.serializers.KafkaAvroSerializer

      So the question then is which class should we pass or how to cast. IntelliJ IDEA suggests a cast which doesn't compile.

      In the end the answer is simply:

        .withValueSerializer((Class)KafkaAvroSerializer.class) 

      I think it's worth documenting this little trick more clearly in the Javadoc of KafkaIO to prevent others from bumping into the same issue. 

      Attachments

        Issue Links

          Activity

            People

              mcasters Matt Casters
              mcasters Matt Casters
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 20m
                  1h 20m