Skip to main content
 首页 » 编程设计

python之Apache Spark Streaming 不读取目录

2025年05月04日20over140

我正在处理 Spark Streaming,我想设置一个本地目录以将数据流式传输到我的 spark 应用程序,以便该目录中的每个新文本文件都将流式传输到我的应用程序。我尝试使用 StreamingContext 的 textFileStream 方法,但我没有从已移动到指定本地目录的文件中获取任何数据。你能帮我找出为什么会这样吗?

所以这是我写的代码:

def main(): 
 
    if len(sys.argv) != 5: 
    print 'Usage: SPARK_HOME/bin/spark-submit CoinpipeVectorBuilder.py <SPARK_HOME> <dir_streaming> ' \ 
          '<dir_crawled_addresses> <dir_output_vectors>' 
    sys.exit(1) 
 
    #Set the path to crawled outputs according to the parameter passed to the spark script 
    global path_crawled_output 
    path_crawled_output = sys.argv[4] 
 
    global sc, ssc 
    sconf = SparkConf().setMaster("local[2]")\ 
        .setAppName("CoinPipeVectorBuilder")\ 
        .set("spark.hadoop.validateOutputSpecs", "false") 
    sc = SparkContext(conf=sconf) 
    ssc = StreamingContext(sc, 10) 
    tx_and_addr_stream = ssc.textFileStream(sys.argv[2]) 
 
    tx_and_addr_stream.foreachRDD(parseAndBuildVectors) 
 
    ssc.start() 
    ssc.awaitTermination() 
 
if __name__ == "__main__": 
    main() 

所以在 parseAndBuildVectors 中,即使我将一个新文件移动到我传递给 ssc.textFileStream

的指定目录,我也得不到任何数据

请您参考如下方法:

Spark 代码在工作时执行。所以工作对您的本地文件系统没有任何访问权限。这是不可能直接的。您可以读取流文件然后制作 RDD 然后可以使用 spark 执行操作。 Spark 只能访问分布式数据。