天天看點

Java調用Hive的操作

     如何在Java中調用Hive的操作步驟如下:

1、啟動Hive遠端服務:

      指令: hive --service hiveserver2  &

      啟動成功,指令框出現以下界面:

Java調用Hive的操作

2、在Eclipse中建立Hadoop 項目,導入Hive的必須包

  Hive的必須包如下:

   hive-exec-2.0.0.jar

Java調用Hive的操作

   hive-jdbc-2.0.0.jar

   hive-service-2.0.0.jar

   httpclient-4.4.jar

   httpcore-4.4.jar

3、編寫Java調用Hive的相關函數,代碼如下:

package com;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class HiveTestCase {

public static void query(Statement stmt) throws Exception {

String querySQL = "select a.* from java_test a";

ResultSet res = stmt.executeQuery(querySQL); // 執行查詢語句

while (res.next()) {

System.out.print("id:" + res.getString("id") + " ");

System.out.println("name:" + res.getString(2) + " ");

}

}

public static void create(Statement stmt) throws Exception {

String createSQL = "create table java_test (id string, name string) row format delimited fields terminated by '\t' ";

boolean bool = stmt.execute(createSQL);

System.out.println("建立表是否成功:" + bool);

}

public static void drop(Statement stmt) throws Exception {

String dropSQL = "drop table java_test";

boolean bool = stmt.execute(dropSQL);

System.out.println("删除表是否成功:" + bool);

}

public static void load(Statement stmt) throws Exception {

String loadSQL = "load data local inpath '/home/hadoop/test' into table java_test ";

boolean bool = stmt.execute(loadSQL);

System.out.println("導入資料是否成功:" + bool);

}

public static void main(String[] args) throws Exception {

Class.forName("org.apache.hive.jdbc.HiveDriver");

Connection con = DriverManager.getConnection(

"jdbc:hive2://192.168.26.131:10000/test", "", "");

Statement stmt = con.createStatement();

drop(stmt); // 執行删除語句

create(stmt); // 執行建表語句

load(stmt); // 執行導入語句

query(stmt); // 執行查詢語句

}

}

4、如果出現錯誤:User: hadoop is not allowed to impersonate 

解決:在hive-site.xml檔案中添加一下配置:

   <property>

      <name>hive.metastore.sasl.enabled</name>

      <value>false</value>

      <description>If true, the metastore Thrift interface will be secured with SASL.  

                   Clients must authenticate with Kerberos.</description>

   </property>

   <property>

       <name>hive.server2.enable.doAs</name>

       <value>false</value>

   </property>

   <property>

       <name>hive.server2.authentication</name>

       <value>NONE</value>

   </property>

   參考連結:

   http://stackoverflow.com/questions/36909002/authorizationexception-user-not-allowed-to-impersonate-user

5、操作過程存在的疑問:

執行以上HQL語句,都能在Hive中成功執行建立表、删除表、導入資料等,但是函數傳回的布爾值為什麼為 false 呢?

解答:

這個execute方法執行一個sql語句并且指向第一個傳回值。

傳回值:true表示第一個傳回值是一個ResultSet對象;false表示這是一個更新個數或者沒有結果集。   

繼續閱讀