hive的数据导入
1 直接插入,效率低
insert into table XXX values(); 如果有分区的话就可以加上 partition(month='201809')
2 通过load方式加载数据
load data local inpath '/export/servers/hive-study-data/score.csv' overwrite into table score partition(month='201810'); //单分区情况
load data local inpath '/export/servers/hive-study-data/score.csv' overwrite into table score2 partition(year='2018',month='10',day='16'); //多分区情况
3 通过查询方式加载数据
insert overwrite table score3 partition(year='2018',month='10',day='16') select s_id,c_id,s_score from score2;
4 多插入模式(将一张表拆分成多个部分的时候用)
将score4拆分成两部分,首先创建两张表
create table score_first(s_id string,c_id string) partitioned by(month string) row format delimited fields terminated by '\t';
create table score_second(c_id string,s_score int) partitioned by(month string) row format delimited fields terminated by '\t';
往两张表插入数据,overwrite表示插入多条数据给表score_first
from score4 insert overwrite table score_first partition(month='201806') select s_id,c_id insert overwrite table score_second partition(month = '201806') select c_id,s_score;
5 查询语句中创建表并加载数据(复制A表数据给B表)
create table score5 as select * from score;
6 创建表时通过location指定加载数据的路径
创建表,并指定在hdfs上的位置
create table score7(s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/scoredatas';
在这个路径下放一个数据
hdfs dfs -put score.csv /scoredatas/
重新查询就可以得到数据
select * from score7;
7export导出与import导入的hive操作
export table techer to '/export/techer'; 从hive到处hdfs中
import table techer2 from '/export/techer';
hive的数据导出:
1 insert overwrite local directory '/export/servers/daochu' select * from score10;
导出是乱码格式的
1 insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;
导出是正常格式:
2 shell命令导出
bin/hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt
正常码
排序:
order by 全局排序
sort by 局部排序,每个reduce 排一个序号
distribute by 分区排序
设置reduce数量
set mapreduce.job.mapreduces=7;
通过distribute进行分区 sort进行排序,这样就会在一个文件夹出现7个文件,按照s_id分区
insert overwrite local directory '/export/servers/hivedatas/sort' select * from score distribute by s_id sort by s_score;
当distribute by 和sort by 字段相同时,可以采用cluster by 方式,这种方式排序只能倒叙排序
以下两种写法等价
select * from score cluster by s_id;
select * from score distribute by s_id sort by s_id;