Hive之系统内置函数 ,自定义UDF函数 ,JSON 解析函数 (7)

一  系统内置函数

1  查看系统自带的函数

hive> show  functions;

2  显示自带的函数的用法

hive> desc function upper;

3  详细显示自带的函数的用法

hive > desc  function extended upper;

二  自定义 UDF 函数

1  自定义临时的UDF函数

1.1   创建一个 Maven 工程 Hive

1.2  导入依赖

<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.2.1</version>
		</dependency>
</dependencies>

1.3  创建一个类

package com.doit.hive;
import org.apache.hadoop.hive.ql.exec.UDF;

public class Lower extends UDF {

	public String evaluate (final String s) {
	
		if (s == null) {
			return null;
		}
		return s.toLowerCase();
	}
}

1.4  打成 jar 包上传到服务器 /opt/module/jars/udf.jar

1.5  将 jar 包添加到 hive 的 classpath

hive (default)> add jar /opt/module/datas/udf.jar;

1.6  创建临时函数与开发好的 java class 关联

hive (default)> create temporary function mf as "com.hive.funcs.MyFunction";

1.7  即可在 hql 中使用自定义的函数 strip

hive (default)> select ename, mylower(ename) lowername from emp;

2  永久函数的创建和使用 :

2.1   把自定义函数的 jar 包上传到 hdfs 上

hdfs dfs -put lower.jar 'hdfs:///path/to/hive_func';

2.2   创建永久函数

hive> create function xxoo_lower as 'com.hive.func.MyFunction'  using  jar 'hdfs:///path/to/hive_func/lower.jar'

2.3   验证

hive> select xxoo_lower("Hello World");
hive> show functions;

2.4   永久函数的删除

hive> drop function xxoo_lower;

 json 解析函数 : 表生成函数

有以下 json 格式的电影评分数据 :

{"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
{"movie":"661","rate":"3","timeStamp":"978302109","uid":"1"}
{"movie":"914","rate":"3","timeStamp":"978301968","uid":"1"}
{"movie":"3408","rate":"4","timeStamp":"978300275","uid":"1"}
{"movie":"2355","rate":"5","timeStamp":"978824291","uid":"1"}
{"movie":"1197","rate":"3","timeStamp":"978302268","uid":"1"}

需要做出各种统计分析. 但是发现对 json 做 sql 查询不方便 ,需要将 json 数据解析成普通的结构化数据 ,可以采用 hive 中内置的

json_tuple() 函数 .

需求实现思路和步骤如下 : 

1   创建一个原始表用来对应原始的 json 数据

create table t_json(json string);

load data local inpath ‘/root/rating.json’ into table t_json;

2   利用 json_tuple 进行 json 数据解析

--------真正解析整张 json 表 ,将解析结果数据插入一张新表 
create table t_movie_rate
as
select json_tuple(json,'movie','rate','timeStamp','uid') as(movie,rate,ts,uid) from t_json;
                  ---这里是需要解析的 json 数据         ---这里是命名的字段,需要一一对应

3  需求实操 :

3.1   统计每部电影的平均得分

create table tb_movie_avg_rate as

select

movie,

avg(rate)

from 

t_movie_rate

group by

movie;

3.2   统计数据中一共有多少部电影

select  count(movie)  from   t_movie_rate   group by  movie;

select count(distinct  movie) from t_movie_rate;

3.3   统计每部电影的被评分次数(热门度)

create table tb_rate_cnt  as

select

movie ,

count(1)  as  cnt

from

t_movie_rate

group by  movie

order by  cnt desc;

3.4   统计每个人的所有电影评分的平均分

create table tb_rate_avg_uid  as

select 

uid,

avg(rate)

from

t_movie_rate

group by uid;

3.5   查询出每个人评分最高的20部电影

利用 json_tuple 从原始 json 数据表中 ,etl 出一个详细信息表

create table t_rate as 
select 
uid,
movie,
rate,
year(from_unixtime(cast(ts as bigint))) as year,
month(from_unixtime(cast(ts as bigint))) as month,
day(from_unixtime(cast(ts as bigint))) as day,
hour(from_unixtime(cast(ts as bigint))) as hour,
minute(from_unixtime(cast(ts as bigint))) as minute,
from_unixtime(cast(ts as bigint)) as ts
from 
(select 
json_tuple(rateinfo,'movie','rate','timeStamp','uid') as(movie,rate,ts,uid)
from t_json) tmp
;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值