Hive启用hiveserver2(JDBC)

使用zk配置为HA高可用

修改hive-site.xml配置文件:增加以下配置


    hive.zookeeper.quorum
    172.x.x.x:2181,172.x.x.x:2181,172.x.x.x:2181


    hive.zookeeper.session.timeout
    10000


    hive.server2.support.dynamic.service.discovery
    true


    hive.server2.zookeeper.namespace
    hiveserver2

另外注意修改hive.server2.thrift.bind.host属性,默认为localhost


    hive.server2.thrift.bind.host
    hadoop.test.n4
    Bind host on which to run the HiveServer2 Thrift service.

启动服务:

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

java连接hiveserver2

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));
		}
	}
}

参考地址: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC

你可能感兴趣的:(大数据,hive)