java连接hiveserver2

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

	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://hostname:10000/dataplat", "hive", "");

		Statement stmt = con.createStatement();

		String sql = " select countertype,cppadate,count(1) from Hive_DataCpzCppaImeiCounter_real where cppadate>='2016-01-07' group by countertype,cppadate";
		ResultSet res = stmt.executeQuery(sql);

		while (res.next()) {

			System.out.println(res.getString(1) + "\t" + res.getString(2));

		}
		
		
	}
}

重点在getConnection的时候,要把你hive帐号传过去,不然会说没有权限操作。

另外吐嘈一下,在java端是没有详情的错误日志的,错误日志只能看hive-server2的日志,我使用的是cdh,日志在/var/log/hive/hadoop-cmf-hive-HIVESERVER2-hostname.log.out

 

posted @ 2016-01-14 15:09  HarkLee  阅读(2836)  评论(0编辑  收藏  举报