Beeline will return non-zero exit code on failure in the following CDH versions 5.2.2 onwards.

The returned exit code simply means the number of errors occurred during beeline’s execution. So for example, the following command will result the exit code of 2 because the first two “show” command will fail and last one will succeed:

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.

5 Comments

  1. Sreeraj Kalappurakkal

    @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.

    1. Eric Lin

      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

  2. Atul

    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

    1. Eric Lin

      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

Leave a Reply

Your email address will not be published. Required fields are marked *