Apache kafka 制片人正在将内容发布到卡夫卡,但无法读取Spark结构化流媒体
我使用卡夫卡发布推文,它运行正常,因为我可以通过以下命令看到回声Apache kafka 制片人正在将内容发布到卡夫卡,但无法读取Spark结构化流媒体,apache-kafka,spark-structured-streaming,spark-kafka-integration,Apache Kafka,Spark Structured Streaming,Spark Kafka Integration,我使用卡夫卡发布推文,它运行正常,因为我可以通过以下命令看到回声 bin/kafka-console-consumer.sh --bootstrap-server xxx.xxx.xx.xxx:9092 --topic trump --from-beginning 但是当我尝试使用以下代码使用结构化流媒体时 if __name__ == "__main__": spark = SparkSession.builder.appName("TwitterSe
bin/kafka-console-consumer.sh --bootstrap-server xxx.xxx.xx.xxx:9092 --topic trump --from-beginning
但是当我尝试使用以下代码使用结构化流媒体时
if __name__ == "__main__":
spark = SparkSession.builder.appName("TwitterSentimentAnalysis").getOrCreate()
source_df = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "xxx.xxx.xx.xxx:9092") \
.option("subscribe", "tweets") \
.option("startingOffsets", "latest") \
.load()
query = source_df \
.writeStream \
.outputMode("append") \
.format("console") \
.start()
然后我得到了一个输出,但它没有在value列下显示Tweet。相反,我有一个奇怪的字母数字链,如下所示。我在没有截断列值的情况下进行了检查,得到了相同但更长的模式
+----+--------------------+------+---------+------+--------------------+-------------+
| key| value| topic|partition|offset| timestamp|timestampType|
+----+--------------------+------+---------+------+--------------------+-------------+
|null|[52 54 20 40 70 7...|tweets| 0| 45724|2021-03-17 12:57:...| 0|
|null|[23 52 57 52 49 2...|tweets| 0| 45725|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 54 7...|tweets| 0| 45726|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 44 6...|tweets| 0| 45727|2021-03-17 12:57:...| 0|
|null|[40 42 42 43 50 6...|tweets| 0| 45728|2021-03-17 12:57:...| 0|
|null|[40 4C 6F 72 64 5...|tweets| 0| 45729|2021-03-17 12:57:...| 0|
|null|[41 6E 6E 6F 75 6...|tweets| 0| 45730|2021-03-17 12:57:...| 0|
|null|[42 69 74 63 6F 6...|tweets| 0| 45731|2021-03-17 12:57:...| 0|
|null|[40 65 72 69 6B 7...|tweets| 0| 45732|2021-03-17 12:57:...| 0|
|null|[74 68 65 20 6D 6...|tweets| 0| 45733|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 6D 6...|tweets| 0| 45734|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 6D 6...|tweets| 0| 45735|2021-03-17 12:57:...| 0|
|null|[40 42 54 43 54 4...|tweets| 0| 45736|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 49 6...|tweets| 0| 45737|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 63 6...|tweets| 0| 45738|2021-03-17 12:57:...| 0|
|null|[42 75 20 6F 6C 6...|tweets| 0| 45739|2021-03-17 12:57:...| 0|
|null|[40 5F 43 72 79 7...|tweets| 0| 45740|2021-03-17 12:57:...| 0|
|null|[40 57 69 6E 66 6...|tweets| 0| 45741|2021-03-17 12:57:...| 0|
|null|[4D 79 20 72 65 6...|tweets| 0| 45742|2021-03-17 12:57:...| 0|
|null|[52 54 20 40 6F 6...|tweets| 0| 45743|2021-03-17 12:57:...| 0|
+----+--------------------+------+---------+------+--------------------+-------------+
only showing top 20 rows
了解情况的任何帮助都将不胜感激。默认情况下,存储在卡夫卡中的数据(列键
和值
)将序列化为字符串
查看,您将看到两列键
和值
的类型为二进制
:
此外,指南中还提到了如何处理这一问题。您需要将列强制转换为字符串类型,如下所示:
source\u df.selectExpr(“转换(键为字符串)”,“转换(值为字符串)”)\
.writeStream\
.outputMode(“追加”)\
.格式(“控制台”)\
.start()