我是Spark的新手,我想将数据帧转换为配对的RDD。我的DataFrame看起来像:
tagname,value,Minute
tag1,13.87,5
tag2,32.50,10
tag3,35.00,5
tag1,10.98,2
tag5,11.0,5
我想要PairedRDD(标记名,值)。我试过了
val byKey:Map[String,Long] = winowFiveRDD.map({case (tagname,value) => (tagname)->value})
我收到以下错误:
error: constructor cannot be instantiated to expected type
非常感谢帮助。在此先感谢。
答案 0 :(得分:1)
org.apache.spark.sql.Row
针对某些数据类型具有自定义get
方法。
val df = sc.parallelize(List(
("tag1",13.87,5),
("tag2",32.50,10),
("tag3",35.00,5),
("tag1",10.98,2),
("tag5",11.0,5)
)).toDF("tagname", "value", "minute")
val pairedRDD = df.map(x => (x.getString(0), x.getDouble(1) ) )
pairedRDD.collect
Array[(String, Double)] = Array((tag1,13.87), (tag2,32.5), (tag3,35.0), (tag1,10.98), (tag5,11.0))
然后,您可以调用pairedRDD.collect.toMap
将其转换为Scala地图。问题中有两个名为tag1
的密钥,这是不正确的。
答案 1 :(得分:0)
从RDD.SCALA,地图返回MapPartitionsRDD。你不能直接把它贴到地图上。只需删除" Map [String,Long]"没关系。
答案 2 :(得分:0)
我会使用Dataset.as
:
import org.apache.spark.rdd.RDD
val df = Seq(
("tag1", "13.87", "5"), ("tag2", "32.50", "10"), ("tag3", "35.00", "5"),
("tag1", "10.98", "2"), ("tag5", "11.0", "5")
).toDF("tagname", "value", "minute")
val pairedRDD: RDD[(String, Double)] = df
.select($"tagname", $"value".cast("double"))
.as[(String, Double)].rdd