简体   繁体   English

使用java使用语句类的单个execute()方法执行多个hive查询

[英]Execute multiple hive queries using single execute() method of statement class using java

I am using Java API to access HiveServer2, I have requirement of executing multiple hive queries in single call to execute() method of statements class.我正在使用 Java API 访问 HiveServer2,我需要在对 statements 类的execute()方法的单个调用中执行多个 hive 查询。 Is it possible to submit multiple queries of hive in one call to execute() method.是否可以在一次调用execute()方法中提交多个 hive 查询。 I have hive properties to set as:我有配置单元属性设置为:

SET hive.exec.max.created.files=200000;
SET hive.exec.compress.output=true;
SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
SET hive.exec.dynamic.partition = true; 
SET hive.exec.dynamic.partition.mode = nonstrict; 
set hive.exec.max.dynamic.partitions=5000;
set hive.exec.max.dynamic.partitions.pernode=5000; 
              .
              .
Statement stmt = con.createStatement();
stmt.execute("SET hive.exec.max.created.files=200000");
              .
              .

for that now I am setting these properties one at a time using execute() method.为此,我现在使用execute()方法一次设置一个这些属性。 Is there any way so i can set all these properties by executing all above statement in one call to execute() method.有什么方法可以通过在一次调用execute()方法中执行所有上述语句来设置所有这些属性。
Expected:预期:

stmt.execute("SET hive.exec.max.created.files=200000;
    SET hive.exec.dynamic.partition = true; 
    SET hive.exec.dynamic.partition.mode = nonstrict; 
    set hive.exec.max.dynamic.partitions=5000;
    set hive.exec.max.dynamic.partitions.pernode=5000;");


Thanks.谢谢。

Answer is big NO.答案是大不。 You cannot execute multiple queries in a single execute() method.您不能在单个 execute() 方法中执行多个查询。 Why can't you do the same in a for loop like below为什么你不能在像下面这样的 for 循环中做同样的事情

String[] queries = new String[] {"SET hive.exec.dynamic.partition = true", "set hive.exec.max.dynamic.partitions=5000"}; //and so on

Statement stmt = con.createStatement();

for(int i = 0; i < queries.length; i++){
   stmt.execute(queries[i]);
}

This applies for multiple hive sql queries too like select, insert, delete, etc,.这也适用于多个 hive sql 查询,例如选择、插入、删除等。 based on the sql statement resultset print or populate in the JTable.基于 sql 语句结果集打印或填充到 JTable 中。

Note : execute(), executeUpdate() and executeQuery() are not the same.注意:execute()、executeUpdate() 和executeQuery() 不一样。 Choose based on your sql statement.根据你的sql语句选择。

execute() - Returns true if the first object that the query returns is a ResultSet object. execute() - 如果查询返回的第一个对象是 ResultSet 对象,则返回 true。

executeUpdate() - Returns an integer representing the number of rows affected by the SQL statement. executeUpdate() - 返回一个整数,表示受 SQL 语句影响的行数。

executeQuery() - Returns one ResultSet object. executeQuery() - 返回一个 ResultSet 对象。

Reference: docs.oracle.com/javase参考: docs.oracle.com/javase

HTH HTH

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

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