测试数据:
{"vin": "LW433B109L1021956","receiveTime": 1655777399240,"signals": [{"sigName": "BMS_SensorTemp1","sigVal": 0,"collectTime": 1655777397224,"validStatus": 1},{"sigName": "BMS_SensorTemp2","sigVal": 0,"collectTime": 1655777397224,"validStatus": 1},{"sigName": "BMS_SensorTemp3","sigVal": 0,"collectTime": 1655777397224,"validStatus": 1},{"sigName": "BMS_SensorTemp4","sigVal": 0,"collectTime": 1655777397224,"validStatus": 1}],"location": null}
代码:
signalSourceDs.flatMap(new FlatMapFunction[String,SignalInputBean] {
override def flatMap(value: String, out: Collector[SignalInputBean]): Unit = {
val valueObj: JSONObject = JSON.parseObject(value)
val vin: String = valueObj.getString("vin")
var sigName: String = ""
var sigVal: Double = 0D
var collectTime: Long = 0L
var validStatus: String = ""
val signalsArr = valueObj.getJSONArray("signals")
if(signalsArr != null&&signalsArr.size()>0){
for (i<-0 until signalsArr.size()){
val signalObj: JSONObject = signalsArr.getJSONObject(i)
signalObj.put("vin",vin)
sigName = signalObj.getString("sigName")
sigVal = signalObj.getDoubleValue("sigVal")
collectTime = signalObj.getLong("collectTime")
validStatus = signalObj.getString("validStatus")
out.collect(SignalInputBean(vin,sigName,sigVal,collectTime,validStatus))
}
}
}
}).print()