一、数据导入
1.最基本的导入方式:load
load data [ local ] inpath '/opt/module/datas/test.txt' [overwrite] into table student [partition (partcol1=val1,…)];
栗子:
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)
栗子:
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 表中
注意:
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';
注意:
5.Sqoop导出......百度