外部表和内部表在元数据的组织上是相同的,但实际数据的存储有较大的差别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。不然是根本访问不到数据的