我正在使用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>
的連接是否良好?如果是的話,你必須在你的pom中包含依賴。你能展示你的pom嗎?如果您不使用maven,則必須將.jar包含在庫文件夾中。 – Patrick
請看上面附帶的pom文件。 我指出了物理下載的jar,甚至能夠在HiveTableExample類中創建一個實例。 –