hive常用函数

来源:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF


Complex Type
Constructors


The following functions construct instances of complex types.



























Constructor Function

Operands

Description

map

(key1, value1, key2, value2, ...)

Creates a map with the given key/value pairs

struct

(val1, val2, val3, ...)

Creates a struct with the given field values. Struct field names will
be col1, col2, ...

named_struct

(name1, val1, name2, val2, ...)

Creates a struct with the given field names and values. (as of Hive 0.8.0)

array

(val1, val2, ...)

Creates an array with the given elements

create_union

(tag, val1, val2, ...)

Creates a union type with the value that is being pointed to by the tag
parameter

Date Functions


The following built-in date functions are supported in hive:











































































Return Type

Name(Signature)

Description

string

from_unixtime(bigint unixtime[, string format])

Converts the number of seconds from unix epoch (1970-01-01 00:00:00
UTC) to a string representing the timestamp of that moment in the current
system time zone in the format of "1970-01-01 00:00:00"

bigint

unix_timestamp()

Gets current Unix timestamp in seconds

bigint

unix_timestamp(string date)

Converts time string in format yyyy-MM-dd HH:mm:ss to Unix
timestamp (in seconds), using the default timezone and the default locale,
return 0 if fail: unix_timestamp(‘2009-03-20 11:30:01‘) = 1237573801

bigint

unix_timestamp(string date, string pattern)

Convert time string with given pattern (see [http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html])
to Unix time stamp (in seconds), return 0 if fail:
unix_timestamp(‘2009-03-20‘, ‘yyyy-MM-dd‘) = 1237532400

string

to_date(string timestamp)

