![](/img/trans.png)
[英]Getting exception while fetching data from cloudant DB using selector through Java program
[英]Exception while fetching the data from hive2 json-serde table in java program
我正在使用https://github.com/rcongiu/Hive-JSON-Serde这个json serde。 我在将json serde jar添加到console后进行查询,它将数据返回给我。 我正在尝试使用Java代码做同样的事情,但是没有发生。
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>
为了回答这个问题,让我首先解释一下Serde是如何工作的。 SerDe是一种向蜂巢添加新功能的方法,它提供了可扩展的接口来插入JSON之类的数据格式。
作为配置单元的扩展,SERDE的代码必须对集群中的所有节点都可用。 使用配置单元外壳时,可以通过将Serde放入EXTRA_LIBS目录中,或者通过告诉脚本ADD JAR serde.jar
。 蜂巢壳为您提供的服务实际上是在每次运行查询时获取Serde并将其发送到所有节点。
现在,关于您的问题。 您不是在使用Shell,而是在使用JDBC API,该API与hiveserver进程而不是hive shell进行通信。 您不需要在您的maven项目中包含serde,因为JDBC API不会像配置单元外壳一样自动为您分发JAR。 您需要做的是将Serde安装在您要与之通信的Hive 服务器的额外libs目录中
因此,这是配置问题,而不是代码问题。 与此问题无关,但最好使用try { ...} finally { ..}
来关闭连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.