[英]java.lang.String cannot be cast to java.lang.Double Error when trying to return Map[(String, String),(Double, Double)] from RDD
I am trying to read a .txt file with |
我正在尝试使用
|
读取.txt文件|
delimiters as an RDD and trying return a Map[(String, String),(Double, Double)]
, however I am running into CastException 分隔符作为RDD并尝试返回
Map[(String, String),(Double, Double)]
,但是我遇到了CastException
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
input data looks like this 输入数据如下所示
string1|string2|100.00|200.00
string1|string2|34.98|0.989
this is how i am reading the file as rdd and parsing it 这就是我作为rdd读取文件并解析它的方式
val mydata = sc
.textFile("file")
.map(line => line.split("|"))
.map(row =>
((row(0), row(1)),
(row(2).asInstanceOf[Double], row(3).asInstanceOf[Double])))
.collect
.toMap
How can I fix this issue 我该如何解决这个问题
expected o/p: 预期输出:
Map[(String, String),(Double, Double)] = Map((string1,string2) -> (100.0,200.0), (string1,string2) -> (34.98,0.989))
To be on the safe side you can use trim
function and you can use collectAsMap
为了安全起见,您可以使用
trim
函数,也可以使用collectAsMap
val mydata = sc
.textFile("file")
.map(line => line.split("\\|"))
.map(row =>
((row(0), row(1)),
(row(2).trim.asInstanceOf[Double], row(3).trim.asInstanceOf[Double])))
.collectAsMap()
And to be more safe you can use Try/getOrElse
为了更加安全,您可以使用
Try/getOrElse
val mydata = sc
.textFile("file")
.map(line => line.split("\\|"))
.map(row =>
((row(0), row(1)),
(Try(row(2).trim.asInstanceOf[Double]).getOrElse(0.0), Try(row(3).trim.asInstanceOf[Double]).getOrElse(0.0))))
.collectAsMap()
Moreover you can use toDouble
instead of asInstanceOf[Double]
此外,您可以使用
toDouble
代替asInstanceOf[Double]
val mydata = sc
.textFile("file")
.map(line => line.split("\\|"))
.map(row =>
((row(0), row(1)),
(Try(row(2).trim.toDouble).getOrElse(0.0), Try(row(3).trim.toDouble).getOrElse(0.0)))
)
.collectAsMap().foreach(println)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.