说明:集群是cdh5.7.5
1、maven的配置:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo2-api</artifactId>
<version>2.3-eb</version>
</dependency>
</dependencies>
2、代码:
/*
* 针对hive2的:org.apache.hive.jdbc.HiveDriver;jdbc:hive2://10.2.4.238:10000/default
* 不是hive2的:org.apache.hadoop.hive.jdbc.HiveDriver;jdbc:hive://localhost:10000/default
* */
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://10.2.4.238:10000/health_report", "", "");
Statement stmt = con.createStatement();
String sql = "select a.projid,compload,round(a.su/b.su,3) percent from(select projid,compload,sum(duration) su from lxj_load0023 where projid=105206 and month=7 group by projid,compload)a inner join (select projid,sum(duration) su from lxj_load0023 where projid=105206 and month=7 group by projid)b on a.projid=b.projid";
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1)+","+res.getString(2)+","+res.getString(3));
}
}