将数据加载到表配置单元中

时间:2015-01-05 15:56:33

标签: eclipse hadoop jdbc hive

我试图配置Hive以使用JDBC,我在eclipse上使用了这个例子:

public class HiveJdbcClient {
    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
    /**
    * @param args
    * @throws SQLException
    **/
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(1);
        }
        Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
        Statement stmt = con.createStatement();
        String tableName = "testHiveDriverTable";
        stmt.executeQuery("drop table " + tableName);
        ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");

        // show tables
        String sql = "show tables '" + tableName + "'";
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        if (res.next()) {
            System.out.println(res.getString(1));
        }

        // describe table
        sql = "describe " + tableName;
        System.out.println("Running: " + sql);  
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1) + "\t" + res.getString(2));
        }

        // load data into table
        // NOTE: filepath has to be local to the hive server
        // NOTE: /tmp/test_hive_server.txt is a ctrl-A separated file with two fields per line
        String filepath = "/tmp/test_hive_server.txt";
        sql = "load data local inpath '" + filepath + "' into table " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        // select * query
        sql = "select * from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()){
            System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
        }
        // regular hive query
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()){
            System.out.println(res.getString(1));
        }
    }
}

我能够在配置单元上创建表,但是当我尝试将数据加载到表中时,会出现错误。所以我的问题是我应该放入什么" test_hive_server.txt"让它工作!因为我尝试了一切,每次我都得到同样的错误。 谢谢!

错误:

Exception in thread "main" java.sql.SQLException: org.apache.thrift.TApplicationException: Internal error processing execute
    at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:196)
    at com.palmyra.nosql.HiveJdbcClient.main(HiveJdbcClient.java:52)

1 个答案:

答案 0 :(得分:1)

我自己找到了答案:

ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';

确定我们要在表中加载的“file.txt”中的行格式,Hive的默认记录和字段分隔符列表是:

\n

^A

^B

^C

press ^V^A could insert a ^A in Vim.

或者您可以这样做:  create table tableName (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';

和'file.txt'应该是这样的:

value1 value2
value3 value4

在此示例中,value1和value3表示密钥 value4和value5代表值

相关问题