使用Kafka和Spark Streaming处理JSON数据

时间:2018-05-31 14:57:29

标签: java apache-spark apache-kafka spark-streaming

我是Spark-Streaming和Kafka的新手。使用以下代码,我可以使用Kafka-Messages,它以JSON格式传递:

JavaDStream<String> jsonline = stream.map(new Function<ConsumerRecord<String,String>, String>() {
        @Override
        public String call(ConsumerRecord<String, String> kafkaRecord) throws Exception {
        return kafkaRecord.value();
        }
    });

jsonline.print();

控制台输出:

-------------------------------------------
Time: 1527776685000 ms
-------------------------------------------
{"logtyp":"ERROR","LogTypName":"app.warning.exception","LogZeitpunkt":"Thu May 31 16:24:42 CEST 2018"}
{"logtyp":"ERROR","LogTypName":"app.warning.exception","LogZeitpunkt":"Thu May 31 16:24:44 CEST 2018"}
-------------------------------------------
Time: 1527776690000 ms
-------------------------------------------
{"logtyp":"ERROR","LogTypName":"app.warning.exception","LogZeitpunkt":"Thu May 31 16:24:45 CEST 2018"}
{"logtyp":"ERROR","LogTypName":"app.warning.exception","LogZeitpunkt":"Thu May 31 16:24:46 CEST 2018"}

是否可以使用“foreachRDD”方法从消息中提取JSON-Fields?

jsonline.foreachRDD...

...所以我可以将每个JSON记录写入mySQL,如下所示:

insert into my_table (logtyp, logtypname, logzeitpunkt) values ("ERROR", "app.warning.exception", "Thu May 31 16:24:46 CEST 2018");

0 个答案:

没有答案