hive query can't generate result set via jdbc

2019-05-23 18:16发布

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?

1条回答
你好瞎i
2楼-- · 2019-05-23 18:58

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.

查看更多
登录 后发表回答