hive和presto一些好用的自定义udf函数   2016-07-28


自己编写了一些hive和presto的udf函数,最近在github上开源了(可以查看hive-third-functionspresto-third-functions)。有需要的人可以直接下来用,项目README中有详细的用法说明。如果有好的建议或发现了bug,可以在issue提问题。也欢迎加入自己的udf,完善这个第三方函数库。

一、hive-third-functions中包含的函数

可以参考项目说明中的示例来理解这些函数。

1. 字符串函数

函数 描述
pinyin(string) -> string 将汉字转换为拼音
md5(string) -> string md5 哈西函数
sha256(string) -> string sha256 哈西函数

2. 数组函数

函数 描述
array_contains(array<E>, E) -> boolean 判断元素是否在array中.
array_intersect(array, array) -> array 返回两个array的交集.
array_max(array<E>) -> E 返回array中的元素最大值.
array_min(array<E>) -> E 返回array中的元素最小值.
array_join(array, delimiter, null_replacement) -> string 使用delimiter指定的值来连接array中的元素,null_replacement是可选项,用来替代null,返回string.
array_distinct(array) -> array 移除array中的重复元素
array_position(array<E>, E) -> long 返回array中指定值第一次出现的位置,初始值为1,如果没找到返回0.
array_remove(array<E>, E) -> array 移除array中所有指定的值.
array_reverse(array) -> array 翻转array的元素.
array_sort(array) -> array 对array中的元素进行排序,array中的元素必须是可排序的.
array_concat(array, array) -> array 连接两个array.
array_value_count(array<E>, E) -> long 统计array中指定元素出现的次数.
array_slice(array, start, length) -> array 求array的子数组。从start开始,截取length个。如果start为负值,则从元素末尾开始。如-1表示从最后一个元素开始。
array_element_at(array<E>, index) -> E 返回array中第index个位置的元素,如果index为负值,则从数组最后开始。例如-1表示最后一个元素.

3. map函数

函数 描述
map_build(x<K>, y<V>) -> map<K, V> 根据指定的key/value数组来创建map.
map_concat(x<K, V>, y<K, V>) -> map<K,V> 连接两个map,如果一个key在 xy 中都出现了,那合并后的map中该key的值是其在y 中的值.
map_element_at(map<K, V>, key) -> V 返回给定key 的值,如果不存在,返回NULL.
map_equals(x<K, V>, y<K, V>) -> boolean 判断两个map是否相等.

4. 日期函数

函数 描述
day_of_week(date_string | date) -> int 一周的第几天,星期一返回1,星期二返回2,依此类推。
day_of_year(date_string | date) -> int 一年的第几天.
zodiac_en(date_string | date) -> string 将日期转换为星座英文
zodiac_cn(date_string | date) -> string 将日期转换为星座中文
type_of_day(date_string | date) -> string 获取日期的类型(1: 法定节假日, 2: 正常周末, 3: 正常工作日 4:攒假的工作日),错误返回-1.

5. json函数

函数 描述
json_array_get(json, jsonPath) -> array(varchar) returns the element at the specified index into the json_array. The index is zero-based.
json_array_length(json, jsonPath) -> array(varchar) returns the array length of json (a string containing a JSON array).
json_array_extract(json, jsonPath) -> array(varchar) extract json array by given jsonPath.
json_array_extract_scalar(json, jsonPath) -> array(varchar) like json_array_extract, but returns the result value as a string (as opposed to being encoded as JSON).
json_extract(json, jsonPath) -> array(varchar) extract json by given jsonPath.
json_extract_scalar(json, jsonPath) -> array(varchar) like json_extract, but returns the result value as a string (as opposed to being encoded as JSON).
json_size(json, jsonPath) -> array(varchar) like json_extract, but returns the size of the value. For objects or arrays, the size is the number of members, and the size of a scalar value is zero.

6. bitwise函数

函数 描述
bit_count(x, bits) -> bigint count the number of bits set in x (treated as bits-bit signed integer) in 2’s complement representation
bitwise_and(x, y) -> bigint returns the bitwise AND of x and y in 2’s complement arithmetic.
bitwise_not(x) -> bigint returns the bitwise NOT of x in 2’s complement arithmetic.
bitwise_or(x, y) -> bigint returns the bitwise OR of x and y in 2’s complement arithmetic.
bitwise_xor(x, y) -> bigint returns the bitwise XOR of x and y in 2’s complement arithmetic.

7. 身份证函数

函数 描述
id_card_province(string) -> string 获取省份
id_card_city(string) -> string 获取城市
id_card_area(string) -> string 获取区或县
id_card_birthday(string) -> string 获取出生日期
id_card_gender(string) -> string 获取性别
is_valid_id_card(string) -> boolean 鉴定是否是有效的身份证号码.
id_card_info(string) -> json 获取上述所有信息,结果为json.

