一、数据准备,json文件 项目处新建people.json文件
{"name":"Michael"}
{"name":"Andy", "age":30}
{"name":"Justin", "age":19}
创建DataFrame
val df = spark.read.json("input/people.json")
二、DataFrame上的行动操作
行动操作有:show、collect、collectAsList、describe、first、 head、 take、 takeAsList。
这里只介绍show、collect这两种方式。
1、show(展示数据)
以表格的形式在输出中展示DF(DataFrame) 中的数据。
(1)show() 只显示前20行数据
df.show()
| age| name|
+----+-------+
|null|Michael|
| 30| Andy|
| 19| Justin|
(2) show(numRows: Int) 显示前Int行数据
df.show(1)
| age| name|
+----+-------+
|null|Michael|
2、collect(获取所有数据到数组)
collect方法会将DF中的所有数据都获取到, 并返回一个Array对象。
df.collect()