简体   繁体   English

如何通过jdbc将数据插入Hive表中?

[英]How to insert data into hive table through jdbc?

I've tried to use jdbc to operate data in hive. 我试图使用jdbc在蜂巢中操作数据。 When I used select or create operation, everything worked fine. 当我使用选择或创建操作时,一切正常。 But when I tried to use insert to add some rows into a table like: 但是,当我尝试使用插入向表中添加一些行时,例如:

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.");

But after a long time, nothing returned. 但是很长一段时间后,什么也没有返回。 Are there any restriction when using jdbc to connect to hive? 使用jdbc连接到配置单元时是否有任何限制?

The hadoop version in my company is 0.20.2. 我公司的hadoop版本是0.20.2。

You have to use executeUpdate with INSERT UPDATE DELETE : 您必须将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 (eg 1 row inserted, or 2 rows updated, or 0 rows affected). int executeUpdate(String sql):执行INSERT,UPDATE或DELETE语句并返回一个更新帐户,该帐户指示受影响的行数(例如,插入1行,或更新2行,或影响0行)。

ResultSet executeQuery(String sql): executes a SELECT statement and returns a ResultSet object which contains results returned by the query. ResultSet executeQuery(String sql):执行SELECT语句并返回ResultSet对象,该对象包含查询返回的结果。

请使用executeUpdate方法而不是executeQuery方法

executeQuery() --- This is used generally for reading the content of the database. executeQuery()---通常用于读取数据库的内容。 The output will be in the form of ResultSet. 输出将以ResultSet的形式出现。 Generally SELECT statement is used. 通常使用SELECT语句。

executeUpdate() --- This is generally used for altering the databases. executeUpdate()---这通常用于更改数据库。 Generally DROP TABLE or DATABASE, INSERT into TABLE, UPDATE TABLE, DELETE from TABLE statements will be used in this. 通常,将在此使用DROP TABLE或DATABASE,将INSERT插入TABLE,UPDATE TABLE,从TABLE语句删除。 The output will be in the form of int. 输出将为int形式。 This int value denotes the number of rows affected by the query. 此int值表示受查询影响的行数。

execute() --- If you dont know which method to be used for executing SQL statements, this method can be used. execute()---如果您不知道要使用哪种方法执行SQL语句,则可以使用此方法。 This will return a boolean. 这将返回一个布尔值。 TRUE indicates the result is a ResultSet and FALSE indicates it has the int value which denotes number of rows affected by the query. TRUE表示结果为ResultSet,FALSE表示结果具有int值,该值表示受查询影响的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM