</pre><pre code_snippet_id="1748761" snippet_file_name="blog_20160706_2_3968882" name="code" class="html">package zg;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcTest {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public boolean run() {
try {
Class.forName(driverName);
Connection con = null;
con = DriverManager.getConnection("jdbc:hive2://zhangge:10000/zhangge", "zhangge", "zhangge");
//con = DriverManager.getConnection("jdbc:hive2://zhangge:10000/zhangge", "zhangge", "zhangge");
Statement stmt = con.createStatement();
ResultSet res = null;
String sql = "select count(1) from t_mygril where age>19";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
System.out.println("ok");
while (res.next()) {
System.out.println(res.getString(1));
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("error");
return false;
}
}
public static void main(String[] args) throws SQLException {
HiveJdbcTest hiveJdbcClient = new HiveJdbcTest();
hiveJdbcClient.run();
}
}
用到的jar所有jar包http://download.csdn.net/detail/zhangge360/9568873
配置hive-site.xml:
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://zhangge:3306/hive?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>zhangge</value>
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>zhangge</value>
<description>password to use against metastore database</description>
</property>
<property>
<name>hive.hwi.listen.host</name>
<value>zhangge</value>
</property>
<property>
<name>hive.hwi.listen.port</name>
<value>9999</value>
</property>
<property>
<name>hive.hwi.war.file</name>
<value>lib/hive-hwi-1.2.1.war</value>
</property>
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>zhangge</value>
</property>