“$”后对应的是Linux的Shell命令,“hive>”后对应的是CLI里的命令
CLI选项
–help查看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)
变量和属性
命名空间 | 使用权限 | 描述 |
---|---|---|
hivevar | 可读/可写 | 用户自定义变量 |
hiveconf | 可读/可写 | Hive相关的配置属性 |
system | 可读/可写 | Java定义的配置属性 |
env | 只可读 | Shell环境定义的环境变量 |
$ 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
由上可得:–hivevar标识和–define标识是相同的。
在CLI里可以直接引用我们刚刚定义的变量:
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可以用来配置Hive行为的所有属性:
$ 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)>
Hive中“一次使用”命令
所谓一次使用,其实就是执行一个或多个查询之后,立即退出CLI。
-e:执行结束即退出
$ hive -e "select * from x limit 1";
OK
name1 100
Time taken: 3.955 seconds
-S:静默模式,即不会打印“OK”和“Time taken”等行,>重定向,和Linux的一样
$ hive -S -e "select * from x limit 1" > tmp/test
$cat /tmp/test
name1 100
从文件中执行Hive查询
-f可以执行指定文件中的一个或多个查询语句,查询文件一般后缀都为.q或.hql
$ hive -f /root/test.hql
在CLI里可以用SOURCE来做同样的事情:
hive> source /root/test.hql
在CLI里执行shell命令
在命令前加上感叹号!并且以分号;结尾就可以了:
hive> ! /bin/echo "leon";
"leon"
hive> !pwd;
/
hive> !whoami;
root
在Hive内使用Hadoop的dfs命令
只需要将Hadoop命令中的关键字hadoop去掉,然后以分号结尾就可以了:
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
在Hive脚本里进行注释
–开头表示注释:
-- This is a test script
select * from x;
附我在开源中国的原文:
https://my.oschina.net/lonelycode/blog/834170