Spark FileStreaming不使用foreachRDD

时间:2014-02-03 14:25:39

标签: apache-spark

我是Spark的新手,我正在构建一个Small示例应用程序,它是一个Spark文件流。我想要的是一次性读取整个文件而不是逐行读取(我猜这是textFileStream的作用)。

代码如下:

import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.StreamingContext._

import org.apache.hadoop.io.LongWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat

import scalax.io._

object SampleXML{

    def main(args: Array[String]){

        val logFile = "/home/akhld/mobi/spark-streaming/logs/sample.xml"

        val ssc = new StreamingContext("spark://localhost:7077","XML Streaming Job",Seconds(5),"/home/akhld/mobi/spark-streaming/spark-0.8.0-incubating",List("target/scala-2.9.3/simple-project_2.9.3-1.0.jar"))

        val lines = ssc.fileStream[LongWritable, Text, TextInputFormat]("/home/akhld/mobi/spark-streaming/logs/")

        lines.print()

        lines.foreachRDD(rdd => {
          rdd.count()  // prints counts

        })


        ssc.start()


    }
}

此代码失败,并出现例外情况:

[error] /home/akhld/mobi/spark-streaming/samples/samplexml/src/main/scala/SampleXML.scala:31: value foreachRDD is not a member of org.apache.spark.streaming.DStream[(org.apache.hadoop.io.LongWritable, org.apache.hadoop.io.Text)]
[error]         ssc.fileStream[LongWritable, Text, TextInputFormat]("/home/akhld/mobi/spark-streaming/logs/").foreachRDD(rdd => {
[error]                                                                                                       ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 3 s, completed Feb 3, 2014 7:32:57 PM

如果这不是显示流中文件内容的正确方法,请帮我举个例子。我搜索了很多但找不到合适的使用fileStream。

2 个答案:

答案 0 :(得分:1)

好吧,Spark Streaming中的textFileStream更意味着连续读取和处理正在目录中写入的文件。因此,如果您必须一次性处理一个文件,那么直接使用Spark就更简单了!

 val lines = sparkContext.textFile(<file URL>)
 lines.foreach(line => println(line))

这将打印文件中的所有行。

答案 1 :(得分:0)

另外,我相信这里的问题是你不能在forEach流中的RDD上调用count()。原因是,如果你这样做,我认为它阻止了forEach块的进度 - 并且流消费者停止工作。

我为此https://issues.apache.org/jira/browse/SPARK-4040创建了一个JIRA。

我认为当您在forEach块中引用RDD时,可以对RDD进行一些敏感的API调用,但还没有完全解决所有细节问题。