Returns the date part of a timestamp string: to_date("1970-01-01
00:00:00") = "1970-01-01"

int

year(string date)

Returns the year part of a date or a timestamp string: year("1970-01-01
00:00:00") = 1970, year("1970-01-01") = 1970

int

month(string date)

Returns the month part of a date or a timestamp string:
month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11

int

day(string date) dayofmonth(date)

Return the day part of a date or a timestamp string: day("1970-11-01
00:00:00") = 1, day("1970-11-01") = 1

int

hour(string date)

Returns the hour of the timestamp: hour(‘2009-07-30 12:58:59‘) = 12,
hour(‘12:58:59‘) = 12

int

minute(string date)

Returns the minute of the timestamp

int

second(string date)

Returns the second of the timestamp

int

weekofyear(string date)

Return the week number of a timestamp string: weekofyear("1970-11-01
00:00:00") = 44, weekofyear("1970-11-01") = 44

int

datediff(string enddate, string startdate)

Return the number of days from startdate to enddate:
datediff(‘2009-03-01‘, ‘2009-02-27‘) = 2

string

date_add(string startdate, int days)

Add a number of days to startdate: date_add(‘2008-12-31‘, 1) =
‘2009-01-01‘

string

date_sub(string startdate, int days)

Subtract a number of days to startdate: date_sub(‘2008-12-31‘, 1) =
‘2008-12-30‘

timestamp

from_utc_timestamp(timestamp, string timezone)

Assumes given timestamp is UTC and converts to given timezone (as of
Hive 0.8.0)

timestamp

to_utc_timestamp(timestamp, string timezone)

Assumes given timestamp is in given timezone and converts to UTC (as of
Hive 0.8.0)

String Functions


The following built-in String functions are supported in hive:



























































































































































Return Type

Name(Signature)

Description

int

ascii(string str)

Returns the numeric value of the first character of str

string

base64(binary bin)

Convert the argument from binary to a base 64 string (as of Hive 0.12.0)

string

concat(string|binary A, string|binary B...)

Returns the string or bytes resulting from concatenating the strings or
bytes passed in as parameters in order. e.g. concat(‘foo‘, ‘bar‘) results
in ‘foobar‘. Note that this function can take any number of input
strings.

array<struct<string,double>>

context_ngrams(array<array<string>>, array<string>,
int K, int pf)

Returns the top-k contextual N-grams from a set of tokenized sentences,
given a string of "context". See StatisticsAndDataMining
for more information.

string

concat_ws(string SEP, string A, string B...)

Like concat() above, but with custom separator SEP.

string

concat_ws(string SEP, array<string>)

Like concat_ws() above, but taking an array of strings. (as of Hive 0.9.0)

string

decode(binary bin, string charset)

Decode the first argument into a String using the provided character
set (one of ‘US_ASCII‘, ‘ISO-8859-1‘, ‘UTF-8‘, ‘UTF-16BE‘, ‘UTF-16LE‘,
‘UTF-16‘). If either argument is null, the result will also be null. (as
of Hive 0.12.0)

binary

encode(string src, string charset)

Encode the first argument into a BINARY using the provided character
set (one of ‘US_ASCII‘, ‘ISO-8859-1‘, ‘UTF-8‘, ‘UTF-16BE‘, ‘UTF-16LE‘,
‘UTF-16‘). If either argument is null, the result will also be null. (as
of Hive 0.12.0)

int

find_in_set(string str, string strList)

Returns the first occurance of str in strList where strList is a
comma-delimited string. Returns null if either argument is null. Returns 0
if the first argument contains any commas. e.g. find_in_set(‘ab‘,
‘abc,b,ab,c,def‘) returns 3

string

format_number(number x, int d)

Formats the number X to a format like ‘#,###,###.##‘, rounded to D
decimal places, and returns the result as a string. If D is 0, the result
has no decimal point or fractional part. (as of Hive 0.10.0)

string

get_json_object(string json_string, string path)

Extract json object from a json string based on json path specified,
and return json string of the extracted json object. It will return null
if the input json string is invalid. NOTE: The json path can only
have the characters [0-9a-z_], i.e., no upper-case or special characters.
Also, the keys *cannot
start with numbers.* This is due to
restrictions on Hive column names.

boolean

in_file(string str, string filename)

Returns true if the string str appears as an entire line in
filename.

int

instr(string str, string substr)

Returns the position of the first occurrence of substr in
str. Returns null if either of the arguments are
null and returns 0 if substr could
not be found in str. Be aware that this is not zero based.
The first character in str has index 1.

int

length(string A)

Returns the length of the string

int

locate(string substr, string str[, int pos])

Returns the position of the first occurrence of substr in str after
position pos

string

lower(string A) lcase(string A)

Returns the string resulting from converting all characters of B to
lower case e.g. lower(‘fOoBaR‘) results in ‘foobar‘

string

lpad(string str, int len, string pad)

Returns str, left-padded with pad to a length of len

string

ltrim(string A)

Returns the string resulting from trimming spaces from the
beginning(left hand side) of A e.g. ltrim(‘ foobar ‘) results in ‘foobar

array<struct<string,double>>

ngrams(array<array<string>>, int N, int K, int pf)

Returns the top-k N-grams from a set of tokenized sentences, such as
those returned by the sentences() UDAF. See StatisticsAndDataMining
for more information.

string

parse_url(string urlString, string partToExtract [, string
keyToExtract])

Returns the specified part from the URL. Valid values for partToExtract
include HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
e.g. parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1‘,
‘HOST‘) returns ‘facebook.com‘. Also a value of a particular key in QUERY
can be extracted by providing the key as the third argument, e.g.
parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1‘, ‘QUERY‘,
‘k1‘) returns ‘v1‘.

string

printf(String format, Obj... args)

Returns the input formatted according do printf-style format strings
(as of Hive 0.9.0)

string

regexp_extract(string subject, string pattern, int index)

Returns the string extracted using the pattern. e.g.
regexp_extract(‘foothebar‘, ‘foo(.*?)(bar)‘, 2) returns ‘bar.‘ Note that
some care is necessary in using predefined character classes: using ‘\s‘
as the second argument will match the letter s; ‘

s‘ is necessary to match whitespace, etc. The ‘index‘ parameter is the
Java regex Matcher group() method index. See
docs/api/java/util/regex/Matcher.html for more information on the ‘index‘
or Java regex group() method.

string

regexp_replace(string INITIAL_STRING, string PATTERN, string
REPLACEMENT)

Returns the string resulting from replacing all substrings in
INITIAL_STRING that match the java regular expression syntax defined in
PATTERN with instances of REPLACEMENT, e.g. regexp_replace("foobar",
"oo|ar", "") returns ‘fb.‘ Note that some care is necessary in using
predefined character classes: using ‘\s‘ as the second argument will match
the letter s; ‘
s‘ is necessary to match
whitespace, etc.

string

repeat(string str, int n)

Repeat str n times

string

reverse(string A)

Returns the reversed string

string

rpad(string str, int len, string pad)

Returns str, right-padded with pad to a length of len

string

rtrim(string A)

Returns the string resulting from trimming spaces from the end(right
hand side) of A e.g. rtrim(‘ foobar ‘) results in ‘ foobar‘

array<array<string>>

sentences(string str, string lang, string locale)

Tokenizes a string of natural language text into words and sentences,
where each sentence is broken at the appropriate sentence boundary and
returned as an array of words. The ‘lang‘ and ‘locale‘ are optional
arguments. e.g. sentences(‘Hello there! How are you?‘) returns ( ("Hello",
"there"), ("How", "are", "you") )

string

space(int n)

Return a string of n spaces

array

split(string str, string pat)

Split str around pat (pat is a regular expression)

map<string,string>

str_to_map(text[, delimiter1, delimiter2])

Splits text into key-value pairs using two delimiters. Delimiter1
separates text into K-V pairs, and Delimiter2 splits each K-V pair.
Default delimiters are ‘,‘ for delimiter1 and ‘=‘ for
delimiter2.

string

substr(string|binary A, int start) substring(string|binary A, int
start)

Returns the substring or slice of the byte array of A starting from
start position till the end of string A e.g. substr(‘foobar‘, 4) results
in ‘bar‘ (see [http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr])

string

substr(string|binary A, int start, int len) substring(string|binary A,
int start, int len)

Returns the substring or slice of the byte array of A starting from
start position with length len e.g. substr(‘foobar‘, 4, 1) results in ‘b‘
(see [http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr])

string

translate(string input, string from, string to)

Translates the input string by replacing the characters present in the
from string with the corresponding characters in the
to string. This is similar to the translate
function in PostgreSQL. If any of the parameters to this UDF are
NULL, the result is NULL as well (available as of Hive 0.10.0)

string

trim(string A)

Returns the string resulting from trimming spaces from both ends of A
e.g. trim(‘ foobar ‘) results in ‘foobar‘

binary

unbase64(string str)

Convert the argument from a base 64 string to BINARY (as of Hive 0.12.0)

string

upper(string A) ucase(string A)

Returns the string resulting from converting all characters of A to
upper case e.g. upper(‘fOoBaR‘) results in
‘FOOBAR‘

时间: 2024-10-11 04:27:57

hive常用函数的相关文章

orcale和hive常用函数对照表(?代表未证实)

函数分类 oracle hive 说明 字符函数 upper('coolszy') upper(string A) ucase(string A) 将文本字符串转换成字母全部大写形式 lower('KUKA') lower(string A) lcase(string A) 将文本字符串转换成字母全部小写形式 initcap('kuKA aBc') 无  将每个单词的首字母大写,其他位置的字母小写 concat('Hello',' world') concat(string A, string 

hive 常用UDF

Hive UDF整理 (可以直接在mysql上测试,hive中没有伪表,需要手动创建,反应慢)字符串函数 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length(‘abcedfg’) from dual; 7 字符串反转函数:reverse 语法: reverse(string A) 返回值: string 说明:返回字符串A的反转结果 举例: hive> select reverse(

Hive常用语句

1Hive简介 Hive对我来说就是一个基于HDFS的数据仓库,它提供了一个种类SQL语言(和SQL标准基本一样又有一些特殊的地方不一样),能让不精通Java语言而熟悉SQL语言的工程师,快速的对HDFS或其他存储文件系统如Amazon,S3,上的数据进行数据分析,是Hadoop生态系统中非常重要的一个工具.对于大数据分析师而言,HiveQL则是必须要掌握的一个工具. 2.Hive常用语句 2.1菜鸟建表法 1.直接建表,指定分隔符,默认存储为text,也可以指定存储格式! create tab

hive之函数

第一节:内置函数 一.显示内置函数列表 show functions: 默认271个 二.查看函数的基本使用 desc function funname; desc function max; 三.查看函数的详细使用教程 desc function extended funname; desc function extended max; 四.函数分类 1.UDF USER DEFINE FUNCTION 用户定义函数,进一路出一路 2.UDAF USER DEFINE aggregate FU

hive自定义函数UDF UDTF UDAF

Hive 自定义函数 UDF UDTF UDAF 1.UDF:用户定义(普通)函数,只对单行数值产生作用: UDF只能实现一进一出的操作. 定义udf 计算两个数最小值 public class Min extends UDF { public Double evaluate(Double a, Double b) { if (a == null) a = 0.0; if (b == null) b = 0.0; if (a >= b) { return b; } else { return a

Data Frame的常用函数

1.DataFrame的常用函数: (1)np.abs(frame) 绝对值, (2)apply function, lambda f= lambda x: x.max()-x.min(),frame.apply(f); frame.apply(f,axis = 1) f(x), def f(x): return Series([x.min(),x.max()], index=['min','max']),frame.apply(f)(3) applymap format f= lambda x

Oracle SQL语言之常用函数_超越OCP精通Oracle视频教程培训30

Oracle SQL语言之常用函数_超越OCP精通Oracle视频教程培训30 本课程介绍: Oracle视频教程,风哥本套oracle教程培训是<<Oracle数据库SQL语言实战培训教程>>的第5/5套:Oracle SQL语言之常用函数.主要学习Oracle数据库SQL聚合函数,分组函数,字符函数,转换函数,日期字符数字转换,日期函数,集合函数,分析函数等. Oracle SQL语言之常用函数,课程内容详细如下: 聚合函数-数据统计 分组函数-使用group by与havin

MySQL学习笔记(三)——计算字段及常用函数

拼接字段-Concat()函数        将值连接在一起构成单个值.注意:大多数DBMS使用+或者||来实现拼接,mysql则使用Concat()函数来实现. 去空格函数-Trim函数        Trim去掉串左右两边的空格,RTrim去掉串右边的空格,LTrim去掉左边的空格. 使用别名-AS        我们希望查出的新列能有个简介明了的列名,可以用AS来赋予别名. 执行算术运算(+,-,*,/) 大多数sql实现支持以下类型的函数: 1.用于处理文本串(如删除或填充值,转换值为大

162个php常用函数基础用法(个人整理)

PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 浮点数进一取整 3.floor(): 舍去法取整 echo floor(9.999); // 9 浮点数直接舍去小数部分 4.fmod(): 浮点数取余 ? 1 2 3 4 $x = 5.7; $y = 1.3; // 两个浮点数,x>y 浮点余数 $r = fmod($x, $y); // $r