在使用 JDBC 開發 Hive 程序時, 必須首先開啟 Hive 的遠程服務接口。使用下面命令進行開啟:
hive -service hiveserver &
1). 測試數據
userinfo.txt文件內容(每行數據之間用tab鍵隔開):
1 xiapi 2 xiaoxue 3 qingqing
2). 程序代碼
package com.ljq.hive; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class HiveJdbcClient { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive://192.168.11.157:10000/default"; private static String user = "hive"; private static String password = "mysql"; private static String sql = ""; private static ResultSet res; private static final Logger log = Logger.getLogger(HiveJdbcClient.class); public static void main(String[] args) { try { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); // 創建的表名 String tableName = "testHiveDriverTable"; /** 第一步:存在就先刪除 **/ sql = "drop table " + tableName; stmt.executeQuery(sql); /** 第二步:不存在就創建 **/ sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t'"; stmt.executeQuery(sql); // 執行“show tables”操作 sql = "show tables '" + tableName + "'"; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("執行“show tables”運行結果:"); if (res.next()) { System.out.println(res.getString(1)); } // 執行“describe table”操作 sql = "describe " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("執行“describe table”運行結果:"); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } // 執行“load data into table”操作 String filepath = "/home/hadoop/ziliao/userinfo.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); // 執行“select * query”操作 sql = "select * from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("執行“select * query”運行結果:"); while (res.next()) { System.out.println(res.getInt(1) + "\t" + res.getString(2)); } // 執行“regular hive query”操作 sql = "select count(1) from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("執行“regular hive query”運行結果:"); while (res.next()) { System.out.println(res.getString(1)); } conn.close(); conn = null; } catch (ClassNotFoundException e) { e.printStackTrace(); log.error(driverName + " not found!", e); System.exit(1); } catch (SQLException e) { e.printStackTrace(); log.error("Connection error!", e); System.exit(1); } } }
3). 運行結果(右擊-->Run as-->Run on Hadoop)
Running:show tables 'testHiveDriverTable' 執行“show tables”運行結果: testhivedrivertable Running:describe testHiveDriverTable 執行“describe table”運行結果: key int value string Running:load data local inpath '/home/hadoop/ziliao/userinfo.txt' into table testHiveDriverTable Running:select * from testHiveDriverTable 執行“select * query”運行結果: 1 xiapi 2 xiaoxue 3 qingqing Running:select count(1) from testHiveDriverTable 執行“regular hive query”運行結果: 3