How to omit a null value exception in flink-kafka

2020-03-31 02:52发布

问题:

I'm trying to make a code that creates alert when temperature is above threshold temperature (as defined in the code), but keyed stream is creating problem. I'm new to flink and intermediate in scala. I need help in this code

I've tried almost everything

 def main(args: Array[String]): Unit = {
    val TEMPERATURE_THRESHOLD: Double = 50.00

    val see: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "localhost:9092")
    properties.setProperty("zookeeper.connect", "localhost:2181")

    val src = see.addSource(new FlinkKafkaConsumer010("broadcast",
      new JSONKeyValueDeserializationSchema(true), properties))


    var data = src.map { v => {
      val loc = v.get("locationID").asInstanceOf[String]
      val temperature = v.get("temp").asDouble()
    //val json = Map("locationID" -> locationID, "temp" -> v.temp)
    //val jsonVal = JSONObject(json).toString()
      (loc, temperature)
    }}
 data = data
      .keyBy(
        v => v._1
      )
{"locationID": "ASK", "temp": 35} {"locationID": "BC", "temp": 45} {"locationID": "CHD", "temp": 55} {"locationID": "RAJ", "temp": 65} {"locationID": "EGY", "temp": 55}

I think my tuple is taking null value from somewhere , I need help omit that error

Error:-

Exception in thread "main" org.apache.flink.runtime.client.JobExecutionException: Job execution failed.
...

    at flinkBroadcast1$.main(flinkBroadcast1.scala:59)
    at flinkBroadcast1.main(flinkBroadcast1.scala)
Caused by: java.lang.Exception: org.apache.flink.streaming.runtime.tasks.ExceptionInChainedOperatorException: Could not forward element to next operator
...
Caused by: org.apache.flink.streaming.runtime.tasks.ExceptionInChainedOperatorException: Could not forward element to next operator
...
Caused by: java.lang.NullPointerException