无法使用Spark结构化流从Kafka主题获取消息

时间:2018-12-14 09:26:11

标签: spark-structured-streaming

我使用以下命令创建了kafka主题

kafka-topics --create --zookeeper 10.0.2.11:2181 --replication-factor 1 --partitions 1 --topic xmlStore

我正在使用Spark结构化流媒体来订阅和阅读主题。下面是代码

object Test {
  val spark = SparkSession.builder
 .appName("Spark-Kafka-Integration")
 .master("local")
 .getOrCreate()

 def main(args: Array[String]): Unit = {

    println("Inside Main ===>")
    import spark.implicits._
    val inputDf = spark
   .readStream
   .format("kafka")
   .option("kafka.bootstrap.servers", "localhost:9092")
   .option("subscribe", "xmlStore")
   .option("startingOffsets" , "earliest")
   .load()
   inputDf.printSchema()
   val dataSet: Dataset[(String, String)] =inputDf.selectExpr("CAST(key AS 
   STRING)", "CAST(value AS STRING)")
  .as[(String, String)]

   val query = dataSet
     .writeStream
     .outputMode("append")
     .format("memory")
     .queryName("op")
     .start()

    println("*********" +query.isActive)
    spark.sql("select * from op").show(false)
    query.explain()
    query.awaitTermination()
    }
   }

提交上述Spark消费者工作后,我使用以下命令生成值     kafka-console-producer --broker-list 10.0.1.10:9092 --topic xmlStore

  

你好吗?

我在控制台上看不到任何值。我也尝试过

   dataSet
  .writeStream
  .outputMode("append")
  .format("console")
  .start()

仍然不显示任何值。我不知道出了什么问题。请帮帮我。谢谢

我在控制台的输出下方,

18/12/14 07:15:09 INFO metadata.Hive: Registering function 
tempfromfartocelcius com.hive.temparature.udf.ConvertToCelcius
root
 |-- key: binary (nullable = true)
 |-- value: binary (nullable = true)
 |-- topic: string (nullable = true)
 |-- partition: integer (nullable = true)
 |-- offset: long (nullable = true)
 |-- timestamp: timestamp (nullable = true)
 |-- timestampType: integer (nullable = true)


 18/12/14 07:15:11 INFO streaming.MicroBatchExecution: Starting op [id = 
 dfceb317-4a27-4b8a-b2f8-3ff7aa4a0242, runId = f6fe77e1-cb64-4d41-938f- 
 db573380dc71]. Use hdfs://ip-10-0-1-20.ec2.internal:8020/tmp/temporary- 
 b6d69d12-26b4-4635-80bc-d923cf82dc23 to store the query checkpoint.
 18/12/14 07:15:11 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 = [localhost: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 =
    ssl.endpoint.identification.algorithm = null
    max.poll.records = 1
    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.ByteArrayDeserializer
    group.id = spark-kafka-source-4b2e17d1-f289-47be-ab4f-fce9fa1c1723- 
    -744805972-driver-0
    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.ByteArrayDeserializer
    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 = earliest

    18/12/14 07:15:11 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 = [localhost: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-2
    ssl.endpoint.identification.algorithm = null
    max.poll.records = 1
    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.ByteArrayDeserializer
    group.id = spark-kafka-source-4b2e17d1-f289-47be-ab4f-fce9fa1c1723- 
    -744805972-driver-0
    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.ByteArrayDeserializer
    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 = earliest

  INFO utils.AppInfoParser: Kafka version : 0.10.0-kafka-2.1.0
  INFO utils.AppInfoParser: Kafka commitId : unknown
  INFO streaming.MicroBatchExecution: Starting new streaming query.

  +-----++-----+
  |key  ||value|
  +-----++-----+
  +-----++-----+

1 个答案:

答案 0 :(得分:0)

我试图通过提交spark作业在Cluster上运行。可能是由于spark和kafka的版本不匹配消息没有被使用。

我在Windows本地设置了kafka,并且能够使用主题消息。