嗨,我是spark和kafka的新手,我正在编写示例代码,使用spark来使用kafka主题中的消息,
object Init {
def main(args: Array[String]): Unit = {
val kafkaParams = Map[String, Object](
"bootstrap.servers" -> "ip-10-0-1-10.ec2.internal:9092",
"key.deserializer" -> classOf[StringDeserializer],
"value.deserializer" -> classOf[StringDeserializer],
"group.id" -> "12",
"auto.offset.reset" -> "earliest",
"enable.auto.commit" -> (false: java.lang.Boolean)
)
val topics = Array("TestLogs")
val stream = KafkaUtils.createDirectStream[String, String](
SparkConfig.streamContext,
PreferConsistent,
Subscribe[String, String](topics, kafkaParams)
)
stream.print()
SparkConfig.streamContext.start()
SparkConfig.streamContext.awaitTermination()
}
}
当我在集群上使用
"spark2-submit --jars spark-streaming-kafka-0-10_2.11-2.3.0.jar --class Init
--master local kafkademo_2.11-0.1.jar"
消费者将进入无限循环打印没有消息,我通过ctrl+c显式终止进程
INFO consumer.ConsumerConfig: ConsumerConfig values:
metric.reporters = []
metadata.max.age.ms = 300000
partition.assignment.strategy = [org.apache.kafka.clients.consumer.RangeAssignor]
reconnect.backoff.ms = 50
sasl.kerberos.ticket.renew.window.factor = 0.8
max.partition.fetch.bytes = 1048576
bootstrap.servers = [ip-10-0-1-10.ec2.internal:9092]
ssl.keystore.type = JKS
enable.auto.commit = false
sasl.mechanism = GSSAPI
interceptor.classes = null
exclude.internal.topics = true
ssl.truststore.password = null
client.id = consumer-1
ssl.endpoint.identification.algorithm = null
max.poll.records = 2147483647
check.crcs = true
request.timeout.ms = 40000
heartbeat.interval.ms = 3000
auto.commit.interval.ms = 5000
receive.buffer.bytes = 65536
ssl.truststore.type = JKS
ssl.truststore.location = null
ssl.keystore.password = null
fetch.min.bytes = 1
send.buffer.bytes = 131072
value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
group.id = 12
retry.backoff.ms = 100
ssl.secure.random.implementation = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
ssl.trustmanager.algorithm = PKIX
ssl.key.password = null
fetch.max.wait.ms = 500
sasl.kerberos.min.time.before.relogin = 60000
connections.max.idle.ms = 540000
session.timeout.ms = 30000
metrics.num.samples = 2
key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
ssl.protocol = TLS
ssl.provider = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.keystore.location = null
ssl.cipher.suites = null
security.protocol = PLAINTEXT
ssl.keymanager.algorithm = SunX509
metrics.sample.window.ms = 30000
auto.offset.reset = latest
18/09/11 07:03:05 info utils.appinfoparser:Kafka版本:0.10.0-kafka-2.1.0 18/09/11 07:03:05 info utils.appinfoparser:Kafka委员会:未知
如果我使用控制台消费者进行测试,它会显示消息,
kafka-console-consumer --zookeeper ip-10-0-2-11.ec2.internal:2181 --topic
TestLogs --from-beginning
INFO consumer.ConsumerFetcherManager: [ConsumerFetcherManager-1536650065051]
Added fetcher for partitions ArrayBuffer([TestLogs-0, initOffset -1 to broker
BrokerEndPoint(177,ip-10-0-1-10.ec2.internal,9092)] )
Welcome to Kafka APIS
请帮我解决这个问题。
2条答案
按热度按时间h79rfbju1#
我不能重现的问题,你有可能是因为依赖性的问题,但我可以提供样本工作代码,你将能够听任何主题。
我正在使用spark流媒体库来流式传输Kafka主题的数据。请让我知道,如果你发现任何问题,而使用上述代码。
gdx19jrr2#
首先确保主题testlogs包含数据,然后如果您已经使用了组id为12的消息,那么您将只收到尚未为该特定组id提交的新消息:在这种情况下,您可以通过重置该组的kafka偏移量或简单地更改组id(例如13)来重播主题。