6、HiveServer2

启动HS2时, 10000端口 可能会等会才会开启;可能要等10+秒

/tmp/username/hive.log查看日志

HiveServer2 (HS2) is a server interface that enables remote clients to execute queries against Hive and retrieve the results 
HiveServer2 (HS2)是一个服务器接口,        它允许远程客户端对Hive执行查询并检索结果
(a more detailed intro here). The current implementation, based on Thrift RPC, is an improved version of HiveServer and supports
(这里有更详细的介绍)            当前的实现是基于Thrift Rpc                          是HiveServer的改进版本,
multi-client concurrency and authentication. It is designed to provide better support for open API clients like JDBC and ODBC.
支持多客户端并发和身份验证。 旨在为JDBC和ODBC等开放API客户端提供更好的支持。

 

HiveServer is an optional service that allows a remote client to submit requests to Hive, using a variety of programming languages, 
HiverServer是一个可选的服务          允许客户端使用各种编程语言向HIve提交请求,      
and retrieve results. HiveServer is built on Apache ThriftTM (http://thrift.apache.org/), therefore it is sometimes called the Thrift
检索结果          HiveServer构建在Apache Thrifttm上 ,                    因此 它有时被称为        thrift服务器
server although this can lead to confusion because a newer service named HiveServer2 is also built on Thrift.
            尽管这可能导致混淆            因为名为HiveServer2的新服务也是构建在Thrift服务器之上的。                 
Since the introduction of HiveServer2, HiveServer has also been called HiveServer1.
自从 HiveServer2的引入,HiveServer也被称为HiveServer1

 

--->  hive metastore  ---> hs2   -->   beeline
        |__ hive

  

使用hiveserver2的优点如下:
​    1、在应用端不需要部署hadoop和hive的客户端
​    2、hiveserver2不用直接将hdfs和metastore暴露给用户
​    3、有HA机制,解决应用端的并发和负载问题
​    4、jdbc的连接方式,可以使用任何语言,方便与应用进行数据交互

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://node01:3306/hive?createDatabaseIfNotExist=true&amp;verifyserver=false&amp;characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;localHostTrusted=false&amp;remoteHostTrusted=false</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>@aA</value>
</property>
<property>
        <name>hive.server2.thrift.bind.host</name>
        <value>node01</value>
</property>
<property>
       <name>hive.metastore.uris</name>
       <value>thrift://node01:9083</value>
</property>
<property>
        <name>hive.execution.engine</name>
        <value>mr</value>
</property>
</configuration>

 

远程metastore配置    
<property> <name>hive.metastore.uris</name> <value>thrift://node03:9083</value> </property>

 


hiveserver2          之后再10002端口会有UI

修改hadoop core-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
   <property>
      <name>fs.defaultFS</name>
      <value>hdfs://node01:9000</value>
   </property>
   <property>
     <name>hadoop.proxyuser.root.hosts</name>
     <value>*</value>
   </property>
   <property>
     <name>hadoop.proxyuser.root.groups</name>
     <value>*</value>
   </property>
</configuration>


谁启动的hdfs谁就是superuser
所有用户都可以伪装为hadoop root 用户
假如说用xxx用户创建一个表,但是在hdfs层,xxx不能创建 ===> 使用root用户代理xxx用户。 对应文件的属主是xxx

 namenode 执行命令 或重启集群

  hdfs dfsadmin -fs hdfs://node01:9000 -refreshSuperUserGroupsConfiguration
 
[ace@node02 bin]$ beeline 
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/bigdata/apache-hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/var/bigdata/hadoop-3.1.3/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Beeline version 3.1.2 by Apache Hive
beeline> !connect jdbc:hive2://node01:10000/default  root 1234            # 用户名,密码,好像配置了上边的之后,这里随便写都可以了;但是这个又好像不是密码
Connecting to jdbc:hive2://node01:10000/default
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://node01:10000/default> 

10000端口是默认的

root 1234是ssh远程连接的密码

上边是*的前提下,登录用户居然随便写

 
!close      退出会话  
!quit       退出beeline
beeline -u jdbc:hive2://node01:10000/default
beeline -u jdbc:hive2://node01:10000/default  -n root 1234
用户、密码 是无效的
如果想让 用户名 生效,需要额外的配置;    hive一般是内网,  不需要验证

 

 


 

 

 

区别 

 

1、
  server:  hive --service metastore
  client: hive              # 开发人员使用,允许增删改查,,,,

 

2
  server:  hiveserver2
  client:  beeline          # 仅供查询使用, 且需要额外的配置, 也可以使用hive命令

 

 

 code

 

from pyhive import hive
conn = hive.Connection(host='node01', port=10000, username='ace', database='default')
cursor = conn.cursor()
cursor.execute('show databases')

for result in cursor.fetchall():
    print(result)
select cast("1" as int) from psn;

 

from pyhiveConnection import hiveConnector
cursor = hiveConnector.connection("node15.test:2181,node16.test:2181","/hiveserver2","serverUri","admin",None,databasename)
cursor.execute("show databases")
print( curosr.fetchall() )

 

 

 

行转列

join
select name,score from t where t.subject='yuwen';t1
select name,score from t where t.subject='shuxue';t2
select name,score from t where t.subject='score';t3
select t1.name,t1.score,t2.score,t3.score from t1 join t2 on t1.name=t2.name join t3 on t1.name=t3.name;

 


union all
case when
select t.name ,case t.subject when yuwen ,case t.subject when score ,case t.subject when yuwen end;


decode

 

 UDF

 

 

 

UDFS  一进一出
UDAS  多进一出
select lpad(name,10,'*') from psn;

*******小明1 

UDFF  一进多出
https://cwiki.apache.org/confluence/display/Hive/HivePlugins
CREATE FUNCTION myfunc AS 'myclass' USING JAR 'hdfs:///path/to/jar';    别的会话 也能执行

 

select count(distinct(myCol1)), count(distinct(myCol2)) from psn2 
LATERAL VIEW explode(likes) myTable1 AS myCol1
LATERAL VIEW explode(address) myTable2 AS myCol2, myCol3;

列名不要重复

   解决在select使用UDTF做查询过程中,查询只能包含单个UDTF,不能包含 其他字段、以及多个UDTF的问题

 

 

  

### 1.内置运算符

##### 1.1关系运算符

| 运算符        | 类型         | 说明                                                         |
| ------------- | ------------ | ------------------------------------------------------------ |
| A = B         | 所有原始类型 | 如果A与B相等,返回TRUE,否则返回FALSE                          |
| A == B        | 无           | 失败,因为无效的语法。 SQL使用”=”,不使用”==”。              |
| A <> B        | 所有原始类型 | 如果A不等于B返回TRUE,否则返回FALSE。如果A或B值为”NULL”,结果返回”NULL”。 |
| A < B         | 所有原始类型 | 如果A小于B返回TRUE,否则返回FALSE。如果A或B值为”NULL”,结果返回”NULL”。 |
| A <= B        | 所有原始类型 | 如果A小于等于B返回TRUE,否则返回FALSE。如果A或B值为”NULL”,结果返回”NULL”。 |
| A > B         | 所有原始类型 | 如果A大于B返回TRUE,否则返回FALSE。如果A或B值为”NULL”,结果返回”NULL”。 |
| A >= B        | 所有原始类型 | 如果A大于等于B返回TRUE,否则返回FALSE。如果A或B值为”NULL”,结果返回”NULL”。 |
| A IS NULL     | 所有类型     | 如果A值为”NULL”,返回TRUE,否则返回FALSE                      |
| A IS NOT NULL | 所有类型     | 如果A值不为”NULL”,返回TRUE,否则返回FALSE                    |
| A LIKE B      | 字符串       | 如 果A或B值为”NULL”,结果返回”NULL”。字符串A与B通过sql进行匹配,如果相符返回TRUE,不符返回FALSE。B字符串中 的”_”代表任一字符,”%”则代表多个任意字符。例如: (‘foobar’ like ‘foo’)返回FALSE,( ‘foobar’ like ‘foo_ _ _’或者 ‘foobar’ like ‘foo%’)则返回TURE |
| A RLIKE B     | 字符串       | 如 果A或B值为”NULL”,结果返回”NULL”。字符串A与B通过java进行匹配,如果相符返回TRUE,不符返回FALSE。例如:( ‘foobar’ rlike ‘foo’)返回FALSE,(’foobar’ rlike ‘^f.*r$’ )返回TRUE。 |
| A REGEXP B    | 字符串       | 与RLIKE相同。                                                |

##### 1.2算术运算符

| 运算符 | 类型         | 说明                                                         |
| ------ | ------------ | ------------------------------------------------------------ |
| A + B  | 所有数字类型 | A和B相加。结果的与操作数值有共同类型。例如每一个整数是一个浮点数,浮点数包含整数。所以,一个浮点数和一个整数相加结果也是一个浮点数。 |
| A – B  | 所有数字类型 | A和B相减。结果的与操作数值有共同类型。                       |
| A * B  | 所有数字类型 | A和B相乘,结果的与操作数值有共同类型。需要说明的是,如果乘法造成溢出,将选择更高的类型。 |
| A / B  | 所有数字类型 | A和B相除,结果是一个double(双精度)类型的结果。             |
| A % B  | 所有数字类型 | A除以B余数与操作数值有共同类型。                             |
| A & B  | 所有数字类型 | 运算符查看两个参数的二进制表示法的值,并执行按位”与”操作。两个表达式的一位均为1时,则结果的该位为 1。否则,结果的该位为 0。 |
| A\|B   | 所有数字类型 | 运算符查看两个参数的二进制表示法的值,并执行按位”或”操作。只要任一表达式的一位为 1,则结果的该位为 1。否则,结果的该位为 0。 |
| A ^ B  | 所有数字类型 | 运算符查看两个参数的二进制表示法的值,并执行按位”异或”操作。当且仅当只有一个表达式的某位上为 1 时,结果的该位才为 1。否则结果的该位为 0。 |
| ~A     | 所有数字类型 | 对一个表达式执行按位”非”(取反)。                           |

##### 1.3逻辑运算符

| 运算符  | 类型   | 说明                                                         |
| ------- | ------ | ------------------------------------------------------------ |
| A AND B | 布尔值 | A和B同时正确时,返回TRUE,否则FALSE。如果A或B值为NULL,返回NULL。 |
| A && B  | 布尔值 | 与”A AND B”相同                                              |
| A OR B  | 布尔值 | A或B正确,或两者同时正确返返回TRUE,否则FALSE。如果A和B值同时为NULL,返回NULL。 |
| A \| B  | 布尔值 | 与”A OR B”相同                                               |
| NOT A   | 布尔值 | 如果A为NULL或错误的时候返回TURE,否则返回FALSE。             |
| ! A     | 布尔值 | 与”NOT A”相同                                                |

##### 1.4复杂类型函数

| 函数   | 类型                            | 说明                                                        |
| ------ | ------------------------------- | ----------------------------------------------------------- |
| map    | (key1, value1, key2, value2, …) | 通过指定的键/值对,创建一个map。                            |
| struct | (val1, val2, val3, …)           | 通过指定的字段值,创建一个结构。结构字段名称将COL1,COL2,… |
| array  | (val1, val2, …)                 | 通过指定的元素,创建一个数组。                              |

1.5对复杂类型函数操作

| 函数   | 类型                  | 说明                                                         |
| ------ | --------------------- | ------------------------------------------------------------ |
| A[n]   | A是一个数组,n为int型 | 返回数组A的第n个元素,第一个元素的索引为0。如果A数组为['foo','bar'],则A[0]返回’foo’和A[1]返回”bar”。 |
| M[key] | M是Map<K, V>,关键K型 | 返回关键值对应的值,例如mapM为 \{‘f’ -> ‘foo’, ‘b’ -> ‘bar’, ‘all’ -> ‘foobar’\},则M['all'] 返回’foobar’。 |
| S.x    | S为struct             | 返回结构x字符串在结构S中的存储位置。如 foobar \{int foo, int bar\} foobar.foo的领域中存储的整数。 |

### 2.内置函数

##### 2.1数学函数

| 返回类型   | 函数                                              | 说明                                                         |
| ---------- | ------------------------------------------------- | ------------------------------------------------------------ |
| BIGINT     | round(double a)                                   | 四舍五入                                                     |
| DOUBLE     | round(double a, int d)                            | 小数部分d位之后数字四舍五入,例如round(21.263,2),返回21.26   |
| BIGINT     | floor(double a)                                   | 对给定数据进行向下舍入最接近的整数。例如floor(21.2),返回21。 |
| BIGINT     | ceil(double a), ceiling(double a)                 | 将参数向上舍入为最接近的整数。例如ceil(21.2),返回23.         |
| double     | rand(), rand(int seed)                            | 返回大于或等于0且小于1的平均分布随机数(依重新计算而变)     |
| double     | exp(double a)                                     | 返回e的n次方                                                 |
| double     | ln(double a)                                      | 返回给定数值的自然对数                                       |
| double     | log10(double a)                                   | 返回给定数值的以10为底自然对数                               |
| double     | log2(double a)                                    | 返回给定数值的以2为底自然对数                                |
| double     | log(double base, double a)                        | 返回给定底数及指数返回自然对数                               |
| double     | pow(double a, double p) power(double a, double p) | 返回某数的乘幂                                               |
| double     | sqrt(double a)                                    | 返回数值的平方根                                             |
| string     | bin(BIGINT a)                                     | 返回二进制格式                                               |
| string     | hex(BIGINT a) hex(string a)                       | 将整数或字符转换为十六进制格式                               |
| string     | unhex(string a)                                   | 十六进制字符转换由数字表示的字符。                           |
| string     | conv(BIGINT num, int from_base, int to_base)      | 将 指定数值,由原来的度量体系转换为指定的试题体系。例如CONV(‘a’,16,2),返回。参考:’1010′ http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_conv |
| double     | abs(double a)                                     | 取绝对值                                                     |
| int double | pmod(int a, int b) pmod(double a, double b)       | 返回a除b的余数的绝对值                                       |
| double     | sin(double a)                                     | 返回给定角度的正弦值                                         |
| double     | asin(double a)                                    | 返回x的反正弦,即是X。如果X是在-1到1的正弦值,返回NULL。     |
| double     | cos(double a)                                     | 返回余弦                                                     |
| double     | acos(double a)                                    | 返回X的反余弦,即余弦是X,,如果-1<= A <= 1,否则返回null.   |
| int double | positive(int a) positive(double a)                | 返回A的值,例如positive(2),返回2。                          |
| int double | negative(int a) negative(double a)                | 返回A的相反数,例如negative(2),返回-2。                      |

##### 2.2收集函数

| 返回类型 | 函数           | 说明                      |
| -------- | -------------- | ------------------------- |
| int      | size(Map<K.V>) | 返回的map类型的元素的数量 |
| int      | size(Array<T>) | 返回数组类型的元素数量    |

##### 2.3类型转换函数

| 返回类型    | 函数                 | 说明                                                         |
| ----------- | -------------------- | ------------------------------------------------------------ |
| 指定 “type” | cast(expr as <type>) | 类型转换。例如将字符”1″转换为整数:cast(’1as bigint),如果转换失败返回NULL。 |

##### 2.4日期函数

| 返回类型 | 函数                                            | 说明                                                         |
| -------- | ----------------------------------------------- | ------------------------------------------------------------ |
| string   | from_unixtime(bigint unixtime[, string format]) | UNIX_TIMESTAMP参数表示返回一个值’YYYY- MM – DD HH:MM:SS’或YYYYMMDDHHMMSS.uuuuuu格式,这取决于是否是在一个字符串或数字语境中使用的功能。该值表示在当前的时区。 |
| bigint   | unix_timestamp()                                | 如果不带参数的调用,返回一个Unix时间戳(从’1970- 010100:00:00′到现在的UTC秒数)为无符号整数。 |
| bigint   | unix_timestamp(string date)                     | 指定日期参数调用UNIX_TIMESTAMP(),它返回参数值’1970- 010100:00:00′到指定日期的秒数。 |
| bigint   | unix_timestamp(string date, string pattern)     | 指定时间输入格式,返回到1970年秒数:unix_timestamp(’2009-03-20′, ‘yyyy-MM-dd’) = 1237532400 |
| string   | to_date(string timestamp)                       | 返回时间中的年月日: to_date(“1970-01-01 00:00:00″) = “1970-01-01″ |
| string   | to_dates(string date)                           | 给定一个日期date,返回一个天数(0年以来的天数)              |
| int      | year(string date)                               | 返回指定时间的年份,范围在1000到9999,或为”零”日期的0。      |
| int      | month(string date)                              | 返回指定时间的月份,范围为1至12月,或0一个月的一部分,如’0000-00-00′或’2008-00-00′的日期。 |
| int      | day(string date) dayofmonth(date)               | 返回指定时间的日期                                           |
| int      | hour(string date)                               | 返回指定时间的小时,范围为0到23。                            |
| int      | minute(string date)                             | 返回指定时间的分钟,范围为0到59。                            |
| int      | second(string date)                             | 返回指定时间的秒,范围为0到59。                              |
| int      | weekofyear(string date)                         | 返回指定日期所在一年中的星期号,范围为0到53。                |
| int      | datediff(string enddate, string startdate)      | 两个时间参数的日期之差。                                     |
| int      | date_add(string startdate, int days)            | 给定时间,在此基础上加上指定的时间段。                       |
| int      | date_sub(string startdate, int days)            | 给定时间,在此基础上减去指定的时间段。                       |

##### 2.5条件函数

| 返回类型 | 函数                                                       | 说明                                                         |
| -------- | ---------------------------------------------------------- | ------------------------------------------------------------ |
| T        | if(boolean testCondition, T valueTrue, T valueFalseOrNull) | 判断是否满足条件,如果满足返回一个值,如果不满足则返回另一个值。 |
| T        | COALESCE(T v1, T v2, …)                                    | 返回一组数据中,第一个不为NULL的值,如果均为NULL,返回NULL。  |
| T        | CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END         | 当a=b时,返回c;当a=d时,返回e,否则返回f。                   |
| T        | CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END           | 当值为a时返回b,当值为c时返回d。否则返回e。                   |

##### 2.6字符函数

| 返回类型                     | 函数                                                         | 说明                                                         |
| ---------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| int                          | length(string A)                                             | 返回字符串的长度                                             |
| string                       | reverse(string A)                                            | 返回倒序字符串                                               |
| string                       | concat(string A, string B…)                                  | 连接多个字符串,合并为一个字符串,可以接受任意数量的输入字符串 |
| string                       | concat_ws(string SEP, string A, string B…)                   | 链接多个字符串,字符串之间以指定的分隔符分开。               |
| string                       | substr(string A, int start) substring(string A, int start)   | 从文本字符串中指定的起始位置后的字符。                       |
| string                       | substr(string A, int start, int len) substring(string A, int start, int len) | 从文本字符串中指定的位置指定长度的字符。                     |
| 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)                 | 字符串A中的B字符被C字符替代                                  |
| string                       | regexp_extract(string subject, string pattern, int index)    | 通过下标返回正则表达式指定的部分。regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) returns ‘bar.’ |
| string                       | parse_url(string urlString, string partToExtract [, string keyToExtract]) | 返回URL指定的部分。parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1′, ‘HOST’) 返回:’facebook.com’ |
| string                       | get_json_object(string json_string, string path)             | select a.timestamp, get_json_object(a.appevents, ‘$.eventid’), get_json_object(a.appenvets, ‘$.eventname’) from log a; |
| string                       | space(int n)                                                 | 返回指定数量的空格                                           |
| string                       | repeat(string str, int n)                                    | 重复N次字符串                                                |
| int                          | ascii(string str)                                            | 返回字符串中首字符的数字值                                   |
| string                       | lpad(string str, int len, string pad)                        | 返回指定长度的字符串,给定字符串长度小于指定长度时,由指定字符从左侧填补。 |
| string                       | rpad(string str, int len, string pad)                        | 返回指定长度的字符串,给定字符串长度小于指定长度时,由指定字符从右侧填补。 |
| array                        | split(string str, string pat)                                | 将字符串转换为数组。                                         |
| int                          | find_in_set(string str, string strList)                      | 返回字符串str第一次在strlist出现的位置。如果任一参数为NULL,返回NULL;如果第一个参数包含逗号,返回0。 |
| array<array<string>>         | sentences(string str, string lang, string locale)            | 将字符串中内容按语句分组,每个单词间以逗号分隔,最后返回数组。 例如sentences(‘Hello there! How are you?’) 返回:( (“Hello”, “there”), (“How”, “are”, “you”) ) |
| array<struct<string,double>> | ngrams(array<array<string>>, int N, int K, int pf)           | SELECT ngrams(sentences(lower(tweet)), 2, 100 [, 1000]) FROM twitter; |
| array<struct<string,double>> | context_ngrams(array<array<string>>, array<string>, int K, int pf) | SELECT context_ngrams(sentences(lower(tweet)), array(null,null), 100, [, 1000]) FROM twitter; |

