beeline -u jdbc:hive2://localhost:10000 -e "show table" -e "show t" -e "show tables"In the bash script environment, the exit code is the only way to catch beeline’s failure reliably.
beeline -u jdbc:hive2://localhost:10000 -e "show table" -e "show t" -e "show tables"In the bash script environment, the exit code is the only way to catch beeline’s failure reliably.
My new Snowflake Blog is now live. I will not be updating this blog anymore but will continue with new contents in the Snowflake world!
@ericlin05 hello, saw your blogs, very helpful. I am looking for sample Java code which can be used to trigger beeline commands. We use Shell Script to do it, but would need to explore Java/Python. Any help would be much appreciated.
Hi Sreeraj,
Thanks for your visit and question on my blog. I can see you would like to use Java to connect to beeline to run Hive query, correct? Beeline is good to use on command line environment, however, it is not suitable for Java. If you need to use Java to run query, you will need to use JDBC drivers for Hive.
Cloudera provides Hive JDBC driver, documentation can be found here:
http://www.cloudera.com/documentation/other/connectors/hive-jdbc/latest/Cloudera-JDBC-Driver-for-Apache-Hive-Install-Guide.pdf
I also have an example code on github, you can have a look. It is a very simple example to run Hive query. You will need to install JDBC driver JARs first.
Cheers
Sorry, I missed the link to my example code, see below:
https://github.com/ericlin05/jdbc-examples/blob/master/src/main/java/com/example/ClouderaJDBCHiveExample.java
Hi Eric,
How to capture the return code of the Beeline command that I am executing within a Shell Script. Like Below:
beeline -u $Beeline -e “ALTER TABLE DROP if exists partition (date=”);”
I want to capture the status of above query with in shell script.
Suppose Hive server is down and beeline command fails, then how to capture the status code?
Thanks AG
Hi Atul,
Thanks for visiting my blog and post a question, much appreciated.
To answer your question, it is standard Linux way that the exit status code is stored in $? variable, which you can access and determine if there is any failure or not.
Hope that helps.
Cheers
But if beeline fails to connect to Hive then the return code is 0 since no SQL statements failed!
Hi Phil,
Yes, if you did not attempt to run any queries. See below:
[root@hs2-host.com]# beeline -u ‘jdbc:hive2://hs2-host.com’ -n hive
scan complete in 1ms
Connecting to jdbc:hive2://hs2-host.com
Could not open connection to the HS2 server. Please check the server URI and if the URI is correct, then ask the administrator to check the server status.
Error: Could not open client transport with JDBC Uri: jdbc:hive2://hs2-host.com:-1: Cannot open without port. (state=08S01,code=0)
Beeline version 1.1.0-cdh5.13.3 by Apache Hive
beeline>
[root@hs2-host.com]# echo $?
0
However, if you do attempt to run queries, it will still return the number of failures:
[root@hs2-host.com ~]# beeline -u ‘jdbc:hive2://hs2-host.com’ -n hive -e ‘show tables’ -e ‘show databases’
scan complete in 2ms
Connecting to jdbc:hive2://hs2-host.com
Could not open connection to the HS2 server. Please check the server URI and if the URI is correct, then ask the administrator to check the server status.
Error: Could not open client transport with JDBC Uri: jdbc:hive2://hs2-host.com:-1: Cannot open without port. (state=08S01,code=0)
No current connection
No current connection
[root@hs2-host.com ~]# echo $?
2
Cheers
Eric