HiveServer2具有JDBC驱动程序。 它支持对HiveServer2的嵌入式和远程访问。 建议将远程HiveServer2模式用于生产,因为它更安全并且不需要为用户授予直接HDFS /元存储访问权限。
连接URL格式
HiveServer2 URL格式是具有以下语法的字符串
jdbc:hive2://<host1>:<port1>,<host2>:<port2>/dbName;initFile=<file>;sess_var_list?hive_conf_list#hive_var_list
- <host1>:<port1>,<host2>:<port2>是要连接的服务器实例或逗号分隔的服务器实例列表(如果启用了动态服务发现)。 如果为空,将使用嵌入式服务器。
- dbName是初始数据库的名称。
- <file>是初始化脚本文件的路径(Hive 2.2.0及更高版本)。 该脚本文件使用SQL语句编写,连接后将自动执行。 此选项可以为空。
- sess_var_list 是分号分隔的会话变量键值对列表,如user=foo;password=bar
- hive_conf_list是此会话的Hive配置变量的键值对的分号分隔列表,如hive.exec.mode.local.auto=true,要放在问号后面
- hive_var_list 是此会话的Hive变量的键值对的分号分隔列表。
sess_var_list,hive_conf_list,hive_var_list参数值中的特殊字符应根据需要使用URL编码进行编码
关于hive配置
管理员可以使用Hive中的许多配置变量来更改其安装和用户会话的行为。 可以按照以下任意一种方式配置这些变量,以优先顺序说明:
- 在CLI或Beeline中使用set命令为set命令之后的所有语句设置配置变量的会话级别值。 例如,以下命令将所有后续语句的临时目录(Hive用于存储临时输出和计划)设置为 /tmp/mydir:
set hive.exec.scratchdir=/tmp/mydir;
- 在整个会话中使用hive命令的--hiveconf选项(在CLI中)或beeline命令。 例如:
bin/hive --hiveconf hive.exec.scratchdir=/tmp/mydir
- 在hive-site.xml中。 这用于设置整个Hive配置的值, 例如:
<property>
<name>hive.exec.scratchdir</name>
<value>/tmp/mydir</value>
<description>Scratch space for Hive jobs</description>
</property>
- 在特定于服务器配置文件中(受支持的Hive 0.14开始)。 您可以在hivemetastore-site.xml中设置特定于metastore的配置值,并在hiveserver2-site.xml中设置特定于HiveServer2的配置值。
所有配置项参考https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties
使用jdbc
首先添加依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1</version>
</dependency>
注意版本要跟你hive安装的保持一致即可,看到网上有需要其他的,我这边测试只要这一个就够了
然后就是jdbc的api操作
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static Connection con = null;
private static String tableName = "hello";
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
long start = System.currentTimeMillis();
// 创建hive连接
initConn();
// 建表
//createTable();
// 加载文件数据到表
//loadFile();
// 直接插入文本
insertTable();
// 表数据导入文件
//insertFile();
// 查询
//select();
// 关闭连接
destryConn();
long end = System.currentTimeMillis();
long cust = end - start;
System.out.println("执行耗时" + cust);
}
public static void initConn() {
try {
Class.forName(driverName);
con = DriverManager.getConnection("jdbc:hive2://foo-1.example.com:10000/default?hive.exec.mode.local.auto=true", "hive", "");
//replace "hive" here with the name of the user the queries should run as
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
}
public static void destryConn() {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createTable() {
try {
Statement stmt = con.createStatement();
String sql = "create table if not exists " + tableName + " (key int, value string)";
System.out.println("Running: " + sql);
stmt.execute(sql);
// show tables
sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// describe table
sql = "describe " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void loadFile() {
// 导入文件
// load data into table
// NOTE: filepath has to be local to the hive server 注意,这个文件必须在hiveserver服务器上存在,而不是此程序所在的服务器
// NOTE: /tmp/test.txt is a ctrl-A separated file with two fields per line
String filepath = "/tmp/test.txt";
String sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
try {
Statement stmt = con.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertTable() {
// 执行sql
String sql = "insert into " + tableName + "(key,value) values (1,'hello'),(2,'hive')";
for (int i = 0; i < 1000; i++) {
sql = sql + ",("+i+",'hive"+i+"')";
}
System.out.println("Running: " + sql);
try {
Statement stmt = con.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertFile() {
// 执行sql
String sql = "INSERT OVERWRITE LOCAL DIRECTORY '/tmp/hello_out' SELECT a.* FROM " + tableName + " a";
System.out.println("Running: " + sql);
try {
Statement stmt = con.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void select() {
String sql = "select * from " + tableName;
System.out.println("Running: " + sql);
try {
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}