8. 坐标相关函数

函数 说明
wgs_distance(double lat1, double lng1, double lat2, double lng2) -> double 计算WGS84坐标系下的坐标距离,单位为米
gcj_to_bd(double,double) -> json 火星坐标系(GCJ-02)转百度坐标系(BD-09),谷歌、高德——>百度
bd_to_gcj(double,double) -> json 百度坐标系(BD-09)转火星坐标系(GCJ-02),百度——>谷歌、高德
wgs_to_gcj(double,double) -> json WGS84转GCJ02(火星坐标系)
gcj_to_wgs(double,double) -> json GCJ02(火星坐标系)转GPS84,输出的WGS-84坐标精度为1米到2米之间。
gcj_extract_wgs(double,double) -> json GCJ02(火星坐标系)转GPS84,输出的WGS-84坐标精度为0.5米内。但是计算速度慢于gcj_to_wgs

二、presto-third-functions中包含的函数

1. 字符串相关函数

函数 说明
pinyin(string) -> string 将汉字转为拼音
md5(string) -> string 对字符串求md5值
sha256(string) -> string 对字符串求sha256值

2. 日期相关函数

函数 说明
dayofweek(date_string | date) -> int 计算给定日期是每周7天内的第几天,其中周一返回1,周天返回7,错误返回-1.
zodiac(date_string | date) -> string 将日期转换为星座英文
zodiac_cn(date_string | date) -> string 将日期转换为星座中文
typeofdate(date_string | date) -> string 获取日期的类型(1: 法定节假日, 2: 正常周末, 3: 正常工作日 4:攒假的工作日),错误返回-1.

3. 数组相关函数

函数 说明
array_union(array, array) -> array 求两个array的并集
value_count(array(T), T value) -> int 统计在数组中值为给定值的元素个数

4. JSON相关函数

函数 说明
json_array_extract(json, jsonPath) -> array(varchar) 提取json数组中对应路径的值
json_array_extract_scalar(json, jsonPath) -> array(varchar) json_array_extract类似,但是返回结果是string(不是json格式)

5. MAP相关函数

函数 说明
value_count(MAP(K,V), V value) -> int 统计中MAP中值为给定值的元素的个数

6. 身份证相关函数

函数 说明
id_card_province(string) -> string 由身份证号获取省份
id_card_city(string) -> string 由身份证号获取城市
id_card_area(string) -> string 由身份证号获取区或县
id_card_birthday(string) -> string 由身份证号获取出生日期
id_card_gender(string) -> string 由身份证号获取性别
is_valid_id_card(string) -> boolean 鉴别是否是有效的身份证号
id_card_info(string) -> json 获取身份证号对应的信息,包括省份,城市,区县,性别及是否有效

7. 坐标相关函数

函数 说明
wgs_distance(double lat1, double lng1, double lat2, double lng2) -> double 计算WGS84坐标系下的坐标距离,单位为米
gcj_to_bd(double,double) -> json 火星坐标系(GCJ-02)转百度坐标系(BD-09),谷歌、高德——>百度
bd_to_gcj(double,double) -> json 百度坐标系(BD-09)转火星坐标系(GCJ-02),百度——>谷歌、高德
wgs_to_gcj(double,double) -> json WGS84转GCJ02(火星坐标系)
gcj_to_wgs(double,double) -> json GCJ02(火星坐标系)转GPS84,输出的WGS-84坐标精度为1米到2米之间。
gcj_extract_wgs(double,double) -> json GCJ02(火星坐标系)转GPS84,输出的WGS-84坐标精度为0.5米内。但是计算速度慢于gcj_to_wgs

分享到:


  如果您觉得这篇文章对您的学习很有帮助, 请您也分享它, 让它能再次帮助到更多的需要学习的人. 您的支持将鼓励我继续创作 !
本文基于署名4.0国际许可协议发布,转载请保留本文署名和文章链接。 如您有任何授权方面的协商,请邮件联系我。

目录

  1. 1. 一、hive-third-functions中包含的函数
    1. 1.1. 1. 字符串函数
    2. 1.2. 2. 数组函数
    3. 1.3. 3. map函数
    4. 1.4. 4. 日期函数
    5. 1.5. 5. json函数
    6. 1.6. 6. bitwise函数
    7. 1.7. 7. 身份证函数
    8. 1.8. 8. 坐标相关函数
  2. 2. 二、presto-third-functions中包含的函数
    1. 2.1. 1. 字符串相关函数
    2. 2.2. 2. 日期相关函数
    3. 2.3. 3. 数组相关函数
    4. 2.4. 4. JSON相关函数
    5. 2.5. 5. MAP相关函数
    6. 2.6. 6. 身份证相关函数
    7. 2.7. 7. 坐标相关函数