目录
1 Hive--HiveServer2 命令行代码连接
1.1 配置HiveServer2 WEB 参数
1.2 开启HiveServer2
1.3 使用Beeline连接HiveServer2
1.4 使用代码查询HiveServer2
1.5 使用DBeaver连接Hive
2 Hive--Hive常用命令
2.1 Hive 命令
2.2 Hive Shell 命令
3 Hive--自定义UDF函数(User-Defined Functions)
3.1 Pom里面需要依赖
3.2 需求
3.3 MyLower Code
3.4 临时使用UDF函数
3.5 永久注册UDF函数
3.6 Hive Shell初始化UDF函数
3.7 UDF函数转成Hive build-in 函数
3.8 自定义UDTF代码:实现spilt功能,第一个参数传入需要分割的字符串,第二个参数传入分隔符
4 Hive--排序
4.1 在Hive中创建一个表
4.2 修改Reduce Task 个数
4.3 四大By解析
hive.server2.webui.host
you hostname
hive.server2.webui.port
19990
$HIVE_HOME/bin/hiveserver2
OR
$HIVE_HOME/bin/hive --service hiveserver2
[work@bigdatatest01 ~]$ $HIVE_HOME/bin/hiveserver2 -H
usage: hiveserver2
--deregister Deregister all instances of given
version from dynamic service discovery
-H,--help Print help information
--hiveconf Use value for given property
$HIVE_HOME/bin/hiveserver2 --hiveconf hive.server2.thrift.port=14000
Hive--HiveServer2+Clients
[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.1 需求
使用代码查询所有的databases
1.4.2 Code
1.4.2.1 pom依赖
org.apache.hive
hive-jdbc
1.1.0-cdh5.16.2
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));
}
}
}
HiveJDBCClinet Code
1.4.3 结果
1.4.3.1 控制台输出
Running: show databases
bigdata
default
ods_bigdata
test
DBeaver连接HIVE
Hive 命令操作
usage: hive
-d,--define Variable substitution to apply to Hive
commands. e.g. -d A=B or --define A=B
-e SQL from command line
-f SQL from files
-H,--help Print help information
-h Connecting to Hive Server on remote host
--hiveconf Use value for given property
--hivevar Variable substitution to apply to hive
commands. e.g. --hivevar A=B
-i Initialization SQL file
-p Connecting to Hive Server on port number
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the
console)
[work@bigdatatest02 ~]$ ${HIVE_HOME}/bin/hive -e "show databases"
bigdata
default
ods_bigdata
test
[work@bigdatatest02 hive]$ vim demo.sql
show databases;
[work@bigdatatest02 hive]$ ${HIVE_HOME}/bin/hive -f demo.sql
bigdata
default
ods_bigdata
test
quit
exit
Use quit or exit to leave the interactive shell.
hive> exit;
[work@bigdatatest02 hive]$
set =
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
add FILE[S] *
add JAR[S] *
add