1. 程式人生 > >Java使用JDBC連線Hive(新版本)API封裝

Java使用JDBC連線Hive(新版本)API封裝

網上找了很多封裝的API,發現都是過時了的,執行報各種錯誤,經過了幾天的調錯,終於可以使用java程式碼操作hive了

首先看看所需的包

這裡寫圖片描述

所有的分析都在程式碼裡面

注意:網上很多程式碼對於DDL都執行 res = stmt.executeQuery(sql);
這是錯的,因為新版本DDL不能返回結果集,會報如下錯誤
java.sql.SQLException: The query did not generate a result set!
所以只能寫 stmt.execute(sql);
它會返回一個boolean值
只有對於DML才能返回結果集
具體看下面的程式碼大家就懂了,不信的話大家可以試試,我的是1.1.1版本

package com.berg.hive.test1.api;

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;  

/** 
 * Hive的JavaApi 
 *  
 * 啟動hive的遠端服務介面命令列執行:hive --service hiveserver & 
 *  
 * @author
湯高 * */
public class HiveJdbcCli { //網上寫 org.apache.hadoop.hive.jdbc.HiveDriver ,新版本不能這樣寫 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; //這裡是hive2,網上其他人都寫hive,在高版本中會報錯 private static String url = "jdbc:hive2://master:10000/default"; private static String user = "hive"
; private static String password = "hive"; private static String sql = ""; private static ResultSet res; private static final Logger log = Logger.getLogger(HiveJdbcCli.class); public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { conn = getConn(); stmt = conn.createStatement(); // 第一步:存在就先刪除 String tableName = dropTable(stmt); // 第二步:不存在就建立 createTable(stmt, tableName); // 第三步:檢視建立的表 showTables(stmt, tableName); // 執行describe table操作 describeTables(stmt, tableName); // 執行load data into table操作 loadData(stmt, tableName); // 執行 select * query 操作 selectData(stmt, tableName); // 執行 regular hive query 統計操作 countData(stmt, tableName); } 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); } finally { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void countData(Statement stmt, String tableName) throws SQLException { 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("count ------>" + res.getString(1)); } } private static void selectData(Statement stmt, String tableName) throws SQLException { 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)); } } private static void loadData(Statement stmt, String tableName) throws SQLException { //目錄 ,我的是hive安裝的機子的虛擬機器的home目錄下 String filepath = "user.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); stmt.execute(sql); } private static void describeTables(Statement stmt, String tableName) throws SQLException { 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)); } } private static void showTables(Statement stmt, String tableName) throws SQLException { 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)); } } private static void createTable(Statement stmt, String tableName) throws SQLException { sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t'"; stmt.execute(sql); } private static String dropTable(Statement stmt) throws SQLException { // 建立的表名 String tableName = "testHive"; sql = "drop table " + tableName; stmt.execute(sql); return tableName; } private static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } }