内部表和外部表区别
- 未被 external 修饰的表是内部表(managed table ),被 external 修改的是外部表(external table );
- 内部表的数据由Hive自身管理,外部表由HDFS管理;
- 内部表的数据存储位置是hive.metastore.warehouse.dir(默认:/user/hive/warehouse),外部表数据的存储位置由自己指定(如果没有 LOCATION),Hive将在HDFS上的 /user/hive/warehouse/文件夹下以外部表的表名创建一个文件夹,并将属于这个表的数据存放在这里);
- 删除内部表会直接删除元数据(metadata)及存储数据,删除外部表仅仅会删除元数据,HDFS上的文件并不会删除,Hive默认创建的是内部表。
- 对内部表的修改会将修改直接同步给元数据,而对外部表的表结构和分区进行修改,则修改修复。
MSCK REPAIR TABLE table_name
创建内部表
create table test.t1(
id int
,name string
,hobby array<string>
,add map<string,string>
)
row format delimited
fields terminated by ','
collection items terminated by '_'
map keys terminated by ":";
Time taken: 0.112 seconds
hive>
>
>
> create table test.t1(
> id int
> ,name string
> ,hobby array<string>
> ,add map<string,string>
> )
> row format delimited
> fields terminated by ','
> collection items terminated by '_'
> map keys terminated by ":";
OK
Time taken: 0.056 seconds
hive>
查看表的描述
Time taken: 0.056 seconds
hive> desc test.t1;
OK
col_name data_type comment
id int
name string
hobby array<string>
add map<string,string>
Time taken: 0.048 seconds, Fetched: 4 row(s)
hive>
装载数据
注意:一般很少用insert(不是insert overwrite) 语句,因为就算是插入一条语句,也会调用MapReduce,这里我们选择Load Data 的方式。
原始数据
1,xiaoming,book-TV-code,beijing:chaoyang-shagnhai:pudong
2,lilei,book-code,nanjing:jiangning-taiwan:taibei
3,lihua,music-book,heilongjiang:haerbin
加载数据
load data local inpath '/opt/software/gouyang/5.txt' overwrite into table test.t1;
Time taken: 0.048 seconds, Fetched: 4 row(s)
hive> load data local inpath '/opt/software/gouyang/5.txt' overwrite into table test.t1;
Loading data to table test.t1
Table test.t1 stats: [numFiles=1, numRows=0, totalSize=147, rawDataSize=0]
OK
Time taken: 0.253 seconds
hive> select * from test.t1;
OK
t1.id t1.name t1.hobby t1.add
1 xiaoming ["book-TV-code"] {"beijing":"chaoyang-shagnhai:pudong"}
2 lilei ["book-code"] {"nanjing":"jiangning-taiwan:taibei"}
3 lihua ["music-book"] {"heilongjiang":"haerbin"}
Time taken: 0.058 seconds, Fetched: 3 row(s)
hive>
来源:oschina
链接:https://my.oschina.net/u/4101357/blog/3198326