Spark Scala Streaming CSV

I am new to Spark / Scala. I know how to upload CSV files:

    sqlContext.read.format("csv")

and how to read text streams and file streams:

    scc.textFileStream("""file:///c:\path\filename""");
    scc.fileStream[LongWritable, Text, TextInputFormat](...)

but how to read text in CSV format ? Thank you Levy

+4
source share
2 answers

Here you go:

val ssc = new StreamingContext(sparkConf, Seconds(5))


    // Create the FileInputDStream on the directory
    val lines = ssc.textFileStream("file:///C:/foo/bar")

    lines.foreachRDD(rdd => {
        if (!rdd.isEmpty()) {
          println("RDD row count: " + rdd.count())
         // Now you can convert this RDD to DataFrame/DataSet and perform business logic.  

        }
      }
    })

    ssc.start()
    ssc.awaitTermination()
  } 
+3
source

You can easily stream your Csv file using structured stream synchronization.

You can refer to here

0
source

All Articles