Created 11-10-2017 11:18 PM
I have use case that to execute .sql file from java connecting to hive server. SQL file is added with the Jars(SQL file has many statements). the final output of it will be a text file. Basically I have to achieve the below command in java and read the output file out of it. I have gone through many blogs but no luck. Please help me with the examples. or how can i achieve this use case.
hive -f test.sql > test.txt
Created 11-13-2017 12:34 PM
This is the best example for connecting to hive server from Java using JDBC : https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBCClientSa...
You only need to modify the code little bit to read queries from your file and store the result set to a file instead of printing .
But if you are interested in directly executing "hive -f test.hql" from Java then you can create a runtime process and execute it .
p = Runtime.getRuntime().exec((new
String[]{"hive","-f","/hive/scripts/test.hql"}));
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}
Created 11-13-2017 12:34 PM
This is the best example for connecting to hive server from Java using JDBC : https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBCClientSa...
You only need to modify the code little bit to read queries from your file and store the result set to a file instead of printing .
But if you are interested in directly executing "hive -f test.hql" from Java then you can create a runtime process and execute it .
p = Runtime.getRuntime().exec((new
String[]{"hive","-f","/hive/scripts/test.hql"}));
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}
Created 10-30-2018 02:38 PM
Hi @rtrivedi
I have the same question and find this answer. And it worked properly!
But I have another question about this answer. Why can't use:
String command = "hive -f /hive/scripts/test.hql"; p =Runtime.getRuntime().exec(command);
or
String command = "sh hive -f /hive/scripts/test.hql"; p =Runtime.getRuntime().exec(command);
I try both of them and they didn't work.
Created 11-01-2018 12:40 PM
I think I find the answer.
The reason that
String command = "hive -f /hive/scripts/test.hql"; p =Runtime.getRuntime().exec(command);
or
String command = "sh hive -f /hive/scripts/test.hql"; p =Runtime.getRuntime().exec(command);
can't work is:
the Runtime.getRuntime().exec() method bring the full string to bash. But the bash parser can't understand the command. Bash parser need to parse the command step by step:
A full string can't be parsed.
Created 11-14-2017 06:12 AM
Thanks a lot, RTrivedi for your response. It's working. And for my use-case, i used JSCH to connect hive server.
Created 12-06-2017 08:26 AM
Great to know that your problem is resolved . Can you please accept this answer .
Thanks !