hi guys I'm working with kafka > spark streaming > Elasticsearch. but i dont make spark streaming JavaInputDStream JSON to elasticsearch.
My Code :
SparkConf conf = new SparkConf()
.setAppName("Streaming")
.setMaster("local")
.set("es.nodes","localhost:9200")
.set("es.index.auto.create","true");
JavaStreamingContext streamingContext = new JavaStreamingContext(conf, new Duration(5000));
Map<String, Object> kafkaParams = new HashMap<>();
kafkaParams.put("bootstrap.servers", "localhost:9092");
kafkaParams.put("key.deserializer", StringDeserializer.class);
kafkaParams.put("value.deserializer", StringDeserializer.class);
kafkaParams.put("group.id", "exastax");
kafkaParams.put("auto.offset.reset", "latest");
kafkaParams.put("enable.auto.commit", false);
Collection<String> topics = Arrays.asList("loglar");
JavaInputDStream<ConsumerRecord<String, String>> stream =
KafkaUtils.createDirectStream(
streamingContext,
LocationStrategies.PreferConsistent(),
ConsumerStrategies.<String, String>Subscribe(topics, kafkaParams)
);
JavaPairDStream<String, String> finisStream = stream.mapToPair(record -> new Tuple2<>("", record.value()));
finisStream.print();
JavaEsSparkStreaming.saveJsonToEs(finisStream,"spark/docs");
streamingContext.start();
streamingContext.awaitTermination();
}
JavaEsSparkStreaming.saveJsonToEs(finisStream,"spark/docs"); >> finisStream isn't working because it's not JavaDStream. How to convert JavaDStream ?
答案 0 :(得分:1)
JavaEsSparkStreaming.saveJsonToEs
适用于JavaDStream
JavaEsSparkStreaming.saveToEsWithMeta
适用于JavaPairDStream
修复您的代码:
JavaDStream<String> finisStream = stream.map(new Function<Tuple2<String, String>, String>() {
public String call(Tuple2<String, String> stringStringTuple2) throws Exception {
return stringStringTuple2._2();
}
});
JavaEsSparkStreaming.saveJsonToEs(finisStream,"");
答案 1 :(得分:1)
JavaDStream<String> stream1 = stream.map(
new Function<ConsumerRecord<String, String>, String>() {
@Override
public String call(ConsumerRecord<String, String> r) {
return r.value();
}
}
);
JavaEsSparkStreaming.saveJsonToEs(stream1,"spark/docs");