Variables, Properties and Common Commands of Hive Command Line Interface

Keywords: hive shell SQL Hadoop

After "$" corresponds to the Shell command of Linux, and after "hive >" corresponds to the command in CLI.

CLI options

- help to see the parameters of hive

$hive --help --service cli

usage: hive
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
 -h <hostname>                    connecting to Hive Server on remote host
    --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
 -p <port>                        connecting to Hive Server on port number
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the
                                  console)

Variables and attributes

Namespace Use permission describe
hivevar Readable/Writable User-defined variables
hiveconf Readable/Writable Hive-related configuration properties
system Readable/Writable Java-defined configuration properties
env Readable only Environment variables defined by Shell environment
$ hive --define foo=bar
hive> set foo;
foo=bar;

hive> set hivevar:foo;
hivevar:foo=bar;

hive> set hivevar:foo=bar2;

hive> set foo;
foo=bar2;

hive>set hivevar:foo;
hivevar:foo=bar2;

hive> set system:user.name;
system:user.name=root

hive> set system:user.name=leon;

hive> set system:user.name;
system:user.name=leon

hive> set env:HOME;
env:HOME=/root

From the above, we can see that the hivevar and define identities are the same.

In CLI, you can refer directly to the variables we just defined:

hive> create table toss1(i int, ${hivevar:foo} string);

hive> describe toss1;
i       int
bar2    string

hive> create table toss2(i2 int, ${foo} string);

hive> describe toss2;
i2      int
bar2    string

hive> drop table toss1;

hive> drop table toss2;

hiveconf can be used to configure all attributes of Hive behavior:

$ hive --hiveconf hive.cli.print.current.db=true
hive (default)> set hiveconf:hive.cli.print.current.db;
hiveconf:hive.cli.print.current.db=true

hive (default)> set hiveconf:hive.cli.print.current.db;
hiveconf:hive.cli.print.current.db=true

hive (default)> set hiveconf:hive.cli.print.current.db=false;

hive> set hiveconf:hive.cli.print.current.db=true;
hive (default)>

One Use Command in Hive

The so-called one-time use is actually to exit CLI immediately after executing one or more queries.

- e: Exit at the end of execution

$ hive -e "select * from x limit 1";
OK
name1 100
Time taken: 3.955 seconds

- S: Silent mode, i.e., no lines like "OK" and "Time taken," etc. > redirection, as in Linux

$ hive -S -e "select * from x limit 1" > tmp/test

$cat /tmp/test
name1 100

Execute Hive queries from files

- f can execute one or more query statements in a specified file, the query file is usually suffixed with. q or. hql

$ hive -f /root/test.hql

SOURCE can be used to do the same thing in CLI:

hive> source /root/test.hql

Execute shell commands in CLI

Add an exclamation mark before the command! And with a semicolon; the end is OK:

hive> ! /bin/echo "leon";
"leon"
hive> !pwd;
/
hive> !whoami;
root

Use Hadoop's dfs command in Hive

Just remove the keyword Hadoop from the Hadoop command and end with a semicolon:

hive> dfs -ls /;
Found 26 items
dr-xr-xr-x   - root root      36864 2017-02-08 11:11 /bin
dr-xr-xr-x   - root root          0 2017-02-08 11:41 /proc
dr-xr-xr-x   - root root          0 2017-02-08 11:41 /sys
dr-xr-xr-x   - root root      20480 2017-02-06 16:57 /sbin
drwxr-xr-x   - root root       4096 2016-04-21 15:05 /opt
drwxr-xr-x   - root root      12288 2017-02-08 14:43 /etc
dr-xr-xr-x   - root root       4096 2017-02-06 16:57 /lib
drwx------   - root root      16384 2016-04-21 15:00 /lost+found
drwxr-xr-x   - root root        800 2017-02-08 11:42 /run
drwxr-xr-x   - root root       4096 2015-08-12 22:22 /media
drwxr-xr-x   - root root       4096 2017-02-07 11:31 /user
drwxr-xr-x   - root root       4096 2017-01-16 15:31 /tem
drwxr-xr-x   - root root       4096 2017-01-16 15:31 /data
drwxr-xr-x   - root root       4096 2015-08-12 22:22 /mnt
dr-xr-xr-x   - root root      36864 2017-02-06 16:57 /lib64
-rw-r--r--   1 root root          0 2017-01-17 12:04 /.vimrc
drwxr-xr-x   - root root       4096 2017-02-08 11:41 /var
drwxr-xr-x   - root root       4096 2017-02-07 11:19 /usr
drwxr-xr-x   - root root       4096 2017-02-06 17:06 /home
drwxr-xr-x   - root root       4096 2017-02-02 10:00 /smb
drwxr-xr-x   - root root       4096 2015-08-12 22:22 /srv
drwxrwxrwx   - root root       4096 2017-02-08 16:24 /tmp
dr-xr-x---   - root root       4096 2017-02-08 14:43 /root
-rw-r--r--   1 root root          0 2016-04-21 22:18 /.autorelabel
drwxr-xr-x   - root root       2860 2017-02-08 11:41 /dev
dr-xr-xr-x   - root root       4096 2017-01-16 15:07 /boot

Comment in Hive scripts

- Comments at the beginning:

-- This is a test script
select * from x;

Attached is my original text in Open Source China:
https://my.oschina.net/lonelycode/blog/834170

Posted by Visualant on Wed, 27 Mar 2019 20:54:28 -0700