- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
how to run hive SQL file from java?
- Labels:
-
Apache Hive
Created ‎11-10-2017 11:18 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
- Read data to execute
- Process quotes
- Split the read data into commands
- Parse special operators
- Perform expansions
- Split the command into a command name and arguments
- Execute the command
A full string can't be parsed.
Created ‎11-14-2017 06:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Great to know that your problem is resolved . Can you please accept this answer .
Thanks !
