How to run command using Hadoop Hive Client

To get the available Hive client command line options use the -H option along with the hive command:

[root@sandbox tmp]# hive -H
WARNING: Use "yarn jar" to launch YARN applications.
usage: hive
 -d,--define <key=value          Variable subsitution to apply to hive

                                  commands. e.g. -d A=B or --define A=B

    --database <databasename     Specify the database to use

 -e <quoted-query-string         SQL from command line
 -f <filename                    SQL from files
 -H,--help                        Print help information

    --hiveconf <property=value   Use value for given property

    --hivevar <key=value         Variable subsitution to apply to hive

                                  commands. e.g. --hivevar A=B

 -i <filename                    Initialization SQL file
 -S,--silent                      Silent mode in interactive shell

 -v,--verbose                     Verbose mode (echo executed SQL to the

                                  console)
Run a metadata command from the command line and exit:    For this we will use the -e option followed by the meta command to be executed inside commas.

  • list the available databases.
[root@sandbox tmp]# hive -e "show databases;"
WARNING: Use "yarn jar" to launch YARN applications.

Logging initialized using configuration in file:/etc/hive/2.4.0.0-169/0/hive-log4j.properties
OK
default
testdb
xademo
Time taken: 2.756 seconds, Fetched: 3 row(s)
Run a select statement from the command line and exit: For this we will use the -e option followed by the select command to be executed inside commas.
[root@sandbox tmp]# hive -e "select * from testdb.tblone;"
WARNING: Use "yarn jar" to launch YARN applications.

Logging initialized using configuration in file:/etc/hive/2.4.0.0-169/0/hive-log4j.properties
OK
1
Time taken: 11.128 seconds, Fetched: 1 row(s)
[root@sandbox tmp]#
Run a select statement from the command line in silent mode and exit: For this we will use the -S option followed by the select command to be executed inside commas.
  • silent mode will suppers the header and footer of the result set output.
[root@sandbox tmp]# hive -S -e "select * from testdb.tblone;"
WARNING: Use "yarn jar" to launch YARN applications.

1
Pipe a sql command to hive client
[root@sandbox tmp]#  echo "select * from testdb.tblone;" | hive
WARNING: Use "yarn jar" to launch YARN applications.

Logging initialized using configuration in file:/etc/hive/2.4.0.0-169/0/hive-log4j.properties
hive select * from testdb.tblone;
OK
1
Time taken: 8.108 seconds, Fetched: 1 row(s)
Execute a sql command from a file in Hive client  For this we will use the -f option followed by the file location that contains the sql.
[root@sandbox tmp]# cat scr.sql
select * from testdb.tblone;


[root@sandbox tmp]# hive -f scr.sql
WARNING: Use "yarn jar" to launch YARN applications.

Logging initialized using configuration in file:/etc/hive/2.4.0.0-169/0/hive-log4j.properties
OK
1
Time taken: 8.97 seconds, Fetched: 1 row(s)