HIVE
静态分区
二级分区
1 准备数据
a.log
1,a,河北,保定
2,b,河北,保定
3,c,河北,保定
b.log
4,d,河北,石家庄
5,e,河北,石家庄
6,f,河北,石家庄
c.log
7,j,河北,邯郸
8,q,河北,邯郸
9,k,河北,邯郸
d.log
10,A,河南,郑州
11,B,河南,郑州
12,C,河南,郑州
e.log
10,A,河南,洛阳
11,B,河南,洛阳
12,C,山西,太原
2 创建分区表
create table logs (
id int,
name string,
p string,
c string
)
partitioned by(province string,city string)
row format delimited fields terminated by’,’;
3导入数据
load data local inpath ‘/data/logs/a.log’ into table logs partition(province=‘HB’,city=‘BD’);
load data local inpath ‘/data/logs/b.log’ into table logs partition(province=‘HB’,city=‘SJZ’);
load data local inpath ‘/data/logs/c.log’ into table logs partition(province=‘HB’,city=‘HD’);
load data local inpath ‘/data/logs/d.log’ into table logs partition(province=‘HB’,city=‘ZZ’);
4 查看数据
select * from logs where city=‘BD’;
动态分区
1 开启分区功能设置
set hive.exec.dynamic.partition=true ;
set hive.exec.dynamic.partition.mode=nonstrick; 可以从普通表中导入数据
2 准备数据
根据数据的某个字段的值分区 , 地域 时间
1,2021-03-11,100
2,2021-03-12,200
3,2021-03-11,400
4,2021-03-12,110
5,2021-03-11,150
6,2021-03-13,10
3 建普通的表
create table tb_order(
oid int ,
ct string ,
money double
)
row format delimited fields terminated by ‘,’ ;
load data local inpath ‘/data/orders/’ into table tb_order ;
4 建立分区表
create table tb_dyn_order3(
oid int ,
ct string ,
money double
)
partitioned by(dy string)
row format delimited fields terminated by ‘,’ ;
5导入数据
insert into table tb_dyn_order3 partition(dy)
select * ,ct as dy from tb_order ;
数据导入和导出
数据导入方式
1 建表的时候location
2将数据put到hdfs 表目录下
3load数据 :本地数据 load local inpath “/data/user” into table tb_user;
hdfs 数据 同上 少个local
覆盖导入数据 load data local inpath “/data/user” overwrite into table tb_user;
4 insert into tb_name from select…
5建表的同时导入数据 create table into tb_name as select from …
6 import : import table tb_name from ‘hdfs 路径’;
数据导出方式
1 create table as select from 导出
2 hdfs dfs -get 数据 导出的是原始表数据
3将数据导出到本地/hdfs目录中 :insert overwrite (local) directory ‘/data/out’ row format delimited fields terminated by ‘:’ select form tb_name;
4 hive shell -e
-f
>> 文件
5 export table tb_name to ‘hdfs路径’;