如何运行pyspark代码从Kafka获取数据并将其转换为dataframe?

xlpyo6sf  于 2023-01-31  发布在  Apache
关注(0)|答案(1)|浏览(172)

我正在尝试将Kafka主题导入spark dataframe,代码如下:

from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import *

# Create a SparkSession
spark = SparkSession.builder \
    .appName("KafkaStreamToDataFrame") \
    .getOrCreate()

# Define the schema for the data in the Kafka stream
schema = StructType([
    StructField("key", StringType()),
    StructField("value", StringType())
])

# Read the data from the Kafka stream
df = spark \
    .readStream \
    .format("kafka") \
    .option("kafka.bootstrap.servers", "kafka_host:9092") \
    .option("subscribe", "ext_device-measurement_10121") \
    .load() \
    .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)") \
    .select(from_json(col("value"), schema).alias("data")) \
    .select("data.*")

# Start the stream and display the data in the DataFrame
query = df \
    .writeStream \
    .format("console") \
    .start()

query.awaitTermination()

我尝试使用spark-submit来执行代码:spark-submit --packages org.apache.spark:spark-sql-kafka-0-10_2.12:3.3.1 dev_ev.pySpark版本:3.3.1
尝试执行时出现以下错误:

File "/home/avs/avnish_spark/dev_ev.py", line 21, in <module>
    .option("subscribe", "ext_device-measurement_10121") \
  File "/opt/avnish/spark-3.3.1-bin-hadoop3/python/lib/pyspark.zip/pyspark/sql/streaming.py", line 469, in load
  File "/opt/avnish/spark-3.3.1-bin-hadoop3/python/lib/py4j-0.10.9.5-src.zip/py4j/java_gateway.py", line 1322, in __call__
  File "/opt/avnish/spark-3.3.1-bin-hadoop3/python/lib/pyspark.zip/pyspark/sql/utils.py", line 190, in deco
  File "/opt/avnish/spark-3.3.1-bin-hadoop3/python/lib/py4j-0.10.9.5-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o35.load.
: java.lang.NoClassDefFoundError: scala/$less$colon$less
    at org.apache.spark.sql.kafka010.KafkaSourceProvider.org$apache$spark$sql$kafka010$KafkaSourceProvider$$validateStreamOptions(KafkaSourceProvider.scala:338)
    at org.apache.spark.sql.kafka010.KafkaSourceProvider.sourceSchema(KafkaSourceProvider.scala:71)
    at org.apache.spark.sql.execution.datasources.DataSource.sourceSchema(DataSource.scala:236)
    at org.apache.spark.sql.execution.datasources.DataSource.sourceInfo$lzycompute(DataSource.scala:118)
    at org.apache.spark.sql.execution.datasources.DataSource.sourceInfo(DataSource.scala:118)
    at org.apache.spark.sql.execution.streaming.StreamingRelation$.apply(StreamingRelation.scala:34)
    at org.apache.spark.sql.streaming.DataStreamReader.loadInternal(DataStreamReader.scala:168)
    at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:144)
    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.ClientServerConnection.waitForCommands(ClientServerConnection.java:182)
    at py4j.ClientServerConnection.run(ClientServerConnection.java:106)
    at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.ClassNotFoundException: scala.$less$colon$less
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 20 more

不知道出了什么问题,Kafka的主题是可及的,并推动json记录。
尝试手动下载jar文件并将其保存在SPARK_HOME的jars目录中,然后使用以下命令执行:spark-submit --jars $SPARK_HOME/jars/org.apache.spark:spark-sql-kafka-0-10_2.12:3.3.1 dev_ev.py我希望显示 Dataframe 。

qkf9rpyu

qkf9rpyu1#

我在你的代码中添加了startingOffsets,它对我来说毫无例外地起作用了。

df = spark \
    .readStream \
    .format("kafka") \
    .option("kafka.bootstrap.servers", "localhost:9092") \
    .option("subscribe", "test.topic") \
    .option("startingOffsets", "latest") \
    .load() \
    .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)") \
    .select(from_json(col("value"), schema).alias("data")) \
    .select("data.*")

相关问题