hive数据定义

1. 内部表

外部表和内部表在元数据的组织上是相同的,但实际数据的存储有较大的差异

可以创建分区

内部表的创建过程和数据加载过程可以分别独立完成,也可以在同一个语句中完成

外部表只有一个过程,加载数据和创建表同时完成

create external table ………. Location

如果不指定location ,会使用默认目录

实质只是指定路径而已

删除一个外部表时,仅仅删除的是链接

使用 DESCRIBE  EXTENDED tablename可以查看表是内部还是外部表

1. 外部表

load data会转移数据

使用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:~$ 
  • 可以对一张存在的表进行表结构复制,而不会复制数据,内部表也可以
  • create  external table  if not exists  mydb.employ2
  • like  mydb.employ
  • location  ‘/path/to/data’
  • 如果省略掉external 而且源表是外部表,那么新生成的也是外部表
  • 如果省略掉external 而且源表是内部表,那么新生成的也是内部表
  • 如果语句中有external 而且源表是内部表,那么新生成的将是外部表

2. 创建表指定表的数据存放地

     如果创建内部表时没有指定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

3. 分区表的创建

分区表属性

如果表中的数据及分区的个数,执行包含有所有分区的查询可能会触发一个巨大的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

4. 外部分区表

基于分区表创建的外部表一定要对外部表执行ALTER TABLE table_name ADD PARTITION。否则是根本访问不到数据的

猜你喜欢

转载自my.oschina.net/u/3746673/blog/1788624