### 3.内置的聚合函数(UDAF)

| 返回类型                 | 函数                                                         | 说明                                                         |
| ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| bigint                   | count(*) , count(expr), count(DISTINCT expr[, expr_., expr_.]) | 返回记录条数。                                               |
| double                   | sum(col), sum(DISTINCT col)                                  | 求和                                                         |
| double                   | avg(col), avg(DISTINCT col)                                  | 求平均值                                                     |
| double                   | min(col)                                                     | 返回指定列中最小值                                           |
| double                   | max(col)                                                     | 返回指定列中最大值                                           |
| double                   | var_pop(col)                                                 | 返回指定列的方差                                             |
| double                   | var_samp(col)                                                | 返回指定列的样本方差                                         |
| double                   | stddev_pop(col)                                              | 返回指定列的偏差                                             |
| double                   | stddev_samp(col)                                             | 返回指定列的样本偏差                                         |
| double                   | covar_pop(col1, col2)                                        | 两列数值协方差                                               |
| double                   | covar_samp(col1, col2)                                       | 两列数值样本协方差                                           |
| double                   | corr(col1, col2)                                             | 返回两列数值的相关系数                                       |
| double                   | percentile(col, p)                                           | 返回数值区域的百分比数值点。0<=P<=1,否则返回NULL,不支持浮点型数值。 |
| array<double>            | percentile(col, array(p~1,,\ [, p,,2,,]…))                   | 返回数值区域的一组百分比值分别对应的数值点。0<=P<=1,否则返回NULL,不支持浮点型数值。 |
| double                   | percentile_approx(col, p[, B])                               | Returns an approximate p^th^ percentile of a numeric column (including floating point types) in the group. The B parameter controls approximation accuracy at the cost of memory. Higher values yield better approximations, and the default is 10,000. When the number of distinct values in col is smaller than B, this gives an exact percentile value. |
| array<double>            | percentile_approx(col, array(p~1,, [, p,,2_]…) [, B])        | Same as above, but accepts and returns an array of percentile values instead of a single one. |
| array<struct\{‘x’,'y’\}> | histogram_numeric(col, b)                                    | Computes a histogram of a numeric column in the group using b non-uniformly spaced bins. The output is an array of size b of double-valued (x,y) coordinates that represent the bin centers and heights |
| array                    | collect_set(col)                                             | 返回无重复记录                                               |

### 4.内置表生成函数(UDTF)

| 返回类型 | 函数                   | 说明                                                         |
| -------- | ---------------------- | ------------------------------------------------------------ |
| 数组     | explode(array<TYPE> a) | 数组一条记录中有多个参数,将参数拆分,每个参数生成一列。     |
|          | json_tuple             | get_json_object 语句:select a.timestamp, get_json_object(a.appevents, ‘$.eventid’), get_json_object(a.appenvets, ‘$.eventname’) from log a; json_tuple语句: select a.timestamp, b.* from log a lateral view json_tuple(a.appevent, ‘eventid’, ‘eventname’) b as f1, f2 |

 

### 5.自定义函数

自定义函数包括三种UDF、UDAF、UDTF

​        UDF(User-Defined-Function) :一进一出

​        UDAF(User- Defined Aggregation Funcation) :聚集函数,多进一出。Count/max/min

​        UDTF(User-Defined Table-Generating Functions) :一进多出,如explore()

##### **5.1** UDF 开发

1、UDF函数可以直接应用于select语句,对查询结构做格式化处理后,再输出内容。

2、编写UDF函数的时候需要注意一下几点:

a)自定义UDF需要继承org.apache.hadoop.hive.ql.UDF。

