0

I am using Structured Streaming (Spark 2.4.0) to read avro mesages through kafka and using Confluent schema-Registry to receive/read schema

I am unable to access the deeply nested fields.

Schema looks like this in compacted avsc format:

{"type":"record","name":"KafkaMessage","namespace":"avro.pojo","fields":[{"name":"context","type":["null",{"type":"record","name":"Context","fields":[{"name":"businessInteractionId","type":["null","string"]},{"name":"referenceNumber","type":["null","string"]},{"name":"serviceName","type":["null","string"]},{"name":"status","type":["null","string"]},{"name":"sourceSystems","type":["null",{"type":"array","items":{"type":"record","name":"SourceSystem","fields":[{"name":"orderId","type":["null","string"]},{"name":"revisionNumber","type":["null","string"]},{"name":"systemId","type":["null","string"]}]}}]},{"name":"sysDate","type":["null","string"]}]}]}]}

as parsed in spark

      context
     |-- businessInteractionId: string (nullable = true)
     |-- referenceNumber: string (nullable = true)
     |-- serviceName: string (nullable = true)
     |-- sourceSystems: array (nullable = true)
     |    |-- element: struct (containsNull = true)
     |    |    |-- orderId: string (nullable = true)
     |    |    |-- revisionNumber: string (nullable = true)
     |    |    |-- systemId: string (nullable = true)
     |-- status: string (nullable = true)
     |-- sysDate: string (nullable = true)

My approach : Cast the returned Object as a GenericRecord and array as GenericData.Array[GenericRecord] Link

Code

val client = new CachedSchemaRegistryClient(schemaRegUrl, 100)
val brdDeser = spark.sparkContext.broadcast(new KafkaAvroDeserializer(client).asInstanceOf[Deserializer[GenericRecord]])

    val results = df.select(col("value").as[Array[Byte]]).map {
      rawBytes: Array[Byte] =>
        //read the raw bytes from spark and then use the confluent deserializer to get the record back
        val deser = brdDeser.value
        val decoded = deser.deserialize(topics, rawBytes)
        val context_GR =
          decoded.get("context").asInstanceOf[GenericRecord]
        val c_businessInteractionId =
          context_GR.get("businessInteractionId").toString  //this works
        val c1_sourceSystems =
          context_GR
            .get("sourceSystems")
            .asInstanceOf[GenericData.Array[GenericRecord]]
        val c_orderId = c1_sourceSystems.get(0).get("orderId").toString   //NullPointerException
        val c_revisionNumber = c1_sourceSystems.get(0).get("revisionNumber").toString
        val c_systemId = c1_sourceSystems.get(0).get("systemId").toString
new CaseMessage(
          c_businessInteractionId, c_orderId, c_revisionNumber, c_systemId )
    }

case class CaseMessage(c_businessInteractionId: String,
                         c_orderId: String,
                         c_revisionNumber: String,
                         c_systemId: String,)

Each time i receive a java.lang.NullPointerException when it is trying to evaluate c_orderId

unchained
  • 19
  • 6

1 Answers1

0

This was a data issue. I was able to resolve this by doing a null value check

val c_orderId = if (c1_sourceSystems.get(0).get("orderId") != null) {
          c1_sourceSystems.get(0).get("orderId").toString
unchained
  • 19
  • 6