【问题标题】:Exception while fetching the data from hive2 json-serde table in java program从java程序中的hive2 json-serde表中获取数据时出现异常
【发布时间】:2016-01-04 07:45:29
【问题描述】:

我正在使用 https://github.com/rcongiu/Hive-JSON-Serde 这个 json serde。 我在将 json serde jar 添加到控制台后进行查询,它将数据返回给我。我正在尝试对 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}]

我正在尝试编写一个从 hive 表中获取数据的程序。 数据为 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>

【问题讨论】:

  • 你用的是maven吗?如果是,则必须在 pom.xml 中包含依赖项。你能展示你的pom吗?如果您不使用 maven,则必须将 .jar 包含在您的库文件夹中。
  • 请查看上面附加的 pom 文件。我指向物理下载的 jar,甚至可以在 HiveTableExample 类中创建一个实例。

标签: java json hadoop hive


【解决方案1】:

要回答这个问题,让我先解释一下 serde 的工作原理。 SerDe 是一种向 hive 添加新功能的方法,它提供了一个可扩展的接口来插入数据格式,例如 JSON。

作为 hive 的扩展,serde 的代码必须可用于集群中的所有节点。 使用 hive shell 时,您可以通过将 serde 放入 EXTRA_LIBS 目录或告诉您的脚本 ADD JAR serde.jar 来实现。 hive shell 为您做的实际上是每次运行查询时获取 serde 并将其发送到所有节点。

现在,至于你的问题。您使用的不是 shell,而是 JDBC API,它与 hiveserver 进程而不是 hive shell 对话。 您不需要在 maven 项目中包含 serde,因为 JDBC API 不会像 hive shell 那样自动为您分发 JAR。 您需要做的是将 serde 安装在您正在与之交谈的 hive 服务器的额外 libs 目录中

所以,这是配置问题,而不是您的代码问题。 与此问题无关,但最好使用try { ...} finally { ..} 关闭连接

【讨论】:

  • 非常感谢罗伯托的帮助。我会按照惯例:)
  • Roberto Congiu,我把这个 jar 添加到 /hadoop/CDH_5.2.0_Linux_parcel/parcels/CDH-5.2.0-1.cdh5.2.0.p0.36/lib/hive/lib 路径,一些查询工作取决于 json-serde。但是对于某些查询 map-reduce 作业被调用,它无法检测到 json serde jar 文件,它抛出了未找到的异常。你能告诉我我需要添加这个jar的mapreduce框架的位置是什么吗?
  • 我已将我的 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 它对我有用。
猜你喜欢
  • 2014-03-04
  • 2021-08-30
  • 2023-04-09
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 2016-08-12
相关资源
最近更新 更多