设为首页 加入收藏

TOP

Hive学习之路(四):Hive内置函数介绍与实现WordCount
2019-04-17 13:02:43 】 浏览:247
Tags:Hive 习之 :Hive 内置 函数 介绍 实现 WordCount
版权声明:原创不易,转载请声明出处,谢谢! https://blog.csdn.net/qq_41955099/article/details/89323027

一、Hive内置函数介绍

Hive为编程人员提供了大量的内置函数,为编程提供了极大的便利,进入hive shell下输入命令:show functions可查看所有Hive所有的内置函数:
在这里插入图片描述
以上函数大致可以分为:数值计算函数、字符串操作函数、聚合函数等等,下面对常用的Hive内置函数做一下介绍。

二、Hive常用内置函数介绍

1.数值计算函数

函数表达式 函数功能 返回值类型
round(Double a) 返回对a四舍五入的Double值 Double
round(Double a, Int d) 返回对a四舍五入的Double值 ,保留d位小数 Double
floor(Double a) 对a向下取整 Double
ceil(Double a) 对a向上取整 Double
rand(Int seed) 返回一个0到1范围内的随机数,seed是随机因子,可以不填 Double
pow(Double a, Double p) 返回a的p次幂 Double
sqrt(Double a) 返回a的平方根 Double
abs(Double a) 返回a的绝对值 Double
pmod(Double a, Double b) 返回a对b取模 Double

2.字符串操作函数

函数表达式 函数功能 返回值类型
length(string A) 返回字符串A的长度 Int
substr(string A,Int start,Int len) 返回字符串A从start位置开始截取长度为len的字符串 string
split(string str, string pat) 返回按照正则表达式pat来分割字符串str的数组 array
reverse(string A) 返回字符串A的反转串 string
concat(string A, string B,…) 返回字符串按次序进行拼接的串 string
lower(string A) 返回字符串A的所有字母转换成小写字母的串 string
upper(string A) 返回字符串A的所有字母转换成大写字母的串 string
rtrim(string A) 返回去掉字符串A后面出现的空格的串 string

3.日期函数

函数表达式 函数功能 返回值类型
current_date 返回当前时间日期 string
year(string date) 返回日期的年部分 string
month(string date) 返回日期的月部分 string
day(string date) 返回日期的日部分 string
weekofyear(string date) 返回日期位于该年的第几个星期 Int
datediff(string enddate, string startdate) 返回始时间startdate到结束时间enddate相差的天数 Int
date_add(string startdate, int days) 返回开始时间startdate加上days的时间 string
date_sub(string startdate, int days) 返回开始时间startdate减去days的时间 string
from_unixtime(BigInt second,string format) 返回时间的秒值转换成format格式 string

4.聚合函数

函数表达式 函数功能 返回值类型
count(*) 返回表的总行数 BigInt
sum(col) 返回表指定列的和 Double
sum(DISTINCT col) 返回表的去重后的列的和 Double
avg(col) 返回表的指定列的平均值 Double
avg(DISTINCT col) 返回表的指定列去重后的平均值 Double
min(col) 返回表指定列的最小值 Double
max(col) 返回表指定列的最大值 Double

5.表生成函数

函数表达式 函数功能 返回值类型
explode(array) 返回每行对应数组中的一个元素 N rows
explode(map) 返回每行对应每个map键-值,其中一个字段是map的键,另一个字段是map的值 N rows

三、使用Hive函数完成WordCount

1.创建表并将数据导入

在hive shell在输入命令:create table wordcount(line string),表的字段只有一个,就是每行未分割的单词,创建完成后,执行命令:load data local inpath' /home/hadoop/data/testdata' into table wordcount将下面的数据testdata导入:

hello world hello hadoop
hello spark hello hbase
hello hive hello hadoop
hello kafka hello flume
hello flink hello sqoop
hello spark hello hbase
hello kafka hello flume
hello spark hello hbase

执行命令:select * from wordcount查看数据:
在这里插入图片描述
至此,数据准备完成。

2.编写SQL句并执行

wordcount表中,每一行就是一个string,所以我们首先需要使用函数split对每行以空格切割,得到一个字符串数组,然后使用函数explode将数组拆开,返回每行一个字符将其命别名为word,对上述结果建立临时表w,最后对表w分组聚合,便完成了WordCount操作,完整SQL语句如下:
select word ,count(1) from (select explode(split(line," ")) as word from wordcount) w group by word,执行结果如下:
在这里插入图片描述

四、总结

本篇首先介绍了Hive的内置函数,并对于常用的内置函数分类做了较为详细的介绍,最后动手使用Hive的内置函数完成了WordCount,可以发现,使用Hive完成WordCount远比手写MapReduce代码要简洁得多,这也是Hive优势之所在。感谢您的阅读~如有错误请不吝赐教!

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Hive踩坑之The specified datasto.. 下一篇执行Hive语句报错:FAILED: Error..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目