您的位置:首页 > 大数据

Hive常用命令与内容

2018-01-09 09:24 330 查看
1、创建普通表
create table test(id string,name string)
row format delimited fields terminated by ','
stored as textfile;
 
查看在HDFS上面储存的路径 hadoop fs -ls /user/hive/warehouse/test
 
2、加载数据

本地加载:load data local inpath '/data/1.txt' into table test;
HDFS加载 :
load data inpath '/user/data/1.txt' into table test;
 
3、创建一个外部表
create external table test_external(id string,name string)
row format delimited fields terminated by ','
stored as textfile LOCATION '/data';
 
从hdfs上加载数据
load data inpath '/user/data/1.txt' into table test_external;
 
抹掉之前的数据重写
load data inpath '/user/data/1.txt' overwrite into table test_external;
 
4、删除表
drop table test
drop table test_external

1、hive分区表的创建
create table patition_table(name string,salary float,gender string)
partitioned by (dt string,dep string)
row format delimited fields terminated by ','
stored as textfile;
 
2、查看表结构
desc patition_table;
 
3、查看分区
show partitions patition_table;
但如果表里面都没有数据,上面这个语句执行后不出任何结果
如果有数据显示如下
dt=2014-03-29/dep=USA
 
4、加载数据到分区表内
load data local inpath '/data/1.txt' into table patition_table partition(dt='2014-03-29',dep='USA’);
 
5、在hdfs上的路径分布如下
/user/hive/warehouse/patition_table/dt=2014-03-29/dep=USA/
 
6、删除一个分区
alter table patition_table drop partition (dt='2014-03-29',dept='USA');
 
7、查看前几条
select * from tablename limit 1;
在hive中不支持 select * from tablename limit 1 5; //1 to 5
 
8、selcet的嵌套
from (select name,salary,from patition_table) e selecet e.name,e.salary where e.salary>1000;
 
9、where in操作
select * from patition_table where salary in(7000,3000);
 
10、case when then 操作
select * case when salary,
case when salary <5000 then 'L1'
 when salary >=5000 and salary <10000  then 'L2'
 when salary >=10000 and salary <15000 then 'L3'
 when salary >=15000 then 'L4'
 else 'L0'
 end
as salary_level,gender from patition_table;
 
11、having操作
select gender from patition_table group by gender having sum(salary)>100000;
如果不支持having
select gender from(select gender,sum(salary) as salary_total from patition_table group by(gender))e where e.salary_total>100000;
注意一下having后面跟着一定是一个聚合函数
 
12、group by
select gender,sum(salary) from patition_table group by gender;
 
13、给外部表加分区时,需要执行MSCK REPAIR TABLE  tablename
在创建动态分区时

1、group by操作
 group by 操作是在map端进行操作
 hive.map.aggr=false //(默认)
 hive.map.aggr=true //作用是在map端做一次聚合操作,要求内存要足够
 
2、join操作,只支持等值join
select a.val,b.val from a join b on(a.key=b.key)
 
3、mapjoin 操作,只适合一个大表时
select /*+mapjoin(b)*/ a.key,a.value from a join b on a.key=b.key;//小表放map join里面
 
4、left semi join操作
select * from things left semi join sales on (sales.id = things.id);
 
5、order by 和 sort by
 select * from order_test order by math;
注意:
order by 是做一个全局的排序,所有的数据发送到一个reduce上操作
set hive.mapred.mode=nonstrict //(默认值)
set hive.mapred.mode=strict //在该模式下必须指定limit
 
sort by只能保证在同一个reduce上排序,使用sort by 可以指定reduce个数(set mapred.reduce.tasks=<number>)
 
6、union all操作,作为多表合并操作
 union all 要求各表select出的字段类型必须完全匹配
 hive不支持顶层的union,只支持将union封装在子查询中,而且子查询中必须要有别名
 select * from (
 select count(*) as type1 from a  where a.math>70
 union all
 select count(*)as tyge1 from a where a.englist>80
 )tmp_user
 
7、索引
 1)、先创建一张表
  create table index_test(id int,name string)partitioned by (dt string) row format delimited fields terminated by ‘,’;
 2)、创建一个临时的表
  create table index_tmp(id int ,name string,dt string)  row format delimited fields terminated by ‘,’;
 
 3)、加载数据到临时表中
1、rcfile用途,压缩比例高,读取列更快,保证同一个record在一个快上
  1)、建表
create table hive_rc(name string,gender string, age int) row format delimited fields terminated by ',' stored as rcfile;
  
  2)、load数据到hive_rc表中
由于以rcfile的存储格式中无法直接加载数据,需创建一个临时表先把数据记载到临时表中
create table hive_raw(name string,gender string, age int) row format delimited fields terminated by ',' stored as textile;
 
load data local in path  ‘数据存放目录’into table hive_raw;
insert into table hive_rc select * from hive_raw;
 
2、hive自定义表结构
 
