目录
3 Hive--自定义UDF函数(User-Defined Functions)
3.8 自定义UDTF代码:实现spilt功能,第一个参数传入需要分割的字符串,第二个参数传入分隔符
- 以下所说的都是针对 Hive 为 1.1.0-cdh5.16.2版本
1 Hive--HiveServer2 命令行代码连接
1.1 配置HiveServer2 WEB 参数
- HiveServer2是一个服务端,开启之后可以通过JDBC连接
- 还对应一个WEB服务,开启WEB服务可以看到当前有哪些Session进行连接,以及执行了哪些语句等详细信息
- 开启HiveServer2 WEB服务需要在hive-site.xml配置
<property>
<name>hive.server2.webui.host</name>
<value>you hostname</value>
</property>
<property>
<name>hive.server2.webui.port</name>
<value>19990</value>
</property>
1.2 开启HiveServer2
$HIVE_HOME/bin/hiveserver2
OR
$HIVE_HOME/bin/hive --service hiveserver2
- 查看hiveserver2命令帮助
[work@bigdatatest01 ~]$ $HIVE_HOME/bin/hiveserver2 -H
usage: hiveserver2
--deregister <versionNumber> Deregister all instances of given
version from dynamic service discovery
-H,--help Print help information
--hiveconf <property=value> Use value for given property
- 后面加hiveconf参数可以直接指定hive-site.xml里面的参数:K=V
- 修改HiveServer2端口
$HIVE_HOME/bin/hiveserver2 --hiveconf hive.server2.thrift.port=14000
1.3 使用Beeline连接HiveServer2
- hostname:HiveServer2开启服务的机器的Hostname
- port: HiveServer2服务开启的端口
- uesername:用户名
- passwd: 密码
[work@bigdatatest01 ~]$ $HIVE_HOME/bin/beeline -u jdbc:hive2://hostname:port/default -n hadoop
OR
[work@bigdatatest01 ~]$ $HIVE_HOME/bin/beeline
beeline> !connect jdbc:hive2://hostname:port/default uesername passwd
1.4 使用代码查询HiveServer2
1.4.1 需求
使用代码查询所有的databases
1.4.2 Code
1.4.2.1 pom依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0-cdh5.16.2</version>
</dependency>
1.4.2.2 HiveJDBCClinet Code
package com.xk.bigdata.hive.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class HiveJDBCClinet {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws Exception {
Class.forName(driverName);
//replace "hive" here with the name of the user the queries should run as
Connection con = DriverManager.getConnection("jdbc:hive2://bigdatatest03:10000/default", "hive", "");
Statement stmt = con.createStatement();
// show databases
String sql = "show databases";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}
1.4.3 结果
1.4.3.1 控制台输出
Running: show databases
bigdata
default
ods_bigdata
test
1.5 使用DBeaver连接Hive
- 之前文章已经写了,可以参考一下
2 Hive--Hive常用命令
usage: hive
-d,--define <key=value> Variable substitution to apply to Hive
commands. e.g. -d A=B or --define A=B
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
-h <hostname> Connecting to Hive Server on remote host
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable substitution to apply to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-p <port> Connecting to Hive Server on port number
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the
console)
2.1 Hive 命令
- 常用命令有如下
- ${HIVE_HOME}/bin/hive -e + ${SQL 语句}
[work@bigdatatest02 ~]$ ${HIVE_HOME}/bin/hive -e "show databases"
bigdata
default
ods_bigdata
test
- ${HIVE_HOME}/bin/hive -f + ${SQL 文件}
- Hive 0.14 版本之后还支持HDFS上面的sql文件
[work@bigdatatest02 hive]$ vim demo.sql
show databases;
[work@bigdatatest02 hive]$ ${HIVE_HOME}/bin/hive -f demo.sql
bigdata
default
ods_bigdata
test
- ${HIVE_HOME}/bin/hive -i : 初始化
- 一般是在自定义UDF函数的时候可以用到
- ${HIVE_HOME}/bin/hive --hiveconf <property=value>
- 一般会在Hive参数传递时候使用,可以把参数传递到SQL 语句或者SQL文件
2.2 Hive Shell 命令
- 退出命令行
quit
exit
Use quit or exit to leave the interactive shell.
hive> exit;
[work@bigdatatest02 hive]$
- 查看或者设置某个参数
- 只对当前Session有效
set <key>=<value>
Sets the value of a particular configuration variable (key).
Note: If you misspell the variable name, the CLI will not show an error.
# 查看hive.cli.print.current.db参数
hive> set hive.cli.print.current.db;
hive.cli.print.current.db=false
# 把hive.cli.print.current.db改为true
hive> set hive.cli.print.current.db=true;
# 查看hive.cli.print.current.db参数
hive (default)> set hive.cli.print.current.db;
hive.cli.print.current.db=true
- 添加、列表、删除文件或者jar
- jar或者文件可以是本地系统也可以是HDFS
add FILE[S] <filepath> <filepath>*
add JAR[S] <filepath> <filepath>*
add