查询
数据仓库是典型的查多写少,这个是根据HDFS本身的特点来的。HDFS本身不支持随机修改,只支持追加。所以Hive读多改少,写就写一遍,重点在查询。
查询的用处在方方面面都有,比如insert等,都需要查询。
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select
查询语句语法:
[WITH CommonTableExpression (, CommonTableExpression)*] (Note: Only available
starting with Hive 0.13.0)
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[ORDER BY col_list]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT number]
1、基本查询(Select…From)
1. 全表和特定列查询
创建部门表
hive (default)> create table if not exists dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by '\t';
创建员工表
hive (default)> create table if not exists emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by '\t';
导入数据
hive (default)> load data local inpath '/export/servers/datas/dept.txt' into table dept;
hive (default)> load data local inpath '/export/servers/datas/emp.txt' into table emp;
1).全表查询
hive (default)> select * from emp;
2).选择特定列查询
hive (default)> select empno, ename from emp;
注意:
(1)SQL 语言大小写不敏感。
(2)SQL 可以写在一行或者多行
(3)关键字不能被缩写也不能分行
(4)各子句一般要分行写。
(5)使用缩进提高语句的可读性。