一、数据导入
1.最基本的导入方式:load
load data [ local ] inpath '/opt/module/datas/test.txt' [overwrite] into table student [partition (partcol1=val1,…)];
- local:从本地导入加上 local ,从 hdfs 导入不用加
- overwrite:是否覆盖之前的数据
- partition:导入指定分区
栗子:
a).加载本地文件到 hive
hive (default)> load data local inpath '/opt/module/datas/test.txt' into table default.test;
b).加载 hdfs 文件到 hive
hive (default)> load data inpath '/user/lpy/hive/test.txt' into table default.test;
c).加载数据覆盖表中已有的数据
hive (default)> load data inpath '/user/lpy/hive/test.txt' overwrite into table default.test;
d).加载数据到二级分区表中
hive (default)> load data local inpath '/opt/module/datas/test.txt' into table
default.test partition(month='200008', day='20');
2.通过查询语句向表中插入数据(Insert)
- insert into:以追加数据的方式插入到表或分区,原有数据不会删除
- insert overwrite:会覆盖表或分区中已存在的数据
- 注意:insert不支持插入部分字段
栗子:
a).基本数据插入
hive (default)> insert into table test partition(month='200008') values(1,'zhangsan'),(2,’lisi’);
b).根据单张表查询结果插入
hive (default)> insert overwrite table student partition(month='201708') select id, name from student where month='201709';
c).多表(多分区)插入模式(根据多张表查询结果)
hive (default)> from test
insert overwrite table test partition(month='200207')
select id, name where month='200209'
insert overwrite table test partition(month='200206')
select id, name where month='200209';
3.查询语句中创建表并加载数据(As Select)
a).根据查询结果创建表(查询的结果会添加到新创建的表中)
hive (default)> create table if not exists test2 as select id, name from test;
4.创建表时通过Location指定加载数据路径
hive (default)> create external table if not exists test3(
id int, name string
)
row format delimited fields terminated by '\t'
location '/test;
5.Import 数据到指定 Hive 表中
注意:
- 先用 export 导出后,再将数据导入。
- import 数据的导入与其他方式不同,import 不需要描述任何表结构,因为在 export 导出数据时是将数据的整体原封不动的连同元数据一同导出。
hive (default)> import table test2 partition(month='200209') from
'/user/hive/warehouse/export/test';
二、数据导出
1.Insert导出
a).将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/test'
select * from test;
b).将查询的结果格式化导出到本地
hive(default)>insert overwrite local directory '/opt/module/datas/export/test1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from test;
c).将查询的结果导出到HDFS上
hive (default)> insert overwrite directory '/user/lpy/test2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from test;
2.Hadoop命令导出到本地
hive (default)> dfs -get /user/hive/warehouse/test/month=200209/000000_0
/opt/module/datas/export/test3.txt;
3.Hive Shell 命令导出
基本语法:( hive -f/-e 执行语句或者脚本 > file )
[lpy@hadoop102 hive]$ bin/hive -e 'select * from default.test;' >
/opt/module/datas/export/test4.txt;
4.Export导出到HDFS上
hive (default)> export table default.test to '/user/hive/warehouse/export/test';
注意:
- export 数据的导出区别于其他方式,export 是整体导出,连同元数据一同导出,其他的只导数据。
- export 和 import 主要用于两个 Hadoop 平台集群之间 Hive 表迁移
5.Sqoop导出......百度