есть hashMap parsedRecord, тип которого scala.collection.immutable.HashMap , способ получения типа:
logger.info(s"parsedRecord: ${parsedRecord}")
logger.info(s"parsedRecord type: ${parsedRecord.getClass}")
есть еще одна карта, которую мы получили бы, это задание с потоковым воспроизведением, запись из кластера kafka, коды, как показано ниже:
val finalRecord = (record: Map[String, Any]) => {
record.get("operation").get.toString match {
case "view" => parsedRecord + ("view" -> 1, "impression" -> 0, "click" -> 0)
case "impression" => parsedRecord + ("view" -> 0, "impression" -> 1, "click" -> 0)
case "click" => parsedRecord + ("view" -> 0, "impression" -> 0, "click" -> 1)
}
}
recordList += finalRecord.asInstanceOf[scala.collection.immutable.HashMap[String, Any]]
этот код я бы получил finalRecord, тип которого map, но есть исключение, подобное этому:
Caused by: java.lang.ClassCastException: com.shopee.mall.data.ParseOperation$$anonfun$9 cannot be cast to scala.collection.immutable.HashMap
at com.shopee.mall.data.ParseOperation$.parseRecord(ParseOperation.scala:88)
at com.shopee.mall.data.ParseOperation$$anonfun$2.apply(ParseOperation.scala:14)
at com.shopee.mall.data.ParseOperation$$anonfun$2.apply(ParseOperation.scala:12)
at com.shopee.mall.data.ParseOperation$$anonfun$10.apply(ParseOperation.scala:105)
at com.shopee.mall.data.ParseOperation$$anonfun$10.apply(ParseOperation.scala:102)
at com.shopee.mall.data.OfficialMallTracker$$anonfun$4.apply(OfficialMallTracker.scala:70)
at com.shopee.mall.data.OfficialMallTracker$$anonfun$4.apply(OfficialMallTracker.scala:70)
at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
at scala.collection.AbstractIterator.to(Iterator.scala:1336)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:936)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:936)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:322)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)