1. 程式人生 > >Hive啟用hiveserver2(JDBC)

Hive啟用hiveserver2(JDBC)

使用zk配置為HA高可用

修改hive-site.xml配置檔案:增加以下配置

<property>
    <name>hive.zookeeper.quorum</name>
    <value>172.x.x.x:2181,172.x.x.x:2181,172.x.x.x:2181</value>
</property>
<property>
    <name>hive.zookeeper.session.timeout</name>
    <value>10000</value>
</property>
<property>
    <name>hive.server2.support.dynamic.service.discovery</name>
    <value>true</value>
</property>
<property>
    <name>hive.server2.zookeeper.namespace</name>
    <value>hiveserver2</value>
</property>

另外注意修改hive.server2.thrift.bind.host屬性,預設為localhost

<property>
    <name>hive.server2.thrift.bind.host</name>
    <value>hadoop.test.n4</value>
    <description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>

啟動服務:

#hiveserver2
nohup hive --service hiveserver2 > /home/hadoop/hive/logs/hiveserver2.log &

java連線hiveserver2

package com.baifendian.hiveserver2.ha;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class TestHiveserverHA {
	private static String driverName = "org.apache.hive.jdbc.HiveDriver";

	/**
	* @param args
	* @throws SQLException
	*/
	public static void main(String[] args) throws SQLException {
		try {
		  Class.forName(driverName);
		} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		  e.printStackTrace();
		  System.exit(1);
		}
		String hive2String = "jdbc:hive2://172.x.x.x:2181,172.x.x.x:2181,172.x.x.x:2181/default;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2";
		//DriverManager.
		Connection con = DriverManager.getConnection(hive2String, "hive", "");
		System.out.println(con);
		Statement stmt = con.createStatement();
		String tableName = "testHiveDriverTable";
		stmt.execute("drop table if exists " + tableName);
		stmt.execute("create table " + tableName + " (key int, value string)");
		String sql = "show tables";
		System.out.println("Running: " + sql);
		ResultSet res = stmt.executeQuery(sql);
		if (res.next()) {
		  System.out.println(res.getString(1));
		}
	}
}