I followed this link to create a template which builds a beam pipeline to read from KafkaIO. But I always encountered " incompatible types: org.apache.beam.sdk.options.ValueProvider cannot be converted to java.lang.String". It is line ".withBootstrapServers(options.getKafkaServer())" which caused the error. Beam version is 2.9.0 and here is part of my code.
public interface Options extends PipelineOptions {
@Description("Kafka server")
@Required
ValueProvider<String> getKafkaServer();
void setKafkaServer(ValueProvider<String> value);
@Description("Topic to read from")
@Required
ValueProvider<String> getInputTopic();
void setInputTopic(ValueProvider<String> value);
@Description("Topic to write to")
@Required
ValueProvider<String> getOutputTopic();
void setOutputTopic(ValueProvider<String> value);
@Description("File path to write to")
@Required
ValueProvider<String> getOutput();
void setOutput(ValueProvider<String> value);
}
public static void main(String[] args) {
Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
Pipeline p = Pipeline.create(options);
PCollection<String> processedData = p.apply(KafkaIO.<Long, String>read()
.withBootstrapServers(options.getKafkaServer())
.withTopic(options.getInputTopic())
.withKeyDeserializer(LongDeserializer.class)
.withValueDeserializer(StringDeserializer.class)
.withoutMetadata()
)
And following is how I run the code:
mvn compile exec:java \
-Dexec.mainClass=${MyClass} \
-Pdataflow-runner -Dexec.args=" \
--project=${MyClass} \
--stagingLocation=gs://${MyBucket}/staging \
--tempLocation=gs://${MyBucket}/temp \
--templateLocation=gs://${MyBucket}/templates/${MyClass} \
--runner=DataflowRunner"