Scala Spark取出DataFrame中列中的值

Scala Spark取出DataFrame中列中的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
scala> val df = Seq(
| ("one", 2.0),
| ("two", 1.5),
| ("three", 8.0)
| ).toDF("id", "val")
df: org.apache.spark.sql.DataFrame = [id: string, val: double]

scala> df.show()
+-----+---+
| id|val|
+-----+---+
| one|2.0|
| two|1.5|
|three|8.0|
+-----+---+


scala> df.select("id").collect().map(_(0)).toList
res6: List[Any] = List(one, two, three)

scala> df.select("id").rdd.map(_(0)).collect.toList
res7: List[Any] = List(one, two, three)

scala> df.select("id").map(_.getString(0)).collect.toList
res8: List[String] = List(one, two, three)
------本文结束,欢迎收藏本站、分享、评论或联系作者!------
赠人玫瑰 手有余香
0%