2016-01-04 24 views
0

我正在使用https://github.com/rcongiu/Hive-JSON-Serde這個json serde。 我在將json serde jar添加到控制檯後執行查詢,它將數據返回給我。我試圖用java代碼做同樣的事情,但沒有發生。在java程序中從hive2 json-serde表中獲取數據時的異常

hive> use oracle_json; 
OK 
Time taken: 0.858 seconds 

hive> add jar json-serde-1.3.6-jar-with-dependencies.jar; 

Added json-serde-1.3.6-jar-with-dependencies.jar to class path 
Added resource: json-serde-1.3.6-jar-with-dependencies.jar 

hive> select * from oracle_trading limit 1; 
OK 
[{"close_date":"2015-08-09 16:59:37.000000000","instrument_type":"Options","units":95000.0,"created_date":"2011-05-03 16:59:37.000000000","empid":10776,"instrument":"Instrument442","id":442,"open_date":null,"customer_id":870,"indexname":"FTSE","currency":null,"empsal":null}] 

我想寫一個程序,它將從配置單元表中獲取數據。 數據爲json serde格式。從json serde表中獲取數據時我收到異常。 特別是我不知道如何反序列化來自hive2服務器的數據,也不知道如何通過java代碼使用這個json serde jar。你能幫我做同樣的事情嗎?

 package com.db.hive; 
     import java.sql.Connection; 
     import java.sql.DriverManager; 
     import java.sql.ResultSet; 
     import java.sql.SQLException; 
     import java.sql.Statement; 
     import org.openx.data.jsonserde.JsonSerDe; 
    /*This jsonSerDe library I have added to POM file BUT do not know how to use 
while executing the executeQuery() method 
     */ 
     public class HiveTableExample { 

      private static String driverName = "org.apache.hive.jdbc.HiveDriver"; 
      final static String url = "jdbc:hive2://xxxx:10000/oracle_json"; 
      final static String user_name = "xxxx"; 
      final static String pwd = "xxxxx"; 
      private static JsonSerDe de = null; 

      public static void main(String[] args) throws SQLException { 
       try { 
        Class.forName(driverName); 
       } catch (ClassNotFoundException e) { 
        System.exit(1); 
       } 
       Connection con = DriverManager.getConnection(url, user_name, pwd); 
       Statement stmt = con.createStatement(); 

       String sql = "select * from oracle_trading limit 10"; 
       System.out.println("Running: " + sql); 
       ResultSet res = stmt.executeQuery(sql);    

       while (res.next()) { 
        System.out.println(String.valueOf(res.getString(1)) + "\t" + res.getString(2)); 
       } 
      } 
     } 

我收到異常,如下所示。 ... ...

Running: select * from oracle_trading limit 10 
Exception in thread "main" org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: RuntimeException MetaException(message:java.lang.ClassNotFoundException Class org.openx.data.jsonserde.JsonSerDe not found) 
    at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:231) 
    at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:217) 
    at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:254) 
    at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:392) 
    at com.db.hive.HiveTableExample.main(HiveTableExample.java:42) 

我的POM文件

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.db.hive</groupId> 
    <artifactId>HiveQuery</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <version>2.4.1</version> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>single</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <mainClass>com.db.hive.HiveTableExample</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 

     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.openx.data</groupId> 
      <artifactId>json-serde</artifactId> 
      <version>1.3.6-SNAPSHOT-jar-with-dependencies</version> 
      <scope>system</scope> 
      <systemPath>C:\Users\mahendra.pansare\Documents\NetBeansProjects\HiveQuery\src\main\resources\json-serde-1.3.6-SNAPSHOT-jar-with-dependencies.jar</systemPath> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hive</groupId> 
      <artifactId>hive-jdbc</artifactId> 
      <version>1.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-core</artifactId> 
      <version>1.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-client</artifactId> 
      <version>2.7.0</version> 
     </dependency> 



    </dependencies> 


</project> 
+0

的連接是否良好?如果是的話,你必須在你的pom中包含依賴。你能展示你的pom嗎?如果您不使用maven,則必須將.jar包含在庫文件夾中。 – Patrick

+1

請看上面附帶的pom文件。 我指出了物理下載的jar,甚至能夠在HiveTableExample類中創建一個實例。 –

回答

1

要回答這個問題,讓我先解釋一下SERDE是如何工作的。 SerDe是向蜂巢添加新功能的一種方法,提供了一個可擴展的接口來插入數據格式,如JSON。

作爲配置單元的擴展,serde的代碼必須可用於羣集中的所有節點。 使用配置單元外殼時,可以通過將serde放入EXTRA_LIBS目錄,或者告訴腳本ADD JAR serde.jar。 hive shell爲你做的事情實際上是在每次運行查詢時將serde發送到所有節點。

現在,至於你的問題。您沒有使用shell,而是JDBC API,它與hiveserver進程而不是hive shell進行通信。 您不需要在maven項目中包含serde,因爲JDBC API不像您的配置單元那樣自動爲您分配JAR。 你需要做的是在你說的蜂巢服務器的額外libs目錄安裝SERDE到

所以,這是一個結構問題,而不是你的代碼有問題。 與此問題無關,但使用maven關閉使用try { ...} finally { ..}

+0

非常感謝Roberto的幫助。我會按照練習:) –

+0

Roberto Congiu, 我將這個jar添加到/hadoop/CDH_5.2.0_Linux_parcel/parcels/CDH-5.2.0-1.cdh5.2.0.p0.36/lib/hive/lib path,有些查詢是依賴於json-serde的。 但是對於某些查詢map-reduce作業被調用,它無法檢測到json serde jar文件,它正在拋出它未找到的異常。 你能告訴我什麼是mapreduce框架的位置,我需要添加這個jar嗎? –

+1

我已將我的jar添加到/hadoop/CDH_5.2.0_Linux_parcel/parcels/CDH-5.2.0-1.cdh5.2.0.p0.36/lib/hadoop-mapreduce/lib/json-serde-1.3.6-jar -with-dependencies.jar和/hadoop/CDH_5.2.0_Linux_parcel/parcels/CDH-5.2.0-1.cdh5.2.0.p0.36/lib/hive/lib/json-serde-1.3。每個節點的6-jar-with-dependencies.jar,它爲我工作。 –

相關問題