简体   繁体   中英

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. Is it possible to submit multiple queries of hive in one call to execute() method. 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. Is there any way so i can set all these properties by executing all above statement in one call to execute() method.
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. Why can't you do the same in a for loop like below

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,. based on the sql statement resultset print or populate in the JTable.

Note : execute(), executeUpdate() and executeQuery() are not the same. Choose based on your sql statement.

execute() - Returns true if the first object that the query returns is a ResultSet object.

executeUpdate() - Returns an integer representing the number of rows affected by the SQL statement.

executeQuery() - Returns one ResultSet object.

Reference: docs.oracle.com/javase

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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