【问题标题】:Unable to find encoder for type stored in a Dataset for streaming mongo db data through Kafka无法找到存储在数据集中用于通过 Kafka 流式传输 mongo db 数据的类型的编码器
【发布时间】:2018-10-28 00:03:36
【问题描述】:

我想跟踪 Mongo oplog 并通过 Kafka 流式传输它。因此,我发现了 debezium Kafka CDC 连接器,它使用内置序列化技术跟踪 Mongo oplog。

Schema 注册表使用下面的转换器进行序列化,

'key.converter=io.confluent.connect.avro.AvroConverter' and
'value.converter=io.confluent.connect.avro.AvroConverter'

下面是我在项目中使用的库依赖

libraryDependencies += "io.confluent" % "kafka-avro-serializer" % "3.1.2"

libraryDependencies += "org.apache.kafka" % "kafka-streams" % "0.10.2.0

下面是反序列化 Avro 数据的流代码

import org.apache.spark.sql.{Dataset, SparkSession}
import io.confluent.kafka.schemaregistry.client.rest.RestService
import io.confluent.kafka.serializers.KafkaAvroDeserializer
import org.apache.avro.Schema

import scala.collection.JavaConverters._

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

    val sparkSession = SparkSession
      .builder
      .master("local")
      .appName("kafka")
      .getOrCreate()
    sparkSession.sparkContext.setLogLevel("ERROR")

    import sparkSession.implicits._

    case class DeserializedFromKafkaRecord(key: String, value: String)

    val schemaRegistryURL = "http://127.0.0.1:8081"

    val topicName = "productCollection.inventory.Product"
    val subjectValueName = topicName + "-value"

    //create RestService object
    val restService = new RestService(schemaRegistryURL)

    //.getLatestVersion returns io.confluent.kafka.schemaregistry.client.rest.entities.Schema object.
    val valueRestResponseSchema = restService.getLatestVersion(subjectValueName)

    //Use Avro parsing classes to get Avro Schema
    val parser = new Schema.Parser
    val topicValueAvroSchema: Schema = parser.parse(valueRestResponseSchema.getSchema)

    //key schema is typically just string but you can do the same process for the key as the value
    val keySchemaString = "\"string\""
    val keySchema = parser.parse(keySchemaString)

    //Create a map with the Schema registry url.
    //This is the only Required configuration for Confluent's KafkaAvroDeserializer.
    val props = Map("schema.registry.url" -> schemaRegistryURL)

    //Declare SerDe vars before using Spark structured streaming map. Avoids non serializable class exception.
    var keyDeserializer: KafkaAvroDeserializer = null
    var valueDeserializer: KafkaAvroDeserializer = null

    //Create structured streaming DF to read from the topic.
    val rawTopicMessageDF = sparkSession.readStream
      .format("kafka")
      .option("kafka.bootstrap.servers", "127.0.0.1:9092")
      .option("subscribe", topicName)
      .option("startingOffsets", "earliest")
      .option("maxOffsetsPerTrigger", 20)  //remove for prod
      .load()
    rawTopicMessageDF.printSchema()

    //instantiate the SerDe classes if not already, then deserialize!
    val deserializedTopicMessageDS = rawTopicMessageDF.map{
      row =>
        if (keyDeserializer == null) {
          keyDeserializer = new KafkaAvroDeserializer
          keyDeserializer.configure(props.asJava, true)  //isKey = true
        }
        if (valueDeserializer == null) {
          valueDeserializer = new KafkaAvroDeserializer
          valueDeserializer.configure(props.asJava, false) //isKey = false
        }

        //Pass the Avro schema.
        val deserializedKeyString = keyDeserializer.deserialize(topicName, row.getAs[Array[Byte]]("key"), keySchema).toString //topic name is actually unused in the source code, just required by the signature. Weird right?
        val deserializedValueJsonString = valueDeserializer.deserialize(topicName, row.getAs[Array[Byte]]("value"), topicValueAvroSchema).toString

        DeserializedFromKafkaRecord(deserializedKeyString, deserializedValueJsonString)
    }

    val deserializedDSOutputStream = deserializedTopicMessageDS.writeStream
      .outputMode("append")
      .format("console")
      .option("truncate", false)
      .start()

Kafka 消费者运行良好,我可以看到来自 oplog 的数据拖尾,但是当我运行上面的代码时,我遇到了错误,

Error:(70, 59) Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._  Support for serializing other types will be added in future releases.
    val deserializedTopicMessageDS = rawTopicMessageDF.map{

Error:(70, 59) not enough arguments for method map: (implicit evidence$7: org.apache.spark.sql.Encoder[DeserializedFromKafkaRecord])org.apache.spark.sql.Dataset[DeserializedFromKafkaRecord].
Unspecified value parameter evidence$7.
    val deserializedTopicMessageDS = rawTopicMessageDF.map{

请建议我在这里缺少什么。

提前致谢。

【问题讨论】:

  • 您是否尝试过导入spark.implicits._,正如错误消息所暗示的(Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._)?
  • @norbjd,我已经导入了 sparkSession.implicits._
  • 哎呀,在阅读您的示例时错过了那行......无论如何,在下面找到我的答案,它应该可以解决您的问题。

标签: scala apache-spark apache-kafka spark-structured-streaming


【解决方案1】:

只需在 main 方法之外声明您的案例类 DeserializedFromKafkaRecord

我想当 case 类在 main 中定义时,带有隐式编码器的 Spark 魔法无法正常工作,因为在执行 main 方法之前 case class 不存在。

这个问题可以用一个更简单的例子来重现(没有 Kafka):

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

object SimpleTest {

  // declare CaseClass outside of main method
  case class CaseClass(value: Int)

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

    // when case class is declared here instead
    // of outside main, the program does not compile
    // case class CaseClass(value: Int)

    val sparkSession = SparkSession
      .builder
      .master("local")
      .appName("simpletest")
      .getOrCreate()

    import sparkSession.implicits._

    val df: DataFrame = sparkSession.sparkContext.parallelize(1 to 10).toDF()
    val ds: Dataset[CaseClass] = df.map { row =>
      CaseClass(row.getInt(0))
    }

    ds.show()
  }
}

【讨论】:

  • 在 main 之外声明案例类后错误消失了。 deserializedTopicMessageDS 似乎得到了正确的反序列化(通过打印模式),但是即使我在控制台上开始流并且没有打印任何内容,spark 会话也会停止。
  • Aslo,我注意到的另一件事是,在使用消费者配置时,它正在将“class org.apache.kafka.common.serialization.ByteArrayDeserializer”用于键和值反序列化器,尽管我已经提供io.confluent.connect.avro.AvroConverter 作为模式注册表中的键和值转换器。请提出建议。
  • @Ganesh 如果您需要有关该问题的帮助,我建议您为此问题创建一个新帖子以获得更多可见性,因为它与初始主题 Unable to find encoder 无关。如果我的回答解决了您丢失的编码器问题,请不要忘记接受答案,以帮助面临相同情况的其他人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2019-11-14
  • 2016-12-04
  • 2018-08-13
相关资源
最近更新 更多