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.

7 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

        1. Eric Lin

          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

Leave a Reply

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