b)需要实现evaluate函数,evaluate函数支持重载。

3、步骤

(1)将jar包上传到虚拟机中:

​    a)把程序打包放到目标机器上去;

​    b)进入hive客户端,添加jar包:hive>add jar /run/jar/udf_test.jar;

​    c)创建临时函数:hive>CREATE TEMPORARY FUNCTION add_example AS 'hive.udf.Add';

​    d)查询HQL语句:

​        SELECT add_example(8, 9) FROM scores;

​        SELECT add_example(scores.math, scores.art) FROM scores;

​        SELECT add_example(6, 7, 8, 6.8) FROM scores;

​    e)销毁临时函数:hive> DROP TEMPORARY FUNCTION add_example;

​    注意:此种方式创建的函数属于临时函数,当关闭了当前会话之后,函数会无法使用,因为jar的引用没有了,无法找到对应的java文件进行处理,因此不推荐使用。

(2)将jar包上传到hdfs集群中:

​     a)把程序打包上传到hdfs的某个目录下

​    b)创建函数:hive>CREATE FUNCTION add_example AS 'hive.udf.Add' using jar "hdfs://mycluster/jar/udf_test.jar";

​    d)查询HQL语句:

​        SELECT add_example(8, 9) FROM scores;

​        SELECT add_example(scores.math, scores.art) FROM scores;

