Hive表數(shù)據(jù)的導(dǎo)入
- 從本地導(dǎo)入
load data local inpath 'local_path' into table table_name;


- 從hdfs導(dǎo)入
load data inpath 'hdfs_path' into table table_name;

- 覆蓋的方式導(dǎo)入
load data local inpath 'local_inpath' overwrite into table table_name;
load data inpath 'hdfs_inpath' overwrite into table table_name;
-
創(chuàng)建表時(shí)通過(guò)子查詢加載(會(huì)自動(dòng)創(chuàng)建表結(jié)構(gòu))
hive7.png - insert加載子查詢到已創(chuàng)建好的表中(手動(dòng)創(chuàng)建表結(jié)構(gòu))
hive (test_db)> create table emp3 like emp;
hive (test_db)> insert into table emp3 select * from emp;
- 在創(chuàng)建表時(shí)location 指定文件位置(外部表)
create table ... location 'hdfs_location';
- 分區(qū)表加載數(shù)據(jù)
load data [local] inpath 'paht' into table table_name partition(partioncol1=val1...);
Hive查詢結(jié)果和表數(shù)據(jù)的導(dǎo)出
-
導(dǎo)出到本地文件(目錄數(shù)據(jù)會(huì)被覆蓋)
hive9.png -
導(dǎo)出到hdfs(目錄數(shù)據(jù)會(huì)被覆蓋,如果這個(gè)目錄不存在則會(huì)自動(dòng)創(chuàng)建)
hive11.png -
用hive的-e、-f命令導(dǎo)出(不會(huì)覆蓋數(shù)據(jù))
hive10.png - 利用sqoop導(dǎo)入導(dǎo)出
排序
- order by:order by子句對(duì)一列按升序(asc)或降序(desc)排列。但是只能限制于一個(gè)reduce。
hive (test_db)> select sal from emp order by sal desc;

- sort by:對(duì)每個(gè)reduce中的數(shù)據(jù)進(jìn)行單獨(dú)排序,但是全局不一定有序。
hive (test_db)> set mapreduce.job.reduces=3;
hive (test_db)> select sal,deptno from emp sort by sal desc;

- distribute by:shuffle中的分區(qū)默認(rèn)是按照key值計(jì)算hash值然后取余均勻的分發(fā)到reducer中。distribute by可以設(shè)置map端輸出后是按照哪一個(gè)字段進(jìn)行hash取余分區(qū)的。
hive (test_db)> select sal,deptno from emp distribute by deptno sort by sal desc;

- cluster by:當(dāng)dstribute by和sort by字段相同時(shí),可以使用cluster by,相當(dāng)于一種簡(jiǎn)寫方式。
分組
- group by和having group by對(duì)一列進(jìn)行分組,一般與聚合函數(shù)結(jié)合使用
hive (test_db)> select avg(sal) avg_sal,deptno from emp group by deptno having avg_sal>2000;

- over(partition by):與group具有同樣的分組功能,但是顯示方式會(huì)不同。
hive (test_db)> select sal,deptno,avg(sal) over(partition by deptno) avg_sal from emp;

表的join
- 等值join:只有進(jìn)行連接的兩個(gè)表中都存在于連接標(biāo)準(zhǔn)相匹配的數(shù)據(jù)才會(huì)保存下來(lái)。
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返回左表中的所有值,加上右表,如果右表對(duì)應(yīng)項(xiàng)沒(méi)有則用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;



