外部表和內部表在元數據的組織上是相同的,但實際數據的存儲有較大的差別java
能夠建立分區linux
內部表的建立過程和數據加載過程能夠分別獨立完成,也能夠在同一個語句中完成bash
外部表只有一個過程,加載數據和建立表同時完成oop
create external table ………. Locationspa
若是不指定location ,會使用默認目錄code
實質只是指定路徑而已hadoop
刪除一個外部表時,僅僅刪除的是連接input
使用 DESCRIBE EXTENDED tablename能夠查看錶是內部仍是外部表string
load data會轉移數據it
使用local表示從本地導入,使用的是複製操做,原文件保留;沒有local,表示從hdfs文件系統導入,使用的是剪切操做,原目錄下的文件將被移除
不管是內部仍是外部表,無非就是往對應的hdfs目錄複製文件,再以定義的表結構來讀取數據。
hive> create external table etest1(name string , age string); OK Time taken: 1.181 seconds hive> load data inpath '/hadoopin/wordcout/wc.txt' into table etest1; Loading data to table default.etest1 OK Time taken: 1.346 seconds hive> select * from etest1; OK home java NULL linux java NULL java NULL home NULL NULL Time taken: 1.981 seconds, Fetched: 5 row(s) hive>
使用以後wc.txt文件就不存在了。
hadoop@hadoop:~$ hadoop fs -ls /hadoopin/wordcout/ Found 1 items drwxr-xr-x - hadoop supergroup 0 2018-03-15 18:18 /hadoopin/wordcout/output hadoop@hadoop:~$
若是建立內部表時沒有指定location,就會在/user/Hive/warehouse/下新建一
個表目錄,其他狀況同上
hive> create table test2(name string , age string) location '/hive/input/table_data';
hadoop@hadoop:~$ hadoop fs -ls /hive/input/ Found 1 items drwxr-xr-x - hadoop supergroup 0 2018-04-02 14:47 /hive/input/table_data
分區表屬性
若是表中的數據及分區的個數,執行包含有全部分區的查詢可能會觸發一個巨大的MR任務。
可將hive設置爲strict模式,若是對分區表進行查詢 而where子句沒有加分區過濾的話,會禁止提交任務。
set hive.mapred.mode=strict;
set hive.mapred.mode=nonstrict;
partition by 中子句中定義的列是表中正式的列,可是數據文件內並不包含這些列。
hive> create table logs(ts bigint,line string) > partitioned by(dt string,country string); OK Time taken: 0.202 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file1' into table logs > partition(dt='2001-01-01',country='GB'); Loading data to table default.logs partition (dt=2001-01-01, country=GB) OK Time taken: 2.252 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file2' into table logs > partition(dt='2001-01-01',country='GB'); Loading data to table default.logs partition (dt=2001-01-01, country=GB) OK Time taken: 0.975 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file3' into table logs > partition(dt='2001-01-01',country='US'); Loading data to table default.logs partition (dt=2001-01-01, country=US) OK Time taken: 1.002 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file4' into table logs > partition(dt='2001-01-02',country='GB'); Loading data to table default.logs partition (dt=2001-01-02, country=GB) OK Time taken: 1.023 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file5' into table logs > partition(dt='2001-01-02',country='US'); Loading data to table default.logs partition (dt=2001-01-02, country=US) OK Time taken: 0.918 seconds hive> load data local inpath '/home/hadoop/input/hive/partition/file6' into table logs > partition(dt='2001-01-02',country='US'); Loading data to table default.logs partition (dt=2001-01-02, country=US) OK Time taken: 0.718 seconds hive>
此時分區表已經分區,表目錄結構以下:
查看分區
hive> show partitions logs; OK dt=2001-01-01/country=GB dt=2001-01-01/country=US dt=2001-01-02/country=GB dt=2001-01-02/country=US Time taken: 0.142 seconds, Fetched: 4 row(s) hive> show partitions logs partition(country='US'); OK dt=2001-01-01/country=US dt=2001-01-02/country=US Time taken: 0.123 seconds, Fetched: 2 row(s) hive> show partitions logs partition(dt='2001-01-01'); OK dt=2001-01-01/country=GB dt=2001-01-01/country=US Time taken: 0.121 seconds, Fetched: 2 row(s) hive> show partitions logs partition(dt='2001-01-01',country='US'); OK dt=2001-01-01/country=US Time taken: 0.121 seconds, Fetched: 1 row(s) hive>
使用hdfs的rmr刪除分區
hadoop fs -rmr /user/hive/warehouse/2001-01-01/US
基於分區表建立的外部表必定要對外部表執行ALTER TABLE table_name ADD PARTITION。不然是根本訪問不到數據的