数据的导入和导出

数据加载到表

非分区表

方式1:
假设当前已经存在一张非分区表,那么可以直接通过拷贝的方式把数据拷贝到hdfs上面的对应目录
eg

1、创建测试表
hive (r_db2)> create table input_test(id int, name string);
2、创建测试数据
[hduser@yjt test8.out]$ cat 000000_1   # 这个数据之间的分隔符^A或者1
1hadoop
2spark
3、put数据
[hduser@yjt test8.out]$ hadoop fs -put 000000_1 /hive/warehouse/r_db2.db/input_test
4、查询数据
hive (r_db2)> select * from input_test;
OK
input_test.id	input_test.name
1	hadoop
2	spark

方式2:
与方式1类似,其实底层都是hive进行数据的拷贝

表创建好以后,直接使用   load data [local] inpath "path" [overwrite] into table tblname;

方式3:
使用insert方式添加

方式4:
通过其他表的数据插入到当前表

hive (r_db2)> truncate table input_test;   # 清空表数据
hive (r_db2)> insert into table input_test select s.id, s.name from test3;   # 通过其他表填充当前表
或者
hive (r_db2)> from test3 as s insert into table input_test select s.id, s.name;

分区表

分区表与非分区表类似
eg

方式1:

1、创建分区表
hive (r_db2)> create table input_test1(id int, name string) partitioned by(longtime string) row format delimited fields terminated by ',';
2、创建hdfs目录
hive (r_db2)> dfs -mkdir "/hive/warehouse/r_db2.db/input_test1/2020-06-29";
3、put数据
[hduser@yjt hive]$ hadoop fs -put test.txt /hive/warehouse/r_db2.db/input_test1/2020-06-29
4、修复元数据
hive (r_db2)> msck repair table input_test1;  # 或者使用alter添加对应的分区

方式2:

hive (r_db2)> load data local inpath "/tmp/hive/test.txt" into table input_test1 partition(longtime='2002-06-29');

方式3:

hive (r_db2)> insert into table input_test1 partition(longtime="2021-06-29") values(1, 'test');

方式4:

hive (r_db2)> from test3 as s insert into table input_test1  partition(longtime)  select s.id,s.name, s.currentdate;

数据导出

导出到本地

hive (r_db2)> insert overwrite local directory "/tmp/hive/out" select * from input_test1;

导出到hdfs

hive (r_db2)> insert overwrite directory "/tmp/hive/out" select * from input_test1;
原文地址:https://www.cnblogs.com/yjt1993/p/13214908.html