在Spark Straming中使用DStream API从Kafka读取时间戳。

问题描述 投票:0回答:1

我想使用Python用Spark流读取Kafka主题的值。我正在使用DStream API,使用 spark-streaming-kafka-0-8 支持(虽然已经废弃)。我的代码如下。

from pyspark import SparkContext, SparkConf
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils

# Local SparkContext and StreamingContext (batch interval of 5 seconds)
sc = SparkContext(master="local[*]",
                  appName="Kafka-DStream-StdOut",
                  conf=SparkConf()
                  .set("spark.jars.packages", "org.apache.spark:spark-streaming-kafka-0-8_2.11:2.4.5"))
ssc = StreamingContext(sc, 5)

# Input: DStream from Apache Kafka
stream = KafkaUtils.createStream(
    ssc, "localhost:2181", "spark-streaming-consumer", {"test-topic": 1})

# Output: show stream in the console
stream.pprint()

ssc.start()
ssc.awaitTermination()

问题是我只接收到Kafka中存储的键和值,但没有时间戳。当从Kafka生产者每秒发送随机数时,我在控制台中看到这样的内容(代码为 此处):

-------------------------------------------
Time: 2020-04-30 17:12:10
-------------------------------------------
(None, '69')
(None, '68')
(None, '6')
(None, '25')
(None, '73')

有什么方法可以用这种方法在Spark中读取时间戳?

python apache-spark pyspark apache-kafka spark-streaming
1个回答
0
投票

Kafka消息的时间戳不是RDD记录的一部分,就像它在 结构化流

https:/mvnrepository.comartifactorg.apache.sparkspark-sql-kafka-0-10。

enter image description here

另外,不要使用Zookeeper连接到Kafka。始终使用 bootstrap.servers 财产

© www.soinside.com 2019 - 2024. All rights reserved.