从本地文件读取 Spark Streaming 会出现 NullPointerException

Spark Streaming reading from local file gives NullPointerException

在 OS X High Sierra 上使用 Spark 2.2.0。我是 运行 一个读取本地文件的 Spark Streaming 应用程序:

val lines = ssc.textFileStream("file:///Users/userName/Documents/Notes/MoreNotes/sampleFile")
    lines.print()

这给了我

org.apache.spark.streaming.dstream.FileInputDStream logWarning - Error finding new files
java.lang.NullPointerException
    at scala.collection.mutable.ArrayOps$ofRef$.length$extension(ArrayOps.scala:192)

该文件存在,我可以在终端上使用 spark-shell 中的 SparkContext (sc) 读取它。由于某种原因,通过 Intellij 应用程序和 Spark Streaming 无法正常工作。任何想法表示赞赏!

引用 textFileStream 的文档注释:

Create an input stream that monitors a Hadoop-compatible filesystem for new files and reads them as text files (using key as LongWritable, value as Text and input format as TextInputFormat). Files must be written to the monitored directory by "moving" them from another location within the same file system. File names starting with . are ignored.

@param directory HDFS directory to monitor for new file

因此,该方法需要参数中 目录 的路径。

所以我认为这应该可以避免该错误:

ssc.textFileStream("file:///Users/userName/Documents/Notes/MoreNotes/")

Spark Streaming 不会读取旧文件,所以先运行 spark-submit 命令然后在指定目录中创建本地文件。确保在 spark-submit 命令中,您只提供目录名而不是文件名。下面是一个示例命令。在这里,我通过 spark 命令传递目录名称作为我的第一个参数。您也可以在 Scala 程序中指定此路径。

spark-submit --class com.spark.streaming.streamingexample.HdfsWordCount --jars /home/cloudera/pramod/kafka_2.12-1.0.1/libs/kafka-clients-1.0.1.jar--master local[4] /home/cloudera/pramod/streamingexample-0.0.1-SNAPSHOT.jar /pramod/hdfswordcount.txt