hive内部表外部表的创建及load数据

版权声明: https://blog.csdn.net/xiongbingcool/article/details/82982099

内部表

  • 创建hive内部表
create table test_01(id bigint, name string ) row format delimited fields terminated by ',';
默认记录和字段分隔符:

\n   每行一条记录

^A    分隔列(八进制 \001)

^B    分隔ARRAY或者STRUCT中的元素,或者MAP中多个键值对之间分隔(八进制 \002)

^C    分隔MAP中键值对的“键”和“值”(八进制 \003)

 

自定义分隔符:

CREATE TABLE test(

    ……

)

ROW FORMAT DELIMITED

FIELDS TERMINATED BY '\001'

COLLECTION ITEMS TERMINATED BY '\002'

MAP KEYS TERMINATED BY '\003'

LINES TERMINATED BY '\n'
  • 创建测试数据文件
vi test_01;

1,test1
2,test2
3,test3
4,test4
5,test5
6,test6
7,test7
  • 将数据文件上传到hdfs并加载到hive,发现hdfs中的文件会被移动到hive表目录,而不是拷贝
hdfs dfs -put test_01 /test
load data inpath '/test/test_01' into table test_01;
  • 再次上传并加载,会追加数据到hive表中,并且hive目录存在test_01和test_01_copy_1两个文件
hdfs dfs -put test_01 /test
load data inpath '/test/test_01' into table test_01;
  • 再次上传并使用overwrite加载,原先的test_01/test_01_copy_1两个文件会先被删除,新文件test_02被移动到hive表目录中
hdfs dfs -put test_01 /test/test_02
load data inpath '/test/test_02' overwrite into table test_01;
  • 删除表后目录文件及meta数据都会被删除
drop table test_01;

外部表

  • 创建hive外部表,如果指定location 'xxx'会在hdfs中创建对应目录并将load的文件移动至该目录下,否则会在hive表下创建test_02目录并装载文件
create EXTERNAL table test_02(id bigint, name string) row format delimited fields terminated by ',';
  • 将数据文件上传到hdfs并加载到hive
hdfs dfs -put test_01 /test
load data inpath '/test/test_01' into table test_02;
  • 删除表后目录及文件依然存在,只会删除test_02的meta数据,再次创建表即可查询历史数据
drop table test_02;

猜你喜欢

转载自blog.csdn.net/xiongbingcool/article/details/82982099