综合案例分析
现有数据集 department.json
与employee.json
,以部门名称和员工性别为粒度,试计算每个部门分性别平均年龄与平均薪资。 department.json
如下:
{"id":1,"name":"Tech Department"}
{"id":2,"name":"Fina Department"}
{"id":3,"name":"HR Department"}
employee.json
如下:
{"name":"zhangsan","age":26,"depId":1,"gender":"male","salary":20000}
{"name":"lisi","age":36,"depId":2,"gender":"female","salary":8500}
{"name":"wangwu","age":23,"depId":1,"gender":"male","salary":5000}
{"name":"zhaoliu","age":25,"depId":3,"gender":"male","salary":7000}
{"name":"marry","age":19,"depId":2,"gender":"female","salary":6600}
{"name":"Tom","age":36,"depId":1,"gender":"female","salary":5000}
{"name":"kitty","age":43,"depId":2,"gender":"female","salary":6000}
两份数据我们在演示的时候已经创建并上传至 hdfs
文件系统,用户在这里需要请自行创建。
执行命令:
root@foo2 cloudera]# cd /root/device-report/
[root@foo2 device-report]# ls
b.txt test.sql
[root@foo2 device-report]# vim department.json
[root@foo2 device-report]# vim employee.json
[root@foo2 device-report]# ls
b.txt department.json employee.json test.sql
[root@foo2 device-report]# chown hdfs:hdfs department.json
[root@foo2 device-report]# chown hdfs:hdfs employee.json
[root@foo2 device-report]# ls
b.txt department.json employee.json test.sql
[root@foo2 device-report]# ll
总用量 16
-rw-r--r-- 1 hdfs hdfs 22 8月 14 10:45 b.txt
-rw-r--r-- 1 hdfs hdfs 100 8月 17 16:50 department.json
-rw-r--r-- 1 hdfs hdfs 474 8月 17 16:53 employee.json [root@foo2 device-report]# su hdfs
[hdfs@foo2 device-report]$ clear
[hdfs@foo2 device-report]$ ls
b.txt test.sql
[hdfs@foo2 device-report]$ cd /var/lib/hadoop-h
hadoop-hdfs/ hadoop-httpfs/
[hdfs@foo2 device-report]$ cd /var/lib/hadoop-hdfs/device-report/
[hdfs@foo2 device-report]$ ls
b.txt department.json employee.json person.json test.sql
[hdfs@foo2 device-report]$ hadoop fs -put department.json /testdata
[hdfs@foo2 device-report]$ hadoop fs -put employee.json /testdata
[hdfs@foo2 device-report]$ hadoop fs -ls /testdata
Found 3 items
-rw-r--r-- 2 hdfs supergroup 100 2018-08-17 16:54 /testdata/department.json
-rw-r--r-- 2 hdfs supergroup 474 2018-08-17 16:55 /testdata/employee.json
-rw-r--r-- 2 hdfs supergroup 71 2018-08-17 16:39 /testdata/person.json
查看内容
[hdfs@foo2 device-report]$ hadoop fs -cat hdfs://192.168.0.106:8020/testdata/department.json
{"id":1,"name":"Tech Department"}
{"id":2,"name":"Fina Department"}
{"id":3,"name":"HR Department"}
[hdfs@foo2 device-report]$ hadoop fs -cat hdfs://192.168.0.106:8020/testdata/employee.json
{"name":"zhangsan","age":26,"depId":1,"gender":"male","salary":20000}
{"name":"lisi","age":36,"depId":2,"gender":"female","salary":8500}
{"name":"wangwu","age":23,"depId":1,"gender":"male","salary":5000}
{"name":"zhaoliu","age":25,"depId":3,"gender":"male","salary":7000}
{"name":"marry","age":19,"depId":2,"gender":"female","salary":6600}
{"name":"Tom","age":36,"depId":1,"gender":"female","salary":5000}
{"name":"kitty","age":43,"depId":2,"gender":"female","salary":6000}
-rw-r--r-- 1 hdfs hdfs 237 8月 14 16:49 test.sql
[root@foo2 device-report]# mv department.json /var/lib/hadoop-hdfs/device-report/
[root@foo2 device-report]# mv employee.json /var/lib/hadoop-hdfs/device-report/
scala> val emp = spark.read.json("hdfs://192.168.0.106:8020/testdata/employee.json")
emp: org.apache.spark.sql.DataFrame = [age: bigint, depId: bigint ... 3 more fields]
scala> val dep = spark.read.json("hdfs://192.168.0.106:8020/testdata/department.json")
dep: org.apache.spark.sql.DataFrame = [id: bigint, name: string]
变成视图:
scala> emp.createOrReplaceTempView("employee")
scala> dep.createOrReplaceTempView("department")
2). 用算子操作
# 注意:两个表的字段的连接条件,需要使用三个等号
emp.join(dep,$"id" === $"depId").groupBy(dep("name"),emp("gender")).agg(avg(emp("salary")),avg(emp("age"))).show()
结果:
+---------------+------+-----------------+------------------+
| name|gender| avg(salary)| avg(age)|
+---------------+------+-----------------+------------------+
|Tech Department| male| 12500.0| 24.5|
|Fina Department|female|7033.333333333333|32.666666666666664|
|Tech Department|female| 5000.0| 36.0|
| HR Department| male| 7000.0| 25.0|
+---------------+------+-----------------+------------------+
3). 用SQL操作
scala> spark.sql("select department.name,avg(employee.salary),avg(employee.age) from employee left join department on employee.depId=department.id group by department.name,employee.gender").show()
结果:
+-----------------+---------------------+----------------------+
| name| avg(salary)| avg(age)|
+-----------------+---------------------+----------------------+
|Tech Department | 12500.0| 24.5|
|Fina Department |7033.333333333333|32.666666666666664|
|Tech Department | 5000.0| 36.0|
| HR Department | 7000.0| 25.0|
+-----------------+---------------------+----------------------+
2、3结果都是一样一样的。
- Spark源码系列(九)Spark SQL初体验之解析过程详解
好久没更新博客了,之前学了一些R语言和机器学习的内容,做了一些笔记,之后也会放到博客上面来给大家共享.一个月前就打算更新Spark Sql的内容了,因为一些别的事情耽误了,今天就简单写点,Spark1 ...
- Spark1.0新特性-->Spark SQL
Spark1.0出来了,变化还是挺大的,文档比以前齐全了,RDD支持的操作比以前多了一些,Spark on yarn功能我居然跑通了.但是最最重要的就是多了一个Spark SQL的功能,它能对RDD进 ...
- Spark SQL概念学习系列之Spark SQL 优化策略(五)
查询优化是传统数据库中最为重要的一环,这项技术在传统数据库中已经很成熟.除了查询优化, Spark SQL 在存储上也进行了优化,从以下几点查看 Spark SQL 的一些优化策略. (1)内存列式存 ...
- Spark SQL 用户自定义函数UDF、用户自定义聚合函数UDAF 教程(Java踩坑教学版)
在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_date等 UDAF( ...
- 8.Spark SQL
Spark SQL 1 Why Apache Spark 2 关于Apache Spark 3 如何安装Apache Spark 4 Apache Spark的工作原理 5 spark弹性分布式数据集 ...
- Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets Guide | ApacheCN
Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...
- Spark sql ---JSON
介绍Spark SQL的JSON支持,这是我们在Databricks中开发的一个功能,可以在Spark中更容易查询和创建JSON数据.随着网络和移动应用程序的普及,JSON已经成为Web服务API以及 ...
- Spark官方1 ---------Spark SQL和DataFrame指南(1.5.0)
概述 Spark SQL是用于结构化数据处理的Spark模块.它提供了一个称为DataFrames的编程抽象,也可以作为分布式SQL查询引擎. Spark SQL也可用于从现有的Hive安装中读取数据 ...
- 深入研究Spark SQL的Catalyst优化器(原创翻译)
Spark SQL是Spark最新和技术最为复杂的组件之一.它支持SQL查询和新的DataFrame API.Spark SQL的核心是Catalyst优化器,它以一种新颖的方式利用高级编程语言特性( ...
随机推荐
- 温度转换-python
#接收一段字符串 TempStr = input("请输入带有符号的温度值:") #如果 TS 最后一位是大写或是小写 f if TempStr[-1] in ['F','f']: ...
- es6学习日记5-对象的扩展
属性的简洁表示法 ES6 允许直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = {foo}; baz // {foo: &q ...
- Parsing Natural Scenes and Natural Language with Recursive Neural Networks-paper
Parsing Natural Scenes and Natural Language with Recursive Neural Networks作者信息: Richard Socher richa ...
- Coprime Sequence (HDU 6025)前缀和与后缀和的应用
题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...
- PTA——组合数
PTA 7-48 求组合数 #include<stdio.h> double fact(int n); int main() { int m,n; int c; scanf("% ...
- linux的基本操作2
/dev/ha[a-d] IDE硬盘(过时了)/dev/sd[a-p] U盘,scsi,sata,ssd硬盘(流行)/dev/cdrom 光盘 CD-ROM/dev/mouse 鼠标 fdisk ...
- 在windows上使用ssh秘钥连接git服务器
git部署在centos7上 安装好git后,新建一个用户test(注意要加入git用户组)配置ssh秘钥登录,我的另一篇博客有写配置步骤 重点的地方是在windows系统上使用秘钥登录git服务器 ...
- spring IOC 和AOP 方面
spring 的2大核心 是Ioc 和 aop spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中 IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...
- lvs基础及部署
LVS简介 LVS--Linux Vritual Server 即linux虚拟服务器,1998年5月由章文嵩博士开发并开源,目前全球多个国家的企业单位都在使用LVS构建集群服务. LVS可实 ...
- edit this cookie chrome插件 (HttpAnalyzerStdV3 获取Cookie 后,再用edit this cookie添加cookie)
插件下载地址及安装方法:http://www.bkill.com/download/154494.html 博客提供下载地址:https://files.cnblogs.com/files/Fooo/ ...