我尝试过使用jdbc来操作hive中的数据。当我使用选择或创建操作时,一切正常。但是当我尝试使用 insert 将一些行添加到表中时,例如:
String sql = "insert into agg_test_20170508 values (20170508, 100)";
Class.forName(JDBC_Driver);
connection = DriverManager.getConnection(JDBC_String, ID, pwd);
Statement stmt = connection.createStatement();
System.out.println(D_FORMAT.format(new Date()) + " Running:\r\n" + sql);
ResultSet res = stmt.executeQuery(sql);
System.out.println(D_FORMAT.format(new Date()) + " QUERY COMPELET.");
但是过了很长一段时间,什么也没有返回。使用jdbc连接hive有什么限制吗?
我公司的hadoop版本是0.20.2。
最佳答案
您必须将 executeUpdate
与 INSERT UPDATE DELETE 一起使用:
int res = stmt.executeUpdate(sql);
int executeUpdate(String sql): executes an INSERT, UPDATE or DELETE statement and returns an update account indicating number of rows affected (e.g. 1 row inserted, or 2 rows updated, or 0 rows affected).
ResultSet executeQuery(String sql): executes a SELECT statement and returns a ResultSet object which contains results returned by the query.
关于java - 如何通过jdbc向hive表插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43848000/