Hive 0.14 + hadoop 2.4.1 環境下的 jdbc連接
本文記錄在Hive 0.14 + hadoop 2.4.1 環境下,如何時用 jdbc連接到hive
hive 的JDBC驅動還是比較好找的,在hive的包里就有 hive-jdbc-0.14.0-standalone.jar 是一個N合一的包,把它放到buildPath
除了這個包,還需要幾個hadoop下的包,最后的buildpah如下圖:
用服務模式啟動hive,10010是監聽的端口號
hive --service hiveserver 10010
下面是個示例代碼,連接到hive以后建立了一個新表
package cn.pior.test;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;
public class HiveTest {
private static final String URLHIVE = "jdbc:hive://192.168.1.201:10010/default"; private static Connection connection = null;
public static Connection getHiveConnection() { if (null == connection) { synchronized (HiveTest.class) { if (null == connection) { try { Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver"); connection = DriverManager.getConnection(URLHIVE, "", ""); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } return connection; }
public static void createTable() throws SQLException { String tweetTableSql = "create table t2 (i int)"; Statement stmt = getHiveConnection().createStatement(); stmt.executeQuery(tweetTableSql); stmt.close(); }
public static void main(String[] args) throws SQLException { createTable(); getHiveConnection().close(); } }</pre>
</div>