Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read json data using scala from kafka topic in apache spark

I am new spark, Could you please let me know how to read json data using scala from kafka topic in apache spark.

Thanks.

like image 608
Bhaskar Avatar asked Feb 16 '16 05:02

Bhaskar


1 Answers

The simplest method would be to make use of the DataFrame abstraction shipped with Spark.

val sqlContext = new SQLContext(sc)
val stream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](
                  ssc, kafkaParams, Set("myTopicName"))

stream.foreachRDD(
  rdd => {
     val dataFrame = sqlContext.read.json(rdd.map(_._2)) //converts json to DF
     //do your operations on this DF. You won't even require a model class.
        })
like image 63
void Avatar answered Oct 24 '22 13:10

void