【问题标题】:Spark Structural Streaming with Confluent Cloud Kafka connectivity issue带有 Confluent Cloud Kafka 连接问题的 Spark Structural Streaming
【发布时间】:2020-04-11 15:34:39
【问题描述】:

我正在 PySpark 中编写一个 Spark 结构化流应用程序,以从 Confluent Cloud 中的 Kafka 读取数据。 spark readstream() 函数的文档太浅了,并且没有在可选参数部分特别是在身份验证机制部分指定太多。我不确定哪个参数出错并导致连接崩溃。任何有 Spark 经验的人都可以帮助我开始这种连接吗?

必填参数

> Consumer({'bootstrap.servers':
> 'cluster.gcp.confluent.cloud:9092',
>               'sasl.username':'xxx',
>               'sasl.password':  'xxx',
>               'sasl.mechanisms': 'PLAIN',
>               'security.protocol': 'SASL_SSL',
>     'group.id': 'python_example_group_1',
>     'auto.offset.reset': 'earliest' })

这是我的 pyspark 代码:

df = spark \
  .readStream \
  .format("kafka") \
  .option("kafka.bootstrap.servers", "cluster.gcp.confluent.cloud:9092") \
  .option("subscribe", "test-topic") \
  .option("kafka.sasl.mechanisms", "PLAIN")\
  .option("kafka.security.protocol", "SASL_SSL")\
  .option("kafka.sasl.username","xxx")\
  .option("kafka.sasl.password", "xxx")\
  .option("startingOffsets", "latest")\
  .option("kafka.group.id", "python_example_group_1")\
  .load()
display(df)

但是,我不断收到错误消息:

kafkashaded.org.apache.kafka.common.KafkaException: 失败 构建kafka消费者

DataBrick Notebook- 用于测试

https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/4673082066872014/3543014086288496/1802788104169533/latest.html

文档

https://home.apache.org/~pwendell/spark-nightly/spark-branch-2.0-docs/latest/structured-streaming-kafka-integration.html

【问题讨论】:

  • 您是否尝试过使用其他消费者? spark.apache.org/docs/2.3.1/…
  • @cricket_007,还没有,因为要求之一是流-流连接,它只支持结构化流。 Direct Stream 在加入两个流时有限制。因此,这就是我需要使用readStream() 的原因。在线资源只关注 DStream。很难找到答案

标签: apache-spark pyspark apache-kafka spark-streaming


【解决方案1】:

此错误表明 JAAS 配置对您的 Kafka 使用者不可见。为了解决这个问题,包括基于以下步骤的 JASS:

Step01:为下面的 JAAS 文件创建一个文件:/home/jass/path

KafkaClient {
     com.sun.security.auth.module.Krb5LoginModule required
     useTicketCache=true
     renewTicket=true
     serviceName="kafka";
     };

Step02:根据下面的conf参数在spark-submit中调用那个JASS文件路径。

--conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=/home/jass/path"

完整的 spark-submit 命令:

/usr/hdp/2.6.1.0-129/spark2/bin/spark-submit --packages com.databricks:spark-avro_2.11:3.2.0,org.apache.spark:spark-avro_2.11:2.4.0,org.apache.spark:spark-sql-kafka-0-10_2.11:2.2.0 --conf spark.ui.port=4055 --files /home/jass/path,/home/bdpda/bdpda.headless.keytab --conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=/home/jass/path" --conf "spark.driver.extraJavaOptions=-Djava.security.auth.login.config=/home/jass/path" pysparkstructurestreaming.py

Pyspark 结构化流式传输示例代码:

from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import *
from pyspark.streaming import StreamingContext
import time

#  Spark Streaming context :

spark = SparkSession.builder.appName('PythonStreamingDirectKafkaWordCount').getOrCreate()
sc = spark.sparkContext
ssc = StreamingContext(sc, 20)

#  Kafka Topic Details :

KAFKA_TOPIC_NAME_CONS = "topic_name"
KAFKA_OUTPUT_TOPIC_NAME_CONS = "topic_to_hdfs"
KAFKA_BOOTSTRAP_SERVERS_CONS = 'kafka_server:9093'

#  Creating  readstream DataFrame :

df = spark.readStream \
     .format("kafka") \
     .option("kafka.bootstrap.servers", KAFKA_BOOTSTRAP_SERVERS_CONS) \
     .option("subscribe", KAFKA_TOPIC_NAME_CONS) \
     .option("startingOffsets", "earliest") \
     .option("kafka.security.protocol","SASL_SSL")\
     .option("kafka.client.id" ,"Clinet_id")\
     .option("kafka.sasl.kerberos.service.name","kafka")\
     .option("kafka.ssl.truststore.location", "/home/path/kafka_trust.jks") \
     .option("kafka.ssl.truststore.password", "password_rd") \
     .option("kafka.sasl.kerberos.keytab","/home/path.keytab") \
     .option("kafka.sasl.kerberos.principal","path") \
     .load()

df1 = df.selectExpr( "CAST(value AS STRING)")

#  Creating  Writestream DataFrame :

df1.writeStream \
   .option("path","target_directory") \
   .format("csv") \
   .option("checkpointLocation","chkpint_directory") \
   .outputMode("append") \
   .start()

ssc.awaitTermination()

【讨论】:

  • 感谢您精心编辑的答案。它实际上在 Spark Cluster 中工作。对于spark-shell 用例,我们可能需要添加--driver-java-options "-Djava.security.auth.login.config=/home/jass/path"
【解决方案2】:

我们需要指定 kafka.sasl.jaas.config 来添加 Confluent Kafka SASL-SSL 身份验证方法的用户名和密码。它的参数看起来有点奇怪,但它正在工作。

df = spark \
      .readStream \
      .format("kafka") \
      .option("kafka.bootstrap.servers", "pkc-43n10.us-central1.gcp.confluent.cloud:9092") \
      .option("subscribe", "wallet_txn_log") \
      .option("startingOffsets", "earliest") \
      .option("kafka.security.protocol","SASL_SSL") \
      .option("kafka.sasl.mechanism", "PLAIN") \
      .option("kafka.sasl.jaas.config", """kafkashaded.org.apache.kafka.common.security.plain.PlainLoginModule required username="xxx" password="xxx";""").load()
display(df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2019-07-12
    • 2019-08-08
    相关资源
    最近更新 更多