Hive学习——命令行

2024-09-10 16:17:06 浏览数 (1)

1 通过hive --help --service cli查看命令行

代码语言:javascript复制
# hive --help --service cli
usage: hive
-d,--define <key=value> Variable substitution 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 substitution 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)

2 set 命令

1)显示某个变量值

代码语言:javascript复制
hive> set env:HOME;
env:HOME=/root

2)显示所有变量

代码语言:javascript复制
hive> set

3)设置变量

代码语言:javascript复制
#hive --define name=jerry
hive> set name;
name=jerry
hive> set hivevar:name;
hivevar:name=jerry
hive> set hivevar:name=JerryGu;
hive> set name;
name=JerryGu
hive> set hivevar:name;
hivevar:name=JerryGu

注意:

hivevar 是可选的

代码语言:javascript复制
--define <key=value>

等价于

代码语言:javascript复制
--hivevar <key=value>

4)使用变量

代码语言:javascript复制
hive> create table myuser(id
int,${hivevar:name} string);
hive> desc myuser;
id  int                                      
jerrygu   string

用到${hivevar:name}

代码语言:javascript复制
hive> create table myuser1(id
int,${name} string);
hive> desc myuser1;
id  int                                      
jerrygu   string

用到${name}

代码语言:javascript复制
hive> drop table myuser;
hive> drop table myuser1;

5)系统变量

代码语言:javascript复制
hive> set hive.cli.print.current.db;
hive.cli.print.current.db=false
hive> set
hiveconf:hive.cli.print.current.db=true;
hive> set hive.cli.print.current.db;
hive.cli.print.current.db=true
hive> set
hiveconf:hive.cli.print.current.db=flase;

3 system变量与env变量

代码语言:javascript复制
hive> set system:user.name;
system:user.name=root
hive>set system:user.name=jerry;
hive> set system:user.name;
system:user.name=root
hive> set env:HOME;
env:HOME=/root
hive> set env:HOME=/home/jerry;
env:* variables can not be set.

注意:system变量可以修改,env变量只读。

4 一次性使用命令 -e -S

在Linux Shell 下可以一次性使用hive命令(假设demo.person表已经存在,后面介绍)

代码语言:javascript复制
# hive -S -e "use demo;select * from
person" >/home/jerry/myquery;
# cat /home/jerry/myquery
1 elite0      10   ["basketball","music","dance"]    {"adderss":"xx"}
2 elite1      20   ["basketball","music","dance"]    {"adderss":"xx"}
3 elite2      10   ["basketball","music","dance"]    {"adderss":"xx"}
4 elite3      20   ["basketball","music","dance"]    {"adderss":"xx"}
5 elite4      10   ["basketball","music","dance"]    {"adderss":"xx"}
6 elite5      20   ["basketball","music","dance"]    {"adderss":"xx"}

>文件重定向

注意:

  1. 1. SQL语句引号。
  2. 2. 分号结束。

5 从文件中执行hive查询:-f

代码语言:javascript复制
#gedit /home/jerry/hive/query.hql
use demo;
select * from person;
# hive -f /home/jerry/hive/query.hql
OK
Time taken: 3.719 seconds
OK
1 elite0      10   ["basketball","music","dance"]    {"adderss":"xx"}
2 elite1      20   ["basketball","music","dance"]    {"adderss":"xx"}
3 elite2      10   ["basketball","music","dance"]    {"adderss":"xx"}
4 elite3      20   ["basketball","music","dance"]    {"adderss":"xx"}
5 elite4      10   ["basketball","music","dance"]    {"adderss":"xx"}
6 elite5      20   ["basketball","music","dance"]    {"adderss":"xx"}

6 执行前指定先执行某个命令:-i

代码语言:javascript复制
#gedit /home/.hiverc
ADD JAR /home/jerry/jar/my.jar;
set hive.cli.print.current.db=true;
set hive.exec.mode.local.auto=true;
#hive -i /home/.hiverc

7 显示字段名称:set hive.cli.print.header=true;

代码语言:javascript复制
hive> set hive.cli.print.header=true;
hive> select * from person;
erson.id person.name person.age    person.likes   person.address
1 elite0      10   ["basketball","music","dance"]    {"adderss":"xx"}
2 elite1      20   ["basketball","music","dance"]    {"adderss":"xx"}
3 elite2      10   ["basketball","music","dance"]    {"adderss":"xx"}
4 elite3      20   ["basketball","music","dance"]    {"adderss":"xx"}
5 elite4      10   ["basketball","music","dance"]    {"adderss":"xx"}
6 elite5      20   ["basketball","music","dance"]    {"adderss":"xx"}

8 其他

1)自动补全

2)记录历史

3)执行Shell命令(!开始;分号结束)

代码语言:javascript复制
hive> ! ls /home/jerry;
Desktop
Documents
Downloads
hive
Music
Pictures
Public
snap
Templates
Tools
Videos
hive>

4)执行dfs命令

去掉haddop关键字,分号结束。

代码语言:javascript复制
hive> dfs -ls /;
Found 2 items
drwx-wx-wx- root supergroup          0 2024-08-06 12:13 /tmp
drwxr-xr-x- root supergroup          0 2024-08-06 13:50 /user

5)注释: --

0 人点赞