hive查询无法通过jdbc生成结果集 [英] hive query can't generate result set via jdbc

查看:576
本文介绍了hive查询无法通过jdbc生成结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hive和Hadoop的新手。在我的教程中,我想创建表格作为

  import java.sql.SQLException; 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveCreateDb {
private static String driverName =org.apache.hive.jdbc.HiveDriver;

public static void main(String [] args){
//注册驱动并创建驱动实例

try {
Class.forName(driverName );

//获取连接

连接con = DriverManager.getConnection(jdbc:hive2:// localhost:10000 / mydb,root,);
Statement stmt = con.createStatement();

$ b stmt.executeQuery(CREATE TABLE IF NOT EXISTS+employee(e_id int,name String,
+salary String)+STORED AS TEXTFILE );

System.out.println(Operation done successfully。);

con.close();
} catch(ClassNotFoundException e){
System.err.println(class not found !!!!);
e.printStackTrace();
} catch(SQLException e){
System.err.println(sql exception !!!);
e.printStackTrace();
}

}
}

当我运行代码我得到这个错误

  sql异常!!! java.sql.SQLException:查询没有生成结果集! at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:393)
at tr.com.vedat.hive.HiveCreateDb.main(HiveCreateDb.java:25)

所以,我找不到解决方案。任何人都可以帮助我找到bug并导致我正确的方式吗?

解决方案

看起来Hive驱动程序随着时间的推移而变化随着时间的推移更具限制性请参阅此 Google小组讨论



对于创建新表的查询,使用 stmt.execute()。的executeQuery。 executeQuery()现在仅适用于select查询(DML),而execute可能适用于DDL(数据定义)。



这很有道理,因为我在其他语言(Python和C#)中看到的大多数驱动程序将会将只读操作与实际上可以更改数据结构的方法分开。



这个页面显示了executeQuery( )为DDL:

  ResultSet res = stmt.executeQuery(create table+ tableName +(key int ,值字符串)); 

这里的 execute 的例子是Python,所以您可以注意到Java示例中的所有DDL都使用 executeQuery


I'm new at Hive and Hadoop. In my tutorial, I want to create table as

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveCreateDb {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";

public static void main(String[] args) {
    // Register driver and create driver instance

    try {
        Class.forName(driverName);

        // get connection

        Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/mydb", "root", "");
        Statement stmt = con.createStatement();


        stmt.executeQuery("CREATE TABLE IF NOT EXISTS " + " employee ( e_id int, name String, "
                + " salary String)" + " STORED AS TEXTFILE");

        System.out.println("Operation done successfully.");

        con.close();
    } catch (ClassNotFoundException e) {
        System.err.println("class not found!!!!");
        e.printStackTrace();
    } catch (SQLException e) {
        System.err.println("sql exception!!!");
        e.printStackTrace();
    }

 }
}

When I run code I get this error

sql exception!!! java.sql.SQLException: The query did not generate a result set! at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:393)
at tr.com.vedat.hive.HiveCreateDb.main(HiveCreateDb.java:25)

So, I couldn't find solution. Can anyone help me to find bug and lead to me correct way?

解决方案

It looks like the Hive driver changed over time to be more restrictive over time. See this Google Group discussion

Use stmt.execute() for a query that makes a new table. of executeQuery. The executeQuery() is now only for select queries (DML) while execute is probably for DDL (data definition).

This makes sense because most drivers I've seen in other languages (Python and C# in my case) will separate the read only actions from the method that can actually change the data structure.

This page shows the usage of executeQuery() for DDL:

 ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");

The examples of execute here are Python, so you can note that all the DDL in the Java example uses executeQuery.

这篇关于hive查询无法通过jdbc生成结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