1)、文件格式如下:
     000377201207221125^^apple iphone 4s^^2
     132288201210331629^^thinking in java^^1
     132288201210331629^^thin sss^^1111
 
2)、add jar /$hive-x.y.z/lib/hive-contrib-x.y.z.jar
3)、创建一张表
create external table hive_ser(
times String,
product_name String,
sale_num string
)row format
serde 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
with serdeproperties
('input.regex'='([^^]*)\\^\\^([^^]*)\\^\\^([^^]*)','output.format.string'='%1$s %2$s %3$s')
stored as textfile
 
3、复合类型
 1)、假设数据如下
  192.168.1.1,3105007010|3105007011|3105007012
  192.168.1.2,3105007020|3105007011|3105007022
 2)、创建表
create table login_array(
ip string,
uid array<bigint>
)
partitioned by (dt string)
row format delimited
fields terminated by ','
collection items terminated by '|'
stored as textfile
3)、加载数据
4)、使用下标访问数组
 select ip,uid[0] from login_array;
5)、读取uid的长度
 select size(uid) as total from login_array;
6)、数组查找
 select * from login_array where array_contains(uid,2105007010)
 
 
map类型
1)、数据格式如下
2014-03-03 12:22:34#127.0.0.1#get#amap#src=123&code=456&cookie=789#status=success&time=2s
2014-03-03 12:22:34#127.0.0.1#get#autonavi#src=123&code=456&cookie=789#status=success&time=2s
2)、创建表
create external table map_test_raw(ts string,ip string,type string,logtype string,request Map<string,string>,response Map<string,string>)
row format delimited fields terminated by '#'
collection items terminated by '='
stored as textfile
 
 
struct类型
1)、数据格式如下
192.168.1.1,zhangsan#40
192.168.1.1,lisi#41
 2)、创建表
create table login_struct(
ip string,
user struct<name:string,age:int>
)
row format delimited fields terminated by ','
collection items terminated by '#'
stored as textfile
3)、加载数据
4)、查询数据
select user.name from login_struct load data local inpath '数据存放路径’into table index_tmp;
 
//生产线上不做2、3步骤,都已做过
 
 4)、设置 set hive.exec.dynamic.partition.mode=nonstrict;
        set hive.exec.dynamic.partition=true;
 5)、insert overwrite table index_test partition(dt) select id,name,dt from index_tmp;
 6)、create index index1_index_test on table index_test(id) as ‘org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler’with deferred rebuild
 7)、alter index index1_index_test on index_test rebuild;
 
 8)、show index on index_test;
 9)、show partitions index_test;
 
注意:创建索引的时候,表必须要有partition
在视图上不能创建index
index可以通过stored as 配置存储格式
 
在hdfs上存放的位置:
 
 
8、bucket操作
  是将表或分区中指定列的值为key进行hash,抽样
  set hive.enforce.bucketing=true;
 1)、创建表
create table tb_tmp(id int,age int,name string,timeflag bigint)
row format delimited fields terminated by ',';
 
 2)、创建bucket表
create table tb_stu(id int,age int,name string, timeflag bigint) clustered by(id)
stored by(age) into 5 buckets row format delimited fields terminated by',';
 
3)、加载数据到tb_tmp下,创建数据
 
4)、将数据导入bucket表中
insert into table tb_stu select id,age,name,timeflag from tb_tmp;
 
5)、输出每逢5的记录数如1-10,抽取第5条第10条记录数来
select * from tb_stu tablesample(bucket 1 out of 5 on id);//在一个bucket每5个抽出一个记录数

1关系运算符
 1)、等值 =
 2)、不等值 <>
 3)、小于 <<
 4)、小于等于 <=
 5)、大于 >
 6)、大于等于 >=
 7)、空值 is null
 8)、非空值 is not null
 9)、like 比较 like
10)、java的like操作 rlike
11)、regexp操作 regexp
 
2、udf类型
1)、创建表
create table udf_table(
dt string,
username string,
score float,
total int,
)
row format delimited fields terminated by ','
stored as textfile
2)、数据模型如下
[29/Sep/2013:00:10:07 +0800],gavin,23.12,45
3)、查询语句
select * from udf_table where username is NULL  //注意这里的null必须大写
 
3、数学运算
1)、加法 +
2)、减法 -
3)、乘法 *
4)、除法 /
5)、取余 %
6)、位与操作 &
7)、位或操作 |
8)、位异或操作 ^
9)、位取反操作 ~
 
select total+5 from udf_table
 
4、逻辑运算 and ,or ,not
5、数值运算
 1)、取整函数 round
 2)、指定精度取整函数 round
 3)、向下取整函数 floor
 4)、向上取整函数 ceil
 5)、向上取整函数ceiling
 6)、取随机数函数 rand
 7)、自然指数函数exp
 8)、以10为底对数函数log10
 9)、以2为底对数函数log2