​        SELECT add_example(6, 7, 8, 6.8) FROM scores;

​    e)销毁临时函数:hive> DROP  FUNCTION add_example;

 

 

 

 

高可用

node01

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://node01:3306/hive?createDatabaseIfNotExist=true&amp;verifyserver=false&amp;characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;localHostTrusted=false&amp;remoteHostTrusted=false</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
</property>
<property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>@aA</value>
</property>
<property>
        <name>hive.server2.thrift.bind.host</name>
        <value>node01</value>
</property> 
<property>
  <name>hive.server2.support.dynamic.service.discovery</name>
  <value>true</value>
</property>
<property>
  <name>hive.server2.zookeeper.namespace</name>
   <value>hiveserver2_zk</value>
</property>
<property>
   <name>hive.zookeeper.quorum</name>
   <value>node01:2181,node02:2181,node03:2181</value>
</property>
<property>
   <name>hive.zookeeper.client.port</name>
   <value>2181</value>
</property>
<property>
   <name>hive.server2.thrift.port</name>
   <value>10001</value>
</property>
</configuration>

以相同配置,除  host,配置到另外两台

启动

连接方式
jdbc:hive2://node01,node02,node03/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2_zk

 

 

启动HiveServer2中或者外部客户端连接HiveServer2时候有可能出现此异常,具体是
java.lang.ClassNotFoundException: org.apache.tez.dag.api.TezConfiguration

解决方案是:配置文件hive-site.xml中的hive.execution.engine属性值由tez修改为mr,然后重启HiveServer2即可。因为没有集成tez,重启后依然会报错,但是60000ms后会自动重试启动(一般重试后会启动成功)
这算是一个遗留问题,但是不影响客户端正常连接,只是启动时间会多了60秒。

 

 

 

posted @ 2020-07-02 21:37  慕沁  阅读(699)  评论(0)    收藏  举报