-- 默认情况下,SparkContext对象在spark-shell启动时用namesc初始化。使用以下命令创建SQLContext。
val sqlcontext = new org.apache.spark.sql.SQLContext(sc)
-- employee.json-将此文件放在currentscala>指针所在的目录中。
{
   {"id" : "1201", "name" : "satish", "age" : "25"}
   {"id" : "1202", "name" : "krishna", "age" : "28"}
   {"id" : "1203", "name" : "amith", "age" : "39"}
   {"id" : "1204", "name" : "javed", "age" : "23"}
   {"id" : "1205", "name" : "prudvi", "age" : "23"}
}
-- 读取JSON文档namedemployee.json。 数据显示为带有字段id,name和age的表。
val dfs = sqlContext.read.json("/root/wangbin/employee.json")
-- 显示数据
dfs.show()
-- 查看数据结构
dfs.printSchema()
-- 查看某一列
dfs.select("name").show()
-- 查找年龄大于23(age> 23)的雇员。
dfs.filter(dfs("age") > 23).show()
-- 计算同一年龄的员工人数。
dfs.groupBy("age").count().show()

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-03-06
  • 2022-02-02
  • 2022-02-12
  • 2021-05-02
猜你喜欢
  • 2022-12-23
  • 2021-11-30
  • 2021-08-02
  • 2022-01-08
  • 2021-08-27
  • 2019-12-17
  • 2021-06-29
相关资源
相似解决方案