带有textFileStream的Python Spark流示例不起作用。为什么?

带有textFileStream的Python Spark流示例不起作用。为什么?,python,apache-spark,spark-streaming,pyspark,Python,Apache Spark,Spark Streaming,Pyspark,我使用spark 1.3.1和Python 2.7 这是我第一次体验火花流 我尝试了一个代码示例,它使用spark流从文件中读取数据 以下是示例的链接: 我的代码如下: 2.txt文件的内容如下: a1 b1 c1 d1 e1 f1 g1 a2 b2 c2 d2 e2 f2 g2 a3 b3 c3 d3 e3 f3 g3 a1 b1 c1 d1 e1 f1 g1 a2 b2 c2 d2 e2 f2 g2 a3 b3 c3 d3 e3 f3 g3 我希望控制台中会有一些与文件内容相关的东西,

我使用spark 1.3.1和Python 2.7

这是我第一次体验火花流

我尝试了一个代码示例,它使用spark流从文件中读取数据

以下是示例的链接:

我的代码如下:

2.txt文件的内容如下:

a1 b1 c1 d1 e1 f1 g1 a2 b2 c2 d2 e2 f2 g2 a3 b3 c3 d3 e3 f3 g3 a1 b1 c1 d1 e1 f1 g1 a2 b2 c2 d2 e2 f2 g2 a3 b3 c3 d3 e3 f3 g3 我希望控制台中会有一些与文件内容相关的东西,但什么都没有。每秒钟除了这样的文本外,没有其他内容:

------------------------------------------- Time: 2015-09-03 15:08:18 ------------------------------------------- ------------------------------------------- 时间:2015-09-03 15:08:18 ------------------------------------------- 还有Spark的日志

我做错什么了吗?否则为什么它不工作?

我发现了问题

我想问题出在文件系统行为上。我用mac

我的程序没有看到文件,如果我只是复制它。 我的程序看到了这个文件,但当我在这个文件夹中创建文件时,它是空的,然后输入数据

最后,如果我创建文件并将其复制到扫描的目录中,并在未扫描目录的时间段内执行该操作,我的程序将看到文件和其中的任何内容


同样在问题文本中的代码中,我扫描了文件,但我应该扫描目录。

我遇到了类似的问题,但我意识到,一旦我设置流运行,streamingcontext就会从新文件中提取数据。一旦流媒体启动,它只接收新放置在源目录中的数据

实际上,pyspark文档非常明确:

textFileStream(目录)


如果使用jupyter notebook执行此问题,则需要在批处理层运行程序,然后使用jupyter将文本文件上载到指定的文档

Json数据:

{“时间戳”:“1571053218000”,“t1”:“55.23”,“t2”:“10”,“t3”:“ON”}

{“时间戳”:“1571053278000”,“t1”:“63.23”,“t2”:“11”,“t3”:“关闭”}

{“时间戳”:“1571053338000”,“t1”:“73.23”,“t2”:“12”,“t3”:“ON”}

{“时间戳”:“1571053398000”,“t1”:“83.23”,“t2”:“13”,“t3”:“ON”}

从上述json数据读取的Pyspark代码:
那是真的!您只需在智能提交作业后将输入文件复制到其中。因为流媒体只会处理新来的流媒体数据! ------------------------------------------- Time: 2015-09-03 15:08:18 -------------------------------------------
Create an input stream that monitors a Hadoop-compatible file system for new files and reads them as text files. Files must be wrriten to the monitored directory by “moving” them from another location within the same file system. File names starting with . are ignored.
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.streaming import StreamingContext
from pyspark.sql.types import IntegerType, LongType, DecimalType,StructType, StructField, StringType
from pyspark.sql import Row
from pyspark.sql.functions import col
import pyspark.sql.functions as F
from pyspark.sql import Window

sc = SparkContext.getOrCreate()
spark = SparkSession(sc)
ssc = StreamingContext(sc, 5)

stream_data = ssc.textFileStream("/filepath/")


def readMyStream(rdd):
  if not rdd.isEmpty():
    df = spark.read.json(rdd)
    print('Started the Process')
    print('Selection of Columns')
    df = df.select('t1','t2','t3','timestamp').where(col("timestamp").isNotNull())
    df.show()


stream_data.foreachRDD( lambda rdd: readMyStream(rdd) )
ssc.start()
ssc.stop()