簡體   English   中英

將collect()應用於Apache Spark結構化的流數據集

[英]Applying collect() to a Apache Spark structured streaming Dataset

我是Apache Spark的新手,目前正在使用結構化流水線。 在數據處理的中間,我需要做一些細致的操作,要求(到目前為止) 所有數據都存在。 此時,管道中的數據量已大大減少,執行類似.collect()的操作將不會成為瓶頸。 我需要執行的操作基本上是將所有剩余元素放入HashSet中,並進行一系列棘手的存在性檢查。 之后,我需要“重新輸入”流傳輸管道以對csv文件執行各種寫入操作。

但是,嘗試在流傳輸管道上執行collect()導致錯誤消息。 下面是一個准系統(和愚蠢的)示例,它說明了我的問題:

// imports ...

val spark = SparkSession.builder
                        .appName("StructuredNetworkWordCount")
                        .getOrCreate()
val lines = spark.readStream
                 .format("socket")
                 .option("host", "localhost")
                 .option("port", 4444)
                 .load()

import spark.implicits._

// Split the lines into words
val words = lines.as[String].flatMap(_.split(" "))

// Won't work in a streaming context
val wordList = words.collectAsList()

// Perform some operations on the collected() data
val numWords = wordList.size
val doubledNum = numWords * 2

// Somehow output doubledNum
val query = wordCounts.writeStream
                      .outputMode("complete")
                      .format("console")
                      .start()

query.awaitTermination()

正如我所說,這絕對不會奏效,但可以說明我的問題。 我需要在每個微批處理的中間執行一個類collect()的操作,以便同時訪問剩下的所有數據。 我將如何去做呢? 累加器是訪問流管道中間所有分區中所有累積數據的唯一方法嗎?

謝謝!

首先,Spark結構流式傳輸返回DataFrame對象,它不支持map和flatMap方法,因此您可以使用foreach方法,在此方法中,您可以操縱輸入流數據並使用counter來計數所有必需的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM