我想使用Spark Streaming并将其与Kafka连接。但是我仍然得到NoSuchMethodError: org.apache.kafka.clients.consumer.KafkaConsumer.subscribe错误,现在我只是不知道接下来该做什么。
我的设置:
Ubuntu 16.04
Scala 2.11
Kafka 2.11-1.0.0(我也试过使用2.11-0.10.0.0)
Spark 2.2.1
Hadoop 2.9.0
我甚至无法运行示例脚本:
from pyspark.sql import SparkSession, Row, SQLContext
from pyspark import SparkContext, SparkConf
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
from pyspark.sql.types import *
sparkSession = SparkSession.builder.appName("example-pyspark-read-and-write").getOrCreate()
# Subscribe to 1 topic
df = sparkSession\
.readStream\
.format("kafka")\
.option("kafka.bootstrap.servers", "localhost:9092")\
.option("kafka.partition.assignment.strategy", "range") \
.option("subscribe", "test")\
.option("startingOffsets", "earliest")\
.load()
query = df.writeStream\
.format("console")\
.start()
query.awaitTermination()
我使用
在Spark上运行它spark-submit --master local[2] --jars /home/some_path/spark-sql-kafka-0-10_2.11-2.2.1.jar spark_streaming_kafka_example.py
我收到错误
Exception in thread "stream execution thread for [id = 38ee73d5-4f20-41d0-ac89-a29c3f3255d1, runId = dadfc8ab-8e4c-464f-b4ef-495426aafc88]" java.lang.NoSuchMethodError: org.apache.kafka.clients.consumer.KafkaConsumer.subscribe(Ljava/util/Collection;)V
at org.apache.spark.sql.kafka010.SubscribeStrategy.createConsumer(ConsumerStrategy.scala:63)
at org.apache.spark.sql.kafka010.KafkaOffsetReader.createConsumer(KafkaOffsetReader.scala:297)
at org.apache.spark.sql.kafka010.KafkaOffsetReader.<init>(KafkaOffsetReader.scala:78)
at org.apache.spark.sql.kafka010.KafkaSourceProvider.createSource(KafkaSourceProvider.scala:88)
at org.apache.spark.sql.execution.datasources.DataSource.createSource(DataSource.scala:244)
at org.apache.spark.sql.execution.streaming.StreamExecution$$anonfun$2$$anonfun$applyOrElse$1.apply(StreamExecution.scala:158)
at org.apache.spark.sql.execution.streaming.StreamExecution$$anonfun$2$$anonfun$applyOrElse$1.apply(StreamExecution.scala:155)
at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:194)
at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:80)
at org.apache.spark.sql.execution.streaming.StreamExecution$$anonfun$2.applyOrElse(StreamExecution.scala:155)
at org.apache.spark.sql.execution.streaming.StreamExecution$$anonfun$2.applyOrElse(StreamExecution.scala:153)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$2.apply(TreeNode.scala:267)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$2.apply(TreeNode.scala:267)
at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:70)
at org.apache.spark.sql.catalyst.trees.TreeNode.transformDown(TreeNode.scala:266)
at org.apache.spark.sql.catalyst.trees.TreeNode.transform(TreeNode.scala:256)
at org.apache.spark.sql.execution.streaming.StreamExecution.logicalPlan$lzycompute(StreamExecution.scala:153)
at org.apache.spark.sql.execution.streaming.StreamExecution.logicalPlan(StreamExecution.scala:147)
at org.apache.spark.sql.execution.streaming.StreamExecution.org$apache$spark$sql$execution$streaming$StreamExecution$$runBatches(StreamExecution.scala:276)
at org.apache.spark.sql.execution.streaming.StreamExecution$$anon$1.run(StreamExecution.scala:206)
Traceback (most recent call last):
File "/home/some_path/spark_streaming_kafka_example.py", line 41, in <module>
query.awaitTermination()
File "/usr/local/spark/spark-2.2.1-bin-without-hadoop/python/lib/pyspark.zip/pyspark/sql/streaming.py", line 106, in awaitTermination
File "/usr/local/spark/spark-2.2.1-bin-without-hadoop/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
File "/usr/local/spark/spark-2.2.1-bin-without-hadoop/python/lib/pyspark.zip/pyspark/sql/utils.py", line 75, in deco
pyspark.sql.utils.StreamingQueryException: u'org.apache.kafka.clients.consumer.KafkaConsumer.subscribe(Ljava/util/Collection;)V\n=== Streaming Query ===\nIdentifier: [id = 38ee73d5-4f20-41d0-ac89-a29c3f3255d1, runId = dadfc8ab-8e4c-464f-b4ef-495426aafc88]\nCurrent Committed Offsets: {}\nCurrent Available Offsets: {}\n\nCurrent State: INITIALIZING\nThread State: RUNNABLE'
我还尝试在--jars中包含spark-streaming-kafka-0-10-assembly_2.11-2.2.1(而不是spark-sql-kafka-0-10_2.11-2.2.1.jar ),但它也没有帮助。
我也尝试将这两行放在spark-defaults.conf
中spark.driver.extraClassPath /home/some_path/spark-streaming-kafka-0-10-assembly_2.11-2.2.1.jar
spark.executor.extraClassPath /home/some_path/spark-streaming-kafka-0-10-assembly_2.11-2.2.1.jar
OR
spark.driver.extraClassPath /home/some_path/spark-sql-kafka-0-10_2.11-2.2.1.jar
spark.executor.extraClassPath /home/some_path/spark-sql-kafka-0-10_2.11-2.2.1.jar
答案 0 :(得分:1)
为了将Spark Structured Streaming与Kafka一起使用,您还需要在spark.driver.extraClassPath
&amp;中添加以下JAR。 spark.executor.extraClassPath
:
/home/some_path/kafka-clients-0.10.2.1.jar
由于spark-sql-kafka-0-10_2.11-2.2.1.jar
不包含KafkaConsumer
,我们还需要添加上述JAR文件。所以,最终的spark-submit
命令将是这样的:
spark-submit --master local [2] --jars /home/some_path/spark-sql-kafka-0-10_2.11-2.2.1.jar:/home/some_path/kafka-clients-0.10。 2.1.jar spark_streaming_kafka_example.py
此外,由于您使用的是Spark Structured Streaming而不是Spark Streaming,因此,您不需要包含任何spark-streaming
JAR。
我希望它有所帮助!
答案 1 :(得分:0)
尝试在export SPARK_KAFKA_VERSION=0.10
命令之前运行spark2-submit
。
[Source for solution]