我有一个运行在集群上的kafka实例,将消息发布到主题。当我触发命令时
./bin/kafka-console-consumer.sh --bootstrap-server kafka-broker:9094 --topic topic-name --consumer.config config/consumer.properties --from-beginning
我收到消息。
我的Consumer.properties:
bootstrap.servers = kafka-broker1:9094,kafka-broker2:9094,kafka-broker3:9094 sasl.jaas.config = org.apache.kafka.common.security.plain.PlainLoginModule必需username =“ username” password =“ password”;
sasl.mechanism = PLAIN security.protocol = SASL_SSL
ssl.truststore.location = / path / to / file.truststore ssl.truststore.password = xxxxxx ssl.keystore.location = /路径/到/file.keystore ssl.keystore.password = xxxxxx ssl.key.password = xxxxxx
现在,当我尝试使用Spark Scala程序获取这些消息时,我没有收到消息:
它们在控制台中没有错误显示。直到获取信息:
08/05/19 09:59:30 INFO AppInfoParser:Kafka版本:0.10.0.0 19/05/08 09:59:30 INFO AppInfoParser:Kafka commitId:ab1234567e89df
此后,控制台屏幕上没有任何内容。
我在Spark程序中的Kafka参数:
object SparkKafkaDemo {
def main(args:Array [String])= { val kafkaParams = Map [String,Object]( “ bootstrap.servers”->“ kafka-broker1:9094,kafka-broker2:9094,kafka-broker3:9094”, “ key.deserializer”-> classOf [StringDeserializer], “ value.deserializer”-> classOf [StringDeserializer], “ security.protocol”->“ SASL_SSL”, “ sasl.mechanism”->“ PLAIN”, “ group.id”->“ group-id-1”, “ auto.offset.reset”->“最早”, “ enable.auto.commit”->(false:java.lang.Boolean), “ ssl.truststore.location”->“ /path/to/truststore/file.truststore”, “ ssl.truststore.password”->“ xxxxxxxxxx”, “ ssl.keystore.location”->“ /path/to/keystore/file.keystore”, “ ssl.keystore.password”->“ xxxxxxxxxx”, “ ssl.key.password”->“ xxxxxxxxxx” ) val topic = Array(“ topic_name”)
val sparkConf = new SparkConf().setAppName("KafkaTest")
val streamingContext = new StreamingContext(sparkConf, Seconds(1))
// Create a input direct stream
val kafkaStream = KafkaUtils.createDirectStream[String, String](
streamingContext,
PreferConsistent,
Subscribe[String, String](topics, kafkaParams)
)
val result = kafkaStream.map(record => (record.topic(), record.value(), record.offset()))
println(result.map(line => line._1))
val recordsCount = result.count()
recordsCount.print()
result.foreachRDD(
rdd => {
val rdd1 = rdd.map(line => (line._1, line._2))
rdd.take(5).foreach(println)
}
)
streamingContext.start()
streamingContext.awaitTermination()
} }
我在spark程序中使用的库:-
spark-streaming-kafka-0-10_2.12-2.4.0 kafka-clients-2.2.0 spark-2.4.2-bin-hadoop2.7
我的spark-submit命令:-
bin/spark-submit --files /path/to/jaas.conf,/path/to/truststore/file.truststore,/path/to/keystore/file.keystore --driver-java-options "-Djava.security.auth.login.config=/path/to/jaas.conf" --conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=/path/to/jaas.conf" --class com.org.spark.SparkKafkaDemo --master local[*] --jars /path/to/spark-streaming-kafka-0-10_2.12-2.4.0.jar,/path/to/kafka-clients-0.10.0.0.jar /path/to/SparkKafkaDemo.jar
请帮助我,如果我错过了什么或做错了什么。 谢谢