繁体   English   中英

在Scala / Spark中将String转换为Double?

[英]Convert String to Double in Scala / Spark?

我有JSON数据集,其中包含像“USD 5.00”这样的字符串中的价格。 我想将数字部分转换为Double以在MLLIB LabeledPoint中使用,并设法将价格字符串拆分为字符串数组。 下面创建一个具有正确结构的数据集:

import org.apache.spark.mllib.linalg.{Vector,Vectors}
import org.apache.spark.mllib.regression.LabeledPoint


case class Obs(f1: Double, f2: Double, price: Array[String])

val obs1 = new Obs(1,2,Array("USD", "5.00"))
val obs2 = new Obs(2,1,Array("USD", "3.00"))

val df = sc.parallelize(Seq(obs1,obs2)).toDF()
df.printSchema
df.show()

val labeled = df.map(row => LabeledPoint(row.get(2).asInstanceOf[Array[String]].apply(1).toDouble, Vectors.dense(row.getDouble(0), row.getDouble(1))))

labeled.take(2).foreach(println)

输出如下:

df: org.apache.spark.sql.DataFrame = [f1: double, f2: double, price: array<string>]
root
 |-- f1: double (nullable = false)
 |-- f2: double (nullable = false)
 |-- price: array (nullable = true)
 |    |-- element: string (containsNull = true)

+---+---+-----------+
| f1| f2|      price|
+---+---+-----------+
|1.0|2.0|[USD, 5.00]|
|2.0|1.0|[USD, 3.00]|
+---+---+-----------+

但最后我得到一个ClassCastException:

java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Ljava.lang.String;

我认为ClassCastException是由println引起的。 但我没想到; 我该如何处理这种情况?

潜在的重复解决了我的问题的一部分(谢谢),但是“在数据框中促进结构元素的更深层次的问题仍然存在”。 我会让mods确定这是否真的是一个骗局。

我觉得问题在这里:

.asInstanceOf[Array[String]]

让我提出一个替代解决方案,我认为比使用所有asInstanceOfasInstanceOf

import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.sql.Row

val assembler = new VectorAssembler()
  .setInputCols(Array("f1", "f2"))
  .setOutputCol("features")

val labeled = assembler.transform(df)
  .select($"price".getItem(1).cast("double"), $"features")
  .map{case Row(price: Double, features: Vector) => 
    LabeledPoint(price, features)}

关于您的问题ArrayType作为WrappedArray存储在Row ,因此您看到错误。 你可以使用

import scala.collection.mutable.WrappedArray

row.getAs[WrappedArray[String]](2)

或者干脆

row.getAs[Seq[String]](2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM