手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Hive基础学习文档和入门教程

[日期:2013-10-18] 来源:didu.me  作者:Linux [字体:大 中 小]

5. HIVE参数设置

开发Hive应用时,不可避免地需要设定Hive的参数。设定Hive的参数可以调优HQL代码的执行效率,或帮助定位问题。然而实践中经常遇到的一个问题是,为什么设定的参数没有起作用?

这通常是错误的设定方式导致的。

对于一般参数,有以下三种设定方式:

  • 配置文件
  • 命令行参数
  • 参数声明

配置文件:Hive的配置文件包括

  • 用户自定义配置文件:$HIVE_CONF_DIR/hive-site.xml
  • 默认配置文件:$HIVE_CONF_DIR/hive-default.xml

用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hadoop的配置文件包括

  • $HADOOP_CONF_DIR/hive-site.xml
  • $HADOOP_CONF_DIR/hive-default.xml

Hive的配置会覆盖Hadoop的配置。

配置文件的设定对本机启动的所有Hive进程都有效。

命令行参数:启动Hive(客户端或Server方式)时,可以在命令行添加-hiveconf param=value来设定参数,例如:

bin/hive -hiveconf hive.root.logger=INFO,console

这一设定对本次启动的Session(对于Server方式启动,则是所有请求的Sessions)有效。

参数声明:可以在HQL中使用SET关键字设定参数,例如:

set mapred.reduce.tasks=100;

这一设定的作用域也是Session级的。

上述三种设定方式的优先级依次递增。即参数声明覆盖命令行参数,命令行参数覆盖配置文件设定。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在Session建立以前已经完成了。

另外,SerDe参数[王黎14] 必须写在DDL(建表)语句中。例如:

create table if not exists t_dummy(

dummy string

)

ROW FORMAT SERDE’org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe’

WITH SERDEPROPERTIES (

‘field.delim’='\t’,

‘escape.delim’='\\’,

‘serialization.null.format’=’ ‘

) STORED AS TEXTFILE;

类似serialization.null.format这样的参数,必须和某个表或分区关联。在DDL外部声明将不起作用。

6. HIVE UDF

6.1 基本函数

SHOW FUNCTIONS;

DESCRIBE FUNCTION <function_name>;

6.1.1 关系操作符

Operator Operand types Description
A = B All primitive types TRUE if expression A is equal to expression B otherwise FALSE
A == B None! Fails because of invalid syntax. SQL uses =, not ==
A <> B All primitive types NULL if A or B is NULL, TRUE if expression A is NOT equal to expression B otherwise FALSE
A < B All primitive types NULL if A or B is NULL, TRUE if expression A is less than expression B otherwise FALSE
A <= B All primitive types NULL if A or B is NULL, TRUE if expression A is less than or equal to expression B otherwise FALSE
A > B All primitive types NULL if A or B is NULL, TRUE if expression A is greater than expression B otherwise FALSE
A >= B All primitive types NULL if A or B is NULL, TRUE if expression A is greater than or equal to expression B otherwise FALSE
A IS NULL all types TRUE if expression A evaluates to NULL otherwise FALSE
A IS NOT NULL All types TRUE if expression A evaluates to NULL otherwise FALSE
A LIKE B strings NULL if A or B is NULL, TRUE if string A matches the SQL simple regular expression B, otherwise FALSE. The comparison is done character by character. The _ character in B matches any character in A(similar to . in posix regular expressions) while the % character
in B matches an arbitrary number of characters in A(similar to .* in posix regular expressions) e.g. ‘foobar’ like ‘foo’ evaluates to FALSE where as ‘foobar’ like ‘foo_ _ _’ evaluates to TRUE and so does ‘foobar’ like ‘foo%’
A RLIKE B strings NULL if A or B is NULL, TRUE if string A matches the Java regular expression B(See Java regular expressions syntax), otherwise FALSE e.g. ‘foobar’ rlike ‘foo’ evaluates to FALSE where as ‘foobar’ rlike ‘^f.*r$’ evaluates to TRUE
A REGEXP B strings Same as RLIKE

6.1.2 代数操作符

返回数字类型,如果任意一个操作符为NULL,则结果为NULL

Operator Operand types Description
A + B All number types Gives the result of adding A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands. e.g. since every integer is a float, therefore float is a containing type of integer
so the + operator on a float and an int will result in a float.
A – B All number types Gives the result of subtracting B from A. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A * B All number types Gives the result of multiplying A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands. Note that if the multiplication causing overflow, you will have to cast one of
the operators to a type higher in the type hierarchy.
A / B All number types Gives the result of dividing B from A. The result is a double type.
A % B All number types Gives the reminder resulting from dividing A by B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A & B All number types Gives the result of bitwise AND of A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A | B All number types Gives the result of bitwise OR of A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A ^ B All number types Gives the result of bitwise XOR of A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
~A All number types Gives the result of bitwise NOT of A. The type of the result is the same as the type of A.

6.1.3 逻辑操作符

