1、数据准备
people.json
{ "id": 1, "name": "张三", "age": 38 } { "id": 2, "name": "李四", "age": 30 } { "id": 3, "name": "王五", "age": 28 }
2、DataFrame
读取json文件创建DataFrame
val df = spark.read.json("/tmp/work/people.json")
展示结果:df.show
3. SQL风格语法
1)创建一个DataFrame
scala> val df = spark.read.json("/tmp/work/people.json")
2)对DataFrame创建一个临时表
df.createOrReplaceTempView("people")
3)通过SQL语句查询
val sqlDF = spark.sql("select * from people")
4)显示结果
sqlDF.show
临时表是Session范围的,Session退出后,表就失效了。如果想在应用范围内使用,可以使用全局表。全局表需要全路径访问,例如global_temp.people
5) 对于DataFrame创建一个全局表
df.createGlobalTempView("people")
6)通过SQL语句查询全表
spark.sql("select * from global_temp.people").show()
spark.newSession().sql("select * from global_temp.people").show()
标签:people,df,编程,DataFrame,json,show,Spark,spark From: https://www.cnblogs.com/linlf03/p/17266144.html