代码示例讲解Hive分区分桶以及自定义函数

导入数据:

1、

load data local inpath '/root/tes.txt' into table test.usr;

  将本地的数据导入到hive中

2、从hdfs集群导入数据

load data inpath 'hdfs://node01:9000/user/tes.txt' into table test.te; LOAD DATA命令,可分为LOAD DATA LOCAL INPATH和LOAD DATA INPATH。两者的区别在于LOCAL导入的是本地文件而不加LOCAL的导入的是HDFS文件---相当于直接将文件进行相应的上传

3、insert into---内外部表,不适应于分区

4、

from table1 insert into(overwrite) tables2 select id ,name

分区表

Hive 分区partition(分成不同的文件目录进行存储)

1、静态分区:

必须在表定义时指定对应的partition字段-----分区字段一定不能与表中字段重复

a、单分区建表语句:

create table day_table (id int, content string) partitioned by (dt int);

上传数据: load data local inpath '/root/tes.txt' into table test.usr partition (age=10);

单分区表,按天分区,在表结构中存在id,content,dt三列。

以dt为文件夹区分

粗细力度分区的时候要根据业务需求,提前进行相应的设定 年月日时分秒----为了减少每一个分区中的内容,提高计算效率

b、 双分区建表语句:

create table hour(id int, content string) partitioned by (dt int, hour int);

双分区表,按天和小时分区,在表结构中新增加了dt和hour两列。

先以dt为文件夹,再以hour子文件夹区分

增加分区

alter table hour add partition(dt=10,hour=40);

alert table tablename add partiton(dt=20,hour=40)

也就是说添加分区的时候不能直接添加,而是需要将原来的分区也要包含其中,完成相应的排序

删除分区

alter table tablename drop partition (sex='boy')

alert table tablename drop partiton(dt=20,hour=40)

注:删除分区的时候,会将所有存在的分区都删除

2、动态分区:

修改权限的方式:

1、conf/hive-site.xml

2、在hive内部使用set进行相应的设置

3、hive启动的时候设置 hive --conf hive.exec.dynamic.partiton=true

修改权限的方法

1、修改权限

set hive.exec.dynamic.partiton=true //开启动态分区

2、修改默认状态

set hive.exec.dynamic.partiton.mode=nostrict //默认strict。至少有一个静态分区

创建分区表:

create table psn22( id int, name string, likes array<String>, address map<string ,string> ) partitioned by (age int ,sex string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ’,’ COLLECTION ITEMS TERMINATED BY ‘,’ MAP KEYS TERMINATED BY ‘:’ LINES TERMINATED BY ‘\t’

写入数据

from psn21 //已经存在的表格并且要有数据 insert overwrite table pas22 partiton (age,sex) select * distribute by age,sex

分桶表:

测试数据

1,tom,11

开启分桶

set hive.enforce.bucketing=true

创建桶

create table psnbucket1 ( id int, name string, age int) clustered by (age) into 4 buckets row format delimited fields terminated by ','

加载数据

insert into table psnbucket select id,name,age from psn31

抽样

select * from bucket_table tablesample(bucket 1 out of 4 by colimes)

自定义函数

UDF:一对一

	1、继承UDF

	2、重写evaluate

	(实现传入的参数,并且封装了很多的方法)

public class AddPrefix extends UDF { /**

  • 这里我们实现将任一输入添加自定义前缀信息 */ public String evaluate(String str) { return "HIVE UDF Prefix:"+ str; } }
UDAF:多对一

UDTF:一对多


1、创建udf自定义函数

2、达成jar包并上传到linux集群

3、将集群中的jar包上传到hive中:add jar /opt/software/jars/UDF.jar;

4、创建属于我自己的函数	

create temporary function bws as "com.hpe.TestUdf.TestUdf"; 5、使用函数执行

但是在创建自定义函数的时候,一般都是创建的临时函数,推出就木有了,那怎么创建永久函数呢?

注册永久hive自定义函数

add jar的方式只是导入临时的jar文件,所以不能创建永久的自定义函数,想要添加永久的自定义函数需要在配置文件中对jar的引入进行修改



在hive-site.xml文件中添加

<property> <name>hive.aux.jars.path</name> <value>file:///opt/module/hive/lib/app_logs_hive.jar</value> </property>

注意:value中的值为你的udf.jar上传到linux的指定路径下,若是多个jar包,以,(逗号)进行分割

知识大家一起学习,关注Java架构师联盟微信公众号,在后台回复关键字可以获取更多栈长整理的Java架构技术干货

发布了78 篇原创文章 · 获赞 1 · 访问量 1256

猜你喜欢

转载自blog.csdn.net/weixin_42864905/article/details/105497896