簡體   English   中英

Spark Streaming:Spark Structured Streaming 中不允許使用 Kafka 組 ID

[英]Spark Streaming: Kafka group id not permitted in Spark Structured Streaming

我正在 PySpark 中編寫一個 Spark 結構化流應用程序來從 Kafka 讀取數據。

但是目前Spark的版本是2.1.0,不允許我設置group id作為參數,每次查詢都會生成一個唯一的id。 但是 Kafka 連接是基於組的授權,需要預先設置組 ID。

因此,是否有任何解決方法可以在不需要將 Spark 更新到 2.2 的情況下建立連接因為我的團隊不想要它。

我的代碼:

if __name__ == "__main__":
    spark = SparkSession.builder.appName("DNS").getOrCreate()
    sc = spark.sparkContext
    sc.setLogLevel("WARN")

    # Subscribe to 1 topic
    lines = spark.readStream.format("kafka").option("kafka.bootstrap.servers", "host:9092").option("subscribe", "record").option('kafka.security.protocol',"SASL_PLAINTEXT").load()
    print(lines.isStreaming) #print TRUE
    lines.selectExpr("CAST(value AS STRING)")
    # Split the lines into words
    words = lines.select(
    explode(
        split(lines.value, " ")
        ).alias("word")
    )
    # Generate running word count
    wordCounts = words.groupBy("word").count()

    # Start running the query that prints the running counts to the console
    query = wordCounts \
        .writeStream \
        .outputMode("complete") \
        .format("console") \
        .start()

    query.awaitTermination()

KafkaUtils類將覆蓋"group.id"的參數值。 它將從原始組 ID 中連接"spark-executor-"

以下是來自 KafkaUtils 的代碼,其中執行此操作:

// driver and executor should be in different consumer groups
    val originalGroupId = kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG)
    if (null == originalGroupId) {
      logError(s"${ConsumerConfig.GROUP_ID_CONFIG} is null, you should probably set it")
    }
    val groupId = "spark-executor-" + originalGroupId
    logWarning(s"overriding executor ${ConsumerConfig.GROUP_ID_CONFIG} to ${groupId}")
    kafkaParams.put(ConsumerConfig.GROUP_ID_CONFIG, groupId)

我們遇到了同樣的問題。 Kafka 基於具有預設組 ID 的 ACL,因此唯一要做的就是在 kafka 配置中更改組 ID。 在我們原來的組 id 之外,我們把"spark-executor-" + originalGroupId

Spark 3.x 現在可以設置 group.id。 請參閱結構化流 + Kafka 集成指南,其中說:

kafka.group.id :從 Kafka 讀取時在 Kafka 消費者中使用的 Kafka 組 ID。 請謹慎使用。 默認情況下,每個查詢都會生成一個唯一的組 ID 用於讀取數據。 這確保了每個 Kafka 源都有自己的消費者組,不會受到任何其他消費者的干擾,因此可以讀取其訂閱主題的所有分區。 在某些場景下(例如Kafka基於組的授權),您可能希望使用特定的授權組id來讀取數據。 您可以選擇設置組 ID。 但是,請務必小心執行此操作,因為它可能會導致意外行為。 同時運行的查詢(批處理和流)或具有相同組 ID 的源可能會相互干擾,導致每個查詢僅讀取部分數據。 當查詢快速連續啟動/重新啟動時,也可能發生這種情況。 為了盡量減少此類問題,請將 Kafka 消費者會話超時(通過設置選項“kafka.session.timeout.ms”)設置得非常小。 設置此項后,將忽略選項“groupIdPrefix”。

然而,這個 group.id 仍然不用於將偏移量提交回 Kafka,並且偏移量管理保留在 Spark 的檢查點文件中。 我在我的回答中提供了更多細節(也適用於 Spark < 3.x):

暫無
暫無

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

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