Hive表數據的導入
- 從本地導入
load data local inpath 'local_path' into table table_name;
- 從hdfs導入
load data inpath 'hdfs_path' into table table_name;
- 覆蓋的方式導入
load data local inpath 'local_inpath' overwrite into table table_name;
load data inpath 'hdfs_inpath' overwrite into table table_name;
-
創建表時通過子查詢加載(會自動創建表結構)
hive7.png - insert加載子查詢到已創建好的表中(手動創建表結構)
hive (test_db)> create table emp3 like emp;
hive (test_db)> insert into table emp3 select * from emp;
- 在創建表時location 指定文件位置(外部表)
create table ... location 'hdfs_location';
- 分區表加載數據
load data [local] inpath 'paht' into table table_name partition(partioncol1=val1...);
Hive查詢結果和表數據的導出
-
導出到本地文件(目錄數據會被覆蓋)
hive9.png -
導出到hdfs(目錄數據會被覆蓋,如果這個目錄不存在則會自動創建)
hive11.png -
用hive的-e、-f命令導出(不會覆蓋數據)
hive10.png - 利用sqoop導入導出
排序
- order by:order by子句對一列按升序(asc)或降序(desc)排列。但是只能限制于一個reduce。
hive (test_db)> select sal from emp order by sal desc;
- sort by:對每個reduce中的數據進行單獨排序,但是全局不一定有序。
hive (test_db)> set mapreduce.job.reduces=3;
hive (test_db)> select sal,deptno from emp sort by sal desc;
- distribute by:shuffle中的分區默認是按照key值計算hash值然后取余均勻的分發到reducer中。distribute by可以設置map端輸出后是按照哪一個字段進行hash取余分區的。
hive (test_db)> select sal,deptno from emp distribute by deptno sort by sal desc;
- cluster by:當dstribute by和sort by字段相同時,可以使用cluster by,相當于一種簡寫方式。
分組
- group by和having group by對一列進行分組,一般與聚合函數結合使用
hive (test_db)> select avg(sal) avg_sal,deptno from emp group by deptno having avg_sal>2000;
- over(partition by):與group具有同樣的分組功能,但是顯示方式會不同。
hive (test_db)> select sal,deptno,avg(sal) over(partition by deptno) avg_sal from emp;
表的join
- 等值join:只有進行連接的兩個表中都存在于連接標準相匹配的數據才會保存下來。
hive (test_db)> select e.empno,e.ename,d.deptno from emp e join dept d on e.deptno=d.deptno;
- 左join和右join:left join返回左表中的所有值,加上右表,如果右表對應項沒有則用null填充。right join相反
select e.empno,e.ename ,d.deptno ,e.sal from emp e left join dept d on e.deptno=d.deptno;
select e.empno,e.ename ,d.deptno ,e.sal from emp e right join dept d on e.deptno=d.deptno;