Reputation: 1027
there is a hashMap parsedRecord which type is scala.collection.immutable.HashMap,the way I get the type is:
logger.info(s"parsedRecord: ${parsedRecord}")
logger.info(s"parsedRecord type: ${parsedRecord.getClass}")
there is another map we would get, this is a spark streaming job, record is from kafka cluster, the codes like below:
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]]
these code I would get the finalRecord whose type is map, But there is an exception like this:
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)
Upvotes: 0
Views: 41
Reputation: 170713
these code I would get the finalRecord whose type is map
No, you defined finalRecord
to be a function which accepts and returns a map. To get a map, you need to apply it to some map, e.g.
recordList += finalRecord(parsedRecord)
You should try to avoid asInstanceOf
unless necessary. E.g. if you need it because recordList
is defined to contain HashMap
s, consider changing it to contain Map
s instead.
Upvotes: 1