6.1.4 复杂类型操作符

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, …
Array (val1, val2, …) Creates an array with the given elements

6.1.5 内建函数

6.1.6 数学函数

6.1.7 集合函数

6.1.8 类型转换

6.1.9 日期函数

返回值类型 名称 描述
string from_unixtime(int unixtime) 将时间戳(unix epoch秒数)转换为日期时间字符串,例如from_unixtime(0)=”1970-01-01 00:00:00″
bigint unix_timestamp() 获得当前时间戳
bigint unix_timestamp(string date) 获得date表示的时间戳
bigint to_date(string timestamp) 返回日期字符串,例如to_date(“1970-01-01 00:00:00″) = “1970-01-01″
string year(string date) 返回年,例如year(“1970-01-01 00:00:00″) = 1970,year(“1970-01-01″) = 1970
int month(string date)
int day(string date) dayofmonth(date)
int hour(string date)
int minute(string date)
int second(string date)
int weekofyear(string date)
int datediff(string enddate, string startdate) 返回enddate和startdate的天数的差,例如datediff(’2009-03-01′, ’2009-02-27′)
= 2
int date_add(string startdate, int days) 加days天数到startdate: date_add(’2008-12-31′, 1) = ’2009-01-01′
int date_sub(string startdate, int days) 减days天数到startdate: date_sub(’2008-12-31′, 1) = ’2008-12-30′

6.1.10 条件函数

返回值类型 名称 描述
- if(boolean testCondition, T valueTrue, T valueFalseOrNull)
当testCondition为真时返回valueTrue,testCondition为假或NULL时返回valueFalseOrNull
- COALESCE(T v1, T v2, …) 返回列表中的第一个非空元素,如果列表元素都为空则返回NULL
- CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END a = b,返回c;a = d,返回e;否则返回f
- CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END a 为真,返回b;c为真,返回d;否则e

6.1.11 字符串函数

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

返回值类型 名称 描述
Int length(string A) 返回字符串长度
String reverse(string A) 反转字符串
String concat(string A, string B…) 合并字符串,例如concat(‘foo’, ‘bar’)=’foobar’。注意这一函数可以接受任意个数的参数
String substr(string A, int start) substring(string A, int start) 返回子串,例如substr(‘foobar’, 4)=’bar’
String substr(string A, int start, int len) substring(string A, int start, int len) 返回限定长度的子串,例如substr(‘foobar’, 4, 1)=’b’
String upper(string A) ucase(string A) 转换为大写
String lower(string A) lcase(string A) 转换为小写
String trim(string A)  
String ltrim(string A)  
String rtrim(string A)  
String regexp_replace(string A, string B, string C) Returns the string resulting from replacing all substrings in B that match the Java regular expression syntax(See Java regular expressions syntax) with C 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 regexp_extract(string subject, string pattern, int intex) 返回使用正则表达式提取的子字串。例如,regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2)=’bar’。注意使用特殊字符的规则:使用’\s’代表的是字符’s';空白字符需要使用’\\s’,以此类推。
String parse_url(string urlString, string partToExtract) 解析URL字符串,partToExtract的可选项有:HOST, PATH, QUERY, REF, PROTOCOL, FILE, AUTHORITY, USERINFO。
例如,
parse_url(‘http://facebook.com/path/p1.php?query=1′, ‘HOST’)=’facebook.com’
parse_url(‘http://facebook.com/path/p1.php?query=1′, ‘PATH’)=’/path/p1.php’
parse_url(‘http://facebook.com/path/p1.php?query=1′, ‘QUERY’)=’query=1′,可以指定key来返回特定参数,key的格式是QUERY:<KEY_NAME>,例如QUERY:k1
parse_url(‘http://facebook.com/path/p1.php?query=1&field=2′,’QUERY’,'query’)=’1′可以用来取出外部渲染参数key对应的value值
parse_url(‘http://facebook.com/path/p1.php?query=1&field=2′,’QUERY’,'field’)=’2′
parse_url(‘http://facebook.com/path/p1.php?query=1#Ref’, ‘REF’)=’Ref’
parse_url(‘http://facebook.com/path/p1.php?query=1#Ref’, ‘PROTOCOL’)=’http’
String get_json_object(string json_string, string path) 解析json字符串。若源json字符串非法则返回NULL。path参数支持JSONPath的一个子集,包括以下标记:
$: Root object
[]: Subscript operator for array
&: Wildcard for []
.: Child operator
String space(int n) 返回一个包含n个空格的字符串
String repeat(string str, int n) 重复str字符串n遍
String ascii(string str) 返回str中第一个字符的ascii码
String lpad(string str, int len, string pad) 左端补齐str到长度为len。补齐的字符串由pad指定。
String rpad(string str, int len, string pad) 右端补齐str到长度为len。补齐的字符串由pad指定。
Array split(string str, string pat) 返回使用pat作为正则表达式分割str字符串的列表。例如,split(‘foobar’,
‘o’)[2] = ‘bar’
。?不是很明白这个结果
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
linux
相关资讯       Hive  Hive入门  Hive教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款