Hive- 表
在hive中表的類型:管理表和托管表(外部表)。
內部表也稱之為MANAGER_TABLE,默認存儲在/user/hive/warehouse下,也可以通過location指定;刪除表時,會刪除表的數據以及元數據;
外部表稱之為EXTERNAL_TABLE。在創建表時可以自己指定目錄位置(LOCATION),數據存儲所在的目錄;刪除表時,只會刪除元數據不會刪除表數據;
創建外部表實例
create external table if not exists default.emp_ext( empno int, ename string, job string, mgr int, hiredate string, sal double, comm double, deptno int ) row format delimited fields terminated by '\t' location '/opt/input/emp';分區表實際上就是對應一個HDFS文件系統上的獨立的文件夾,該文件夾下是該分區所以的數據文件。hive中的分區就是分目錄,把一個大的數據集根據業務需要分割成更小的數據集。
?
在查詢時通過WHERE子句中的表達來選擇所需要的指定的分區,這樣的查詢效率會提高很多。
?
create external table if not exists default.emp_partition( empno int, ename string, job string, mgr int, hiredate string, sal double, comm double, deptno int ) partitioned by(month string) row format delimited fields terminated by '\t';分區表注意事項:
修復表:msck repair table table_name;
可以寫shell腳本
dfs -mkdir -p /user/hive/warehouse/dept_part/day=20171025; dfs -put /opt/weblog/log.log /user/hive/warehouse/dept_part/day=20171025;alter table dept_part and partition('day=20171025');查看表的分區數:show partitions dept_part;
導入數據進入hive表
load data [local] inpath 'filepath' [overwrite] into table tablename into tablename [partition (partcol1=val,...)];
參數帶local意思是本地文件,不帶就是HDFS文件
參數帶overwrite意思是覆蓋原本文件的內容,不帶就追加內容
分區表加載,特殊性partition (partcol1=val,...)
1.加載本地文件到hive表
load data local inpath '/root/emp.txt' into table default.emp2.加載hdfs文件到hive表中
load data inpath '/root/emp.txt' into table default.emp3.加載數據覆蓋表中已有的數據
load data inpath '/root/emp.txt' overwrite into table default.emp4.創建表是通過insert加載
create table default.emp_ci like emp; insert into table default.emp_ci select * from default.emp;5.創建表的時候通過指定location指定加載
?
?
導出hive表數據
?
insert overwrite local directory '/opt/datas/hive/hive_exp_emp' select * from default.emprow format delimited fields terminated by '\t';#bin/hive -e "select * from default.emp;" > /opt/datas/hive/exp_res.txt
?
?
hive表多重插入
假如有一個需求:
從t_4中篩選出不同的數據,插入另外兩張表中;
?
但是以上實現方式有一個弊端,兩次篩選job,要分別啟動兩次mr過程,要對同一份源表數據進行兩次讀取
如果使用多重插入語法,則可以避免上述弊端,提高效率:源表只要讀取一次即可
?
轉載于:https://www.cnblogs.com/RzCong/p/7732590.html
總結
- 上一篇: ListView实现分页
- 下一篇: noip提高组2000 乘积最大