问题描述
我正在做一个流媒体项目,我有一个 kafka ping 统计数据流,如下所示:
I am working on a streaming project where I have a kafka stream of ping statistics like so :
64 bytes from vas.fractalanalytics.com (192.168.30.26): icmp_seq=1 ttl=62 time=0.913 ms
64 bytes from vas.fractalanalytics.com (192.168.30.26): icmp_seq=2 ttl=62 time=0.936 ms
64 bytes from vas.fractalanalytics.com (192.168.30.26): icmp_seq=3 ttl=62 time=0.980 ms
64 bytes from vas.fractalanalytics.com (192.168.30.26): icmp_seq=4 ttl=62 time=0.889 ms
我正在尝试将其作为 pyspark
中的结构化流读取.我使用以下命令启动 pyspark:
I am trying to read this as a structured stream in pyspark
. I start pyspark with the following command :
pyspark --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.0
Pyspark 版本是 2.4,python 版本是 2.7(也尝试过 3.6)
Pyspark version is 2.4, python version is 2.7 (tried with 3.6 as well)
一旦我发送这段代码,我就会收到一个错误(从 结构化流媒体 + Kafka 集成指南):
And I get an error as soon as I send this piece of code (followed from Structured Streaming + Kafka Integration Guide):
df = spark.readStream.format("kafka").option("kafka.bootstrap.servers", "172.18.2.21:2181").option("subscribe", "ping-stats").load()
我遇到了以下错误:
py4j.protocol.Py4JJavaError: An error occurred while calling o37.load.
: java.util.ServiceConfigurationError: org.apache.spark.sql.sources.DataSourceRegister: Provider org.apache.spark.sql.kafka010.KafkaSourceProvider could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at scala.collection.convert.Wrappers$JIteratorWrapper.next(Wrappers.scala:43)
at scala.collection.Iterator$class.foreach(Iterator.scala:891)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1334)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.TraversableLike$class.filterImpl(TraversableLike.scala:247)
at scala.collection.TraversableLike$class.filter(TraversableLike.scala:259)
at scala.collection.AbstractTraversable.filter(Traversable.scala:104)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:630)
at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodError: org.apache.spark.internal.Logging.$init$(Lorg/apache/spark/internal/Logging;)V
at org.apache.spark.sql.kafka010.KafkaSourceProvider.<init>(KafkaSourceProvider.scala:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
... 23 more
有人可以帮我解决这个问题吗?
Can someone help me out with this?
推荐答案
我设法通过确保 spark-sql-kafka 包的版本与 spark 版本匹配来解决这个问题.
I managed to solve this by ensuring that the spark-sql-kafka package's version matches the spark version.
就我而言,我现在使用 --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.1
作为我的 spark 版本 2.4.1,此后代码的.format(kafka")
部分就可以解析了.
In my case, I am now using --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.4.1
for my spark version 2.4.1, thereafter the .format("kafka")
part of the code can be resolved.
此外,该软件包的 v2.12(即 org.apache.spark:spark-sql-kafka-0-10_2.12:2.4.1
)在当时似乎并不稳定写的,使用也会导致上面的错误.
Also, v2.12 of the package (i.e., org.apache.spark:spark-sql-kafka-0-10_2.12:2.4.1
) does not seem stable at the time of writing, and using it will also cause the above error.
* v2.12 spark-sql-kafka
包似乎只适用于用 Scala v2.12 构建的 Spark.因此,对于 Spark v2.X 版本(默认使用 Scala v2.11 预构建),需要使用使用 Scala v2.12 构建的 Spark 二进制文件(例如 spark-2.4.1-bin-without-hadoop-scala-2.12.tgz
) 如果你真的想使用 spark-sql-kafka
v2.12 包.对于 Spark v3.X,它们默认使用 Scala v2.12 预先构建,因此您只会看到/使用包的 v2.12.
* v2.12 spark-sql-kafka
packages seem to only work with Spark built with Scala v2.12. Hence, for Spark v2.X versions (pre-built with Scala v2.11 by default), there's a need to instead use Spark binaries built with Scala v2.12 (e.g. spark-2.4.1-bin-without-hadoop-scala-2.12.tgz
) if you really want to use spark-sql-kafka
v2.12 package. For Spark v3.X, they are pre-built with Scala v2.12 by default, hence you'll only see/use v2.12 of the package.
这篇关于无法实例化 Kafka 结构化流 KafkaSourceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!