zoukankan      html  css  js  c++  java
  • hive (1)Cli命令

    查看命令选项

    # hive --help
    Usage ./hive <parameters> --service serviceName <service parameters>
    Service List: beeline cleardanglingscratchdir cli hbaseimport hbaseschematool help hiveburninclient hiveserver2 hplsql hwi jar lineage llap llapdump llapstatus metastore metatool orcfiledump rcfilecat schemaTool version 
    Parameters parsed:
      --auxpath : Auxillary jars 
      --config : Hive configuration directory
      --service : Starts specific service/component. cli is default
    Parameters used:
      HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
      HIVE_OPT : Hive options
    For help on a particular service:
      ./hive --service serviceName --help
    Debug help:  ./hive --debug --help

    选项解释:

    --auxpath  该选项允许用户指定一个以冒号分割的Java包(jar)。很少有到。
    --config 指定配置文件目录,覆盖系统默认的配置(${HIVE_HOME}/conf)
    --service 启动服务,cli是默认的服务。
    比如,想要启动一个cli:
    # hive --service cli 或者hive
    # hive --help --service cli 查看一个服务的参数(这里的服务是cli)

    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)

    hive服务

    以上服务名字里面的hiveserver已经被改名成hiveserver2了。使用的时候换成hiveserver2.

    hive中的变量和属性

    使用 --define key=value 和 --hivevar key=value 都可以在命令行定义变量,这两者是等价的。
    例如:
    # hive --define name=yjt
    查看定义的变量:以下两种方式都可以查看变量值。

    hive> set hivevar:name;
    hivevar:name=yjt


    hive> set name;
    name=yjt
    那么上述的hivevar是什么呢?这是hive的命名空间,hive会将这项键值对放到hivevar这个命名空间内,当然还有其他三种内置命名空间。hiveconf、system、env。

    hive命名空间

    Hive变量内部是以Java字符串的方式存储的。在使用变量的时候,Hive会先使用变量值替换掉变量引用,然后才将语句提交给处理器。变量替换跟Linux变量替换是一样的。

    在CLI中,可以使用set命令显示或者修改变量值。

    例如:

    hive> set;   显示着四种内置变量的所有值
    hive> set env:HOME; 显示env内置变量的HOME值
    hive> set -v; 更详细的输出,输出内容包括了Hadoop中定义的一些属性。
    hive> set hive.execute.engine=tez; 或者 set hiveconf:hive.execute.engine=tez; 使用set关键字为变量重新赋值或者定义变量。
    hive> set hiveconf:hive.cli.print.current.db=true; 显示当前数据库。

    注意:在使用或者定义system或者env命名空间的属性值时,需要指定前缀systemc:或者env:

    hive 客户端命令选项

    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)

    解释:
    -e: 在命令行执行sql,跟mysql一样。执行完就会退出客户端。
    例如:
    # hive -e "select * from user_info";

    OK
    1 dennis hu CN
    2 Json Lv Jpn
    3 Mike Lu USA
    1 dennis hu CN
    2 Json Lv Jpn
    3 Mike Lu USA
    Time taken: 2.775 seconds, Fetched: 6 row(s)

    -S:静默模式,在输出的时候可以去掉OK,Time taken等行。
    例如:# hive -S -e "select * from user_info";

    1 dennis hu CN
    2 Json Lv Jpn
    3 Mike Lu USA
    1 dennis hu CN
    2 Json Lv Jpn
    3 Mike Lu USA

    -f:指定sql文件来自于文件而不是命令行。不能与-e同时使用。

    在hive shell也可以使用source 文件名 来执行sql脚本。
    -i:指定一个文件,当CLI启动的时候,在提示符出现之前会执行这个文件,默认的文件名是当前用户家目录下的.hiverc文件。可以与-e同时使用。对于需要
    频繁执行的命令,比如设置属性,执行sql、增加对于Hadoop分布式内存进行自定义的hive扩展Java包等
    一个.hiverc的例子:

    set env:HOME;
    set hiveconf:hive.cli.print.current.db=true;

    注意:在hiverc文件里面,每一行后面不要忘记分号。

    说明:如果想要查看hive的历史记录,在当前用户的家目录下有一个.hivehistroy文件,例如:

    ls ~/.hivehistory   默认只记录100条。

    hive客户端执行shell命令

    想要在hive里面执行shell命令,需要在命令的前面加上!,并且以分号(;)结尾,例如:

    hive (default)> !echo "I'm learning hive.";
    "I'm learning hive."

    注意:Hive CLI中不能使用交互式、管道等命令功能。

    hive客户端使用Hadoop的dfs命令

    使用Hadoop的命令时,只需要把前面的hadoop去掉即可

    hive (default)> dfs -ls /;
    Query returned non-zero code: 1, cause: Permission denied: Principal [name=root, type=USER] does not have following privileges for operation DFS [[ADMIN PRIVILEGE] on Object [type=COMMAND_PARAMS, name=[-ls, /]]]
    恩恩。。。。这个报错,没权限。如果在hive里面开启了权限控制,很有可能会导致这个情况。把权限控制去掉,执行:

    hive (default)> dfs -ls /;  这个命令等价于在shell 终端执行   hadoop dfs -ls /
    Found 10 items
    -rw-r--r-- 2 root supergroup 12 2019-06-18 07:17 /aa
    drwxr-xr-x - root supergroup 0 2019-06-18 07:17 /aa.out
    drwxr----- - root supergroup 0 2019-06-18 08:42 /data1
    drwxr-xr-x - root supergroup 0 2019-06-14 08:50 /hbase
    drwxr-xr-x - root supergroup 0 2019-06-17 05:45 /hive
    -rw-r--r-- 2 root supergroup 39904 2019-06-18 03:07 /test
    drwxr-xr-x - root supergroup 0 2019-06-19 01:51 /test.out
    drwxr-xr-x - root supergroup 0 2019-06-18 01:49 /tez-0.9.0
    drwx-wx-wx - root supergroup 0 2019-06-18 11:10 /tmp
    drwx------ - root supergroup 0 2019-05-06 07:43 /user

    使用 def --help查看支持的选项。

    说明:在hive里面执行dfs命令比在shell命令行速度快,这是因为在shell命令行每次执行都需要开启一个新jvm实例,而在hive里面,会在同一个进程里面执行命令。

    hive使用注释

    方法一:直接在命令行使用 -- 进行注释,hive是不会解析的。

    方法二:在脚本中使用 --,通过 -f执行sql的时候,也不会解析。

    列如:

    hive (default)> --file
    hive (default)> -- 123

     显示字段名称

    在使用sql查询的时候,如果想要看到字段名称,如何设置?

    可以通过设置hiveconf配置选项hive.cli.print.header为true,默认是关闭的

    hive (default)> set hive.cli.print.header;
    hive.cli.print.header=false
    hive (default)> set hive.cli.print.header=true;

    hive (default)> select * from user_info limit 3;
    OK
    user_info.user_id user_info.firstname user_info.lastname user_info.count
    1 dennis hu CN
    2 Json Lv Jpn
    3 Mike Lu USA
    Time taken: 0.114 seconds, Fetched: 3 row(s)

    如果想要每次都查看字段名称,想要永久设置这个值,有两种方法:
    1、在用户家目录的.hiverc文件写入上诉的开启命令。
    2、在hive-site.xml配置
  • 相关阅读:
    微博转发的内容如何实现点击人名跳转到个人主页
    解决json_encode中文UNICODE转码问题
    ***git自动化部署总结
    **Git本地仓库图解
    我 Git 命令列表 (1)【转】
    Git
    git pull使用【转】
    git merge简介【转】
    获得内核函数地址的四种方法
    【笔记】一些linux实用函数技巧【原创】
  • 原文地址:https://www.cnblogs.com/yjt1993/p/11058729.html
Copyright © 2011-2022 走看看