Java连接Hive

Hive必须以service模式运行:

hive --service hiveserver2
hiveserver2 --hiveconf hive.server2.thrift.port=20001  


Java maven 项目的pom.xml中加上:


    org.apache.hive
    hive-jdbc
    2.3.2


Java示例代码:

package cn.pywei.HiveUtil;

import java.sql.*;

public class HiveJDBC {

	private static String driverName = "org.apache.hive.jdbc.HiveDriver";

	private static ResultSet rs = null;

	public static void main(String[] args) throws SQLException {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}

		Connection con = DriverManager.getConnection("jdbc:hive2://127.0.0.1:10000/default");
		Statement stmt = con.createStatement();
		stmt = con.createStatement();
		String sql = "select dt6,dt7 from ep limit 100";
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			System.out.println(rs.getString(1)+rs.getString(2));
		}
		con.close();
	}
}

你可能感兴趣的:(Java连接Hive)