10)、对数函数log
11)、幂运算函数pow
12)、幂运算函数power
13)、开平方函数 sqrt
14)、二进制函数 bin
15)、十六进制函数hex
16)、反转十六进制函数 unhex
17)、进制转换函数conv
18)、绝对值函数abs
19)、正取余函数pmod
20)、正弦函数 sin
21)、余弦函数cos
22)、反正弦函数asin
23)、反余弦函数acos
24)、positive函数 positive
25)、negative函数negative
 
6、日期函数
 1)、unix时间戳转日期函数 from_unixtime
 2)、获取当前unix时间戳函数unix_timestamp
 3)、日期转unix时间戳函数unix_timestamp
 4)、指定格式日期转unix时间戳函数 unix_timestamp
 5)、日期时间转日期函数to_date
 6)、日期转年函数year
 7)、日期转月函数month
 8)、日期转天函数day
 9)、日期转小时函数hour
10)、日期转分钟函数minute
11)、日期转秒函数second
12)、日期转周函数weekofyear
13)、日期比较函数 datediff
14)、日期增加函数 date_add
15)、日期减少函数 date_sub
 
7、if函数
1)、非空查找函数 coalesce
select coalesce(username) from udf_table
 
8、字符串函数
 1)、字符串长度 length
 2)、字符串反转函数 reverse
 3)、字符串连接函数 concat
 4)、带分隔符字符串连接函数 concat_ws
 5)、字符串截取函数 substr,substring
 6)、字符串转大写函数 upper,ucase
 7)、字符串转小写函数 lower,lcase
 8)、去空格函数 trim
 9)、左边去空格函数 ltrim
10)、右边去空格函数 rtrim

hive的调优
1、explain
explain select * from login_array where ip=’192.168.0.1’
explain extended select * from login_array where ip=’192.168.0.1’
这里会对要执行的语句进行优化
然后输入 select * from login_array where ip=’192.168.0.1’进行执行
 
调用本地的mr
hive.exec.mode.local.auto=true
set mapred.tmp.dir=/
 
2、设置hive任务执行在mr的队列
set mapred.queue.name=queue3
set mapred.job.queue.name=queue3
设置任务的优先级别
set mapred.job.priority=high
 
3、hive并行执行
hive会将一个任务转化成一个或多个stage
默认情况下hive只会执行一个stage
如果一个任务有多个stage,并且每个stage是依赖的,那么这个任务不可能并行执行
如果要设置并行执行
hive.exec.parallel 默认是false 如果要并行执行就要设置为true
<property>
<name>hive.exec.parallel</name>
<value>true</value>
</property>
 
4、设置mapper和reducer的个数
1)、map的个数由splits确定
2)、reduce的个数默认是1
3)、set mapred.reduce.tasks=15
 
 
5、jvm重用
1)、mapred.job.reuse.jvm.num.tasks 默认值是1 也就是jvm重用一次后就被销毁
       一般在小文件比较多得情况下使用
6、分区
1)、静态分区与动态分区
        静态分区一定会创建分区,不管select语句的结果有没有数据
        动态分区只有在select结果大于0的时候才会被创建分区
        动态分区会为每一个分区分配一个reduce 如set mapred.reduce.tasks=100
 
例如:
动态分区案例
insert overwrite table tbl_name partition(pt,if_online)
select field1,field2,….,pt,if_online from tbl where XXX
静态分区案例
insert overwrite table tbl_name partition(pt=20121023,if_online=1)
select field1,field2,….,field from tbl where XXX
 
 
7、推测执行
 mapreduce的配置
 set mapred.map.tasks.speculative.execution=false;
 set mapred.reduce.tasks.speculative.execution=false;
 hive.mapred.reduce.tasks.speculative.execution 设置成false
 
8、mapjoin,解决数据倾斜的场景下小表关联大表的问题,但如果小表很大
例子:先根据log取所欲的memberid,然后map join关联members取今天有日志的members的信息,然后再和log做map join
select /*+mapjoin(x)*/ * from log a
left outer join (select /*+mapjoin(c)*/d.* from (select distinct memberid from log) c
join members d
on c.memberid = d.memberid) x
on a.menberid = b.memberid
 
 hive安全问题
1、hive的hadoop安全整合
2、使用hive进行验证
3、hive的权限管理
4、分区级别的权限
5、自定义授权
6、使用hive进行验证
1)、hive-site.xml配置
  <property>
<name>hive.security.authorization.enabled</name>
<value>true</value>
  </property>
 
  <property>
<name>hive.security.authorization.createtable.owner.grants</name>
<value>all</value>
 </property>
 
2)、授权
grant select on database default to user zhangsan
收回权限
revoke select on database default from user zhangsan
 
3)、创建一个角色
create role zhangsan
4)、删除一个角色
drop role zhangsan
5)、将角色添加在某个用户组下
grant role zhangsan to user tifa
6)、查看tifa下被授权的用户
show role grant user zhangsan
7)、把select权限授权给zhangsan用户
grant select on database default to user zhangsan
8)、查看zhangsan被授予的权限
show grant user zhangsan on database default
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HIVE 大数据