|
| 1 | +# DSS用户测试样例2:Hive |
| 2 | + |
| 3 | +DSS用户测试样例的目的是为平台新用户提供一组测试样例,用于熟悉DSS的常见操作,并验证DSS平台的正确性 |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## 2.1 数仓建表 |
| 8 | + |
| 9 | + 进入“数据库”页面,点击“+”,依次输入表信息、表结构和分区信息即可创建数据库表: |
| 10 | + |
| 11 | +<img src="../../../images/zh_CN/chapter3/tests/hive1.png" alt="image-20200408212604929" style="zoom:50%;" /> |
| 12 | + |
| 13 | +  |
| 14 | + |
| 15 | + 通过以上流程,分别创建部门表dept、员工表emp和分区员工表emp_partition,建表语句如下: |
| 16 | + |
| 17 | +```sql |
| 18 | +create external table if not exists default.dept( |
| 19 | + deptno int, |
| 20 | + dname string, |
| 21 | + loc int |
| 22 | +) |
| 23 | +row format delimited fields terminated by '\t'; |
| 24 | + |
| 25 | +create external table if not exists default.emp( |
| 26 | + empno int, |
| 27 | + ename string, |
| 28 | + job string, |
| 29 | + mgr int, |
| 30 | + hiredate string, |
| 31 | + sal double, |
| 32 | + comm double, |
| 33 | + deptno int |
| 34 | +) |
| 35 | +row format delimited fields terminated by '\t'; |
| 36 | + |
| 37 | +create table if not exists emp_partition( |
| 38 | + empno int, |
| 39 | + ename string, |
| 40 | + job string, |
| 41 | + mgr int, |
| 42 | + hiredate string, |
| 43 | + sal double, |
| 44 | + comm double, |
| 45 | + deptno int |
| 46 | +) |
| 47 | +partitioned by (month string) |
| 48 | +row format delimited fields terminated by '\t'; |
| 49 | +``` |
| 50 | + |
| 51 | +**导入数据** |
| 52 | + |
| 53 | +目前需要通过后台手动批量导入数据,可以通过insert方法从页面插入数据 |
| 54 | + |
| 55 | +```sql |
| 56 | +load data local inpath 'dept.txt' into table default.dept; |
| 57 | +load data local inpath 'emp.txt' into table default.emp; |
| 58 | +load data local inpath 'emp1.txt' into table default.emp_partition; |
| 59 | +load data local inpath 'emp2.txt' into table default.emp_partition; |
| 60 | +load data local inpath 'emp2.txt' into table default.emp_partition; |
| 61 | +``` |
| 62 | + |
| 63 | +其它数据按照上述语句导入,样例数据文件路径在:`examples\ch3` |
| 64 | + |
| 65 | +## 2.2 基本SQL语法测试 |
| 66 | + |
| 67 | +### 2.2.1 简单查询 |
| 68 | + |
| 69 | +```sql |
| 70 | +select * from dept; |
| 71 | +``` |
| 72 | + |
| 73 | +### 2.2.2 Join连接 |
| 74 | + |
| 75 | +```sql |
| 76 | +select * from emp |
| 77 | +left join dept |
| 78 | +on emp.deptno = dept.deptno; |
| 79 | +``` |
| 80 | + |
| 81 | +### 2.2.3 聚合函数 |
| 82 | + |
| 83 | +```sql |
| 84 | +select dept.dname, avg(sal) as avg_salary |
| 85 | +from emp left join dept |
| 86 | +on emp.deptno = dept.deptno |
| 87 | +group by dept.dname; |
| 88 | +``` |
| 89 | + |
| 90 | +### 2.2.4 内置函数 |
| 91 | + |
| 92 | +```sql |
| 93 | +select ename, job,sal, |
| 94 | +rank() over(partition by job order by sal desc) sal_rank |
| 95 | +from emp; |
| 96 | +``` |
| 97 | + |
| 98 | +### 2.2.5 分区表简单查询 |
| 99 | + |
| 100 | +```sql |
| 101 | +show partitions emp_partition; |
| 102 | +select * from emp_partition where month='202001'; |
| 103 | +``` |
| 104 | + |
| 105 | +### 2.2.6 分区表联合查询 |
| 106 | + |
| 107 | +```sql |
| 108 | +select * from emp_partition where month='202001' |
| 109 | +union |
| 110 | +select * from emp_partition where month='202002' |
| 111 | +union |
| 112 | +select * from emp_partition where month='202003' |
| 113 | +``` |
| 114 | + |
| 115 | +## 2.3 UDF函数测试 |
| 116 | + |
| 117 | +### 2.3.1 Jar包上传 |
| 118 | + |
| 119 | +进入Scriptis页面后,右键目录路径上传jar包: |
| 120 | + |
| 121 | +  |
| 122 | + |
| 123 | +测试样例jar包在`examples\ch3\rename.jar` |
| 124 | + |
| 125 | +### 4.3.2 自定义函数 |
| 126 | + |
| 127 | +进入“UDF函数”选项(如1),右击“个人函数”目录,选择“新增函数”: |
| 128 | + |
| 129 | +<img src="../../../images/zh_CN/chapter3/tests/hive4.png" alt="image-20200408214033801" style="zoom: 50%;" /> |
| 130 | + |
| 131 | +输入函数名称、选择jar包、并填写注册格式、输入输出格式即可创建函数: |
| 132 | + |
| 133 | +  |
| 134 | + |
| 135 | +<img src="../../../images/zh_CN/chapter3/tests/hive-6.png" alt="image-20200409155418424" style="zoom: 67%;" /> |
| 136 | + |
| 137 | +获得的函数如下: |
| 138 | + |
| 139 | +  |
| 140 | + |
| 141 | +### 4.3.3 利用自定义函数进行SQL查询 |
| 142 | + |
| 143 | +完成函数注册后,可进入工作空间页面创建.hql文件使用函数: |
| 144 | + |
| 145 | +```sql |
| 146 | +select deptno,ename, rename(ename) as new_name |
| 147 | +from emp; |
| 148 | +``` |
0 commit comments