hive入门一

创建表

普通表

create table ip_table1(ip string, region string, country string,province string,city string,area string,company string)

row format delimited fields terminated by '|';

导入数据: load data local inpath '/home/adminuser/app/mydata/ip.txt' into table ip_table1;

分区表

create table ip_table2(ip string, region string, country string,province string,city string,area string)

partitioned by(company string)

row format delimited fields terminated by '|';

分区的字段不能出现在创建表的字段里面,否则会报错。

导入数据:load data local inpath '/home/adminuser/app/mydata/ip1.txt' into table ip_table2 partition(company='1')

创建多层分区:

create table t10(

    id      int

   ,name    string

   ,hobby   array<string>

   ,add     map<String,string>

)

partitioned by (pt_d string,sex string)

row format delimited fields terminated by ','

collection items terminated by '-'

map keys terminated by ':'

多分区导入数据:

load data local inpath '/home/hadoop/Desktop/data' overwrite into table t10 partition ( pt_d = '0',sex='male');

多分区的实质是在第一个分区的文件夹的基础上再建子文件夹。

分区的增删

添加一个分区:alter table testljb add partition (age=2);

增加多个分区:

alter table testljb add partition(age=3) partition(age=4);

增加多分区表的分区:

alter table testljb add partition(age=1,sex='male');

删除分区:

alter table testljb drop partition(age=1);

动态分区与静态分区

在一个场景下:将数据导入另一张表中,这张表的创建语句:

create table ip_table3(ip string, country string,province string,city string,area string)

partitioned by(company string,region string)

row format delimited fields terminated by '|';

现在要从另一张表中的数据导入先要设置打开动态分区:

set hive.exec.dynamic.partition=true;

set hive.exec.dynamic.partition.mode=nonstrict;

 

insert overwrite table ip_table3 partition (company,region) select ip,country,province,area,city,company,region from ip_table1;

这里语言会查找最后两个字段的数据匹配到partition里面的字段

坑:尽量避免使用中文作为分区的字段,否则很麻烦!(可以设置使用中文)

 

数据查询:

一、order by语法

select * from ip_table3 order by company desc(asc);

二、sort by语法

select * from ip_table3 sort by company desc(asc);

order by 保证的是全局的排序,而sort by保证的是每个分区(reducer)的有序。

三、distribute by

select * from ip_table3 distribute by company sort by ip;

ditribute by是控制map的输出在reducer是如何划分的,比如在上个字段中,这里需要注意的是distribute by必须要写在sort by之前。因为,sort by作用于分区。

set mapred.reduce.tasks=2;

select * from student distribute by sex sort by sage;

distribute by 是按照指定的字段对数据进行划分到不同的reduce中
第一的语句,我们只是按照性别和年龄排序
第二个,我们设置了2个reduce,并且按照性别分区。sort by sage;

 

四、cluster by

select * from ip_table3 cluster by company

cluster by的功能就是distribute by和sort by相结合,上句sql等价select * from ip_table3 distribute by company sort by ip;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西门吹水之城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值