Java – How to create a table in a hive using java?

How to create a table in a hive using java?… here is a solution to the problem.

How to create a table in a hive using java?

I want to create a table in the hive using Java. Use the following to do this:

public class HiveCreateTable {
    private static String driverName = "com.facebook.presto.jdbc.PrestoDriver";
    public static void main(String[] args) throws SQLException {
         Register driver and create driver instance
        try {
            Class.forName(driverName);
        } 
        catch (ClassNotFoundException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("haiiiiii");
        Connection con = DriverManager.getConnection("jdbc:presto://192.168.1.119:8082/default", "hadoop", "password");
        con.setCatalog("hive");
        con.setSchema("log");
        Statement stmt = con.createStatement();
        ResultSet res = stmt.executeQuery("create table access_log2  (cip string, csusername string, cscomputername string)");
        System.out.println("Table created.");
        con.close();
    }
}

Error:

Exception in thread “main” java.sql.SQLException: Query failed
(#20150731_101653_00008_hv68j): Unknown type for column ‘cip’

Solution

In the code above, you are using the Presto JDBC driver, not Hive. I’m not sure what you want to implement, but Presto supports the following data types, where String is not a :

https://prestodb.io/docs/current/language/types.html

Related Problems and Solutions