HIVE JDBC方法連接

輸入

hiveserver2

打開hive server2

在之前的學習和實踐Hive中,使用的都是CLI或者hive –e的方式,該方式僅允許使用HiveQL執行查詢、更新等操作,並且該方式比較笨拙單一。幸好Hive提供了輕客戶端的實現,通過HiveServer或者HiveServer2,客戶端可以在不啓動CLI的情況下對Hive中的數據進行操作,兩者都允許遠程客戶端使用多種編程語言如Java、Python向Hive提交請求,取回結果。HiveServer或者HiveServer2都是基於Thrift的,但HiveSever有時被稱爲Thrift server,而HiveServer2卻不會。既然已經存在HiveServer爲什麼還需要HiveServer2呢?這是因爲HiveServer不能處理多於一個客戶端的併發請求,這是由於HiveServer使用的Thrift接口所導致的限制,不能通過修改HiveServer的代碼修正。因此在Hive-0.11.0版本中重寫了HiveServer代碼得到了HiveServer2,進而解決了該問題。HiveServer2支持多客戶端的併發和認證,爲開放API客戶端如JDBC、ODBC提供了更好的支持。

輸入

beeline

測試

#!connect jdbc:hive2://IP地址:10000
beeline> !connect jdbc:hive2://cm1:10000
scan complete in 4ms
Connecting to jdbc:hive2://cm1:10000
#用戶名 直接回車
Enter username for jdbc:hive2://cm1:10000: 
#密碼 直接回車
Enter password for jdbc:hive2://cm1:10000: 
Connected to: Apache Hive (version 1.1.0-cdh5.14.4)
Driver: Hive JDBC (version 1.1.0-cdh5.14.4)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://cm1:10000>

在這裏插入圖片描述

在這裏插入圖片描述

打開IDE
建立maven工程
pom.xml添加

<dependency> 
        <groupId>org.apache.hive</groupId> 
        <artifactId>hive-jdbc</artifactId> 
        <version>1.2.1</version> 
</dependency> 
    
<dependency> 
        <groupId>org.apache.hadoop</groupId> 
        <artifactId>hadoop-common</artifactId> 
       <version>2.4.1</version> 
</dependency> 
   
<dependency> 
       <groupId>jdk.tools</groupId> 
       <artifactId>jdk.tools</artifactId> 
       <version>1.6</version> 
       <scope>system</scope> 
       <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> 
   </dependency> 

注意這裏的版本要與你的hive版本一致
如果出現:

java.sql.SQLException: Could not establish connection to jdbc:hive2://:**** Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=***})
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:594)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:192)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=***})
at org.apache.thrift.TApplicationException.read(TApplicationException.java:111)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:71)
at org.apache.hive.service.cli.thrift.TCLIService$Client.recv_OpenSession(TCLIService.java:156)
at org.apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:143)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:583)
... 10 more

說明你的hive-jdbc版本不對

查看hive版本方法
ive 沒有直接查詢當前版本的操作, 不過我們可以通過查詢 jar 包的方式間接知道當前 hive 的版本.
在這裏插入圖片描述
可知我的hive版本爲1.1.0
編寫程序
test:

package hive_test;

import java.sql.Connection;import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
public class test {
    private static String driverName ="org.apache.hive.jdbc.HiveDriver";   // 此Class 位於 hive-jdbc的jar包下
    private static String Url="jdbc:hive2://222.22.91.81:10000/";    //填寫hive的IP,之前在配置文件中配置的IP
    private static Connection conn;
    public static Connection getConnnection()
    {
        try
               {
                  Class.forName(driverName);
                  conn = DriverManager.getConnection(Url,"","");        //只是連接hive, 用戶名可不傳
               }
        catch(ClassNotFoundException e)  {
                   e.printStackTrace();
                   System.exit(1);
                }
         catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static PreparedStatement prepare(Connection conn, String sql) {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
}

test2:

package hive_test;

import java.sql.Connection;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class test2 {
    private static Connection conn=test.getConnnection();
    private static PreparedStatement ps;
    private static ResultSet rs;
    public static void getAll(String tablename)
    {
        String sql="select * from "+tablename;
        System.out.println(sql);
        try {
            ps=test.prepare(conn, sql);
            rs=ps.executeQuery();
            int columns=rs.getMetaData().getColumnCount();
            while(rs.next())
            {
                for(int i=1;i<=columns;i++)
                {
                    System.out.print(rs.getString(i));  
                    System.out.print("\t\t");
                }
                System.out.println();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }

	public static void main(String[] args) { 
		String tablename="students";
        test2.getAll(tablename);
    }
 
}

在這裏插入圖片描述

編譯
在這裏插入圖片描述
clean compile package -DskipTests
在這裏插入圖片描述
選擇jdk
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章