使用 hive -H
或者 hive --help
命令能够查看全部命令的帮助,显示以下:java
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 -- 执行指定的 SQL -f <filename> SQL from files --执行 SQL 脚本 -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
命令,不加任何参数,便可进入交互式命令行。git
在不进入交互式命令行的状况下,可使用 hive -e
执行 SQL 命令。github
hive -e 'select * from emp';
用于执行的 sql 脚本能够在本地文件系统,也能够在 HDFS 上。sql
# 本地文件系统 hive -f /usr/file/simple.sql; # HDFS文件系统 hive -f hdfs://hadoop001:8020/tmp/simple.sql;
其中 simple.sql
内容以下:shell
select * from emp;
可使用 --hiveconf
设置 Hive 运行时的变量。数据库
hive -e 'select * from emp' \ --hiveconf hive.exec.scratchdir=/tmp/hive_scratch \ --hiveconf mapred.reduce.tasks=4;
hive.exec.scratchdir:指定 HDFS 上目录位置,用于存储不一样 map/reduce 阶段的执行计划和这些阶段的中间输出结果。apache
使用 -i
能够在进入交互模式以前运行初始化脚本,至关于指定配置文件启动。编程
hive -i /usr/file/hive-init.conf;
其中 hive-init.conf
的内容以下:服务器
set hive.exec.mode.local.auto = true;
hive.exec.mode.local.auto 默认值为 false,这里设置为 true ,表明开启本地模式。session
--define <key=value>
和 --hivevar <key=value>
在功能上是等价的,都是用来实现自定义变量,这里给出一个示例:
定义变量:
hive --define n=ename --hiveconf --hivevar j=job;
在查询中引用自定义变量:
# 如下两条语句等价 hive > select ${n} from emp; hive > select ${hivevar:n} from emp; # 如下两条语句等价 hive > select ${j} from emp; hive > select ${hivevar:j} from emp;
结果以下:
Hive 内置了 HiveServer 和 HiveServer2 服务,二者都容许客户端使用多种编程语言进行链接,可是 HiveServer 不能处理多个客户端的并发请求,因此产生了 HiveServer2。
HiveServer2(HS2)容许远程客户端可使用各类编程语言向 Hive 提交请求并检索结果,支持多客户端并发访问和身份验证。HS2 是由多个服务组成的单个进程,其包括基于 Thrift 的 Hive 服务(TCP 或 HTTP)和用于 Web UI 的 Jetty Web 服务器。
HiveServer2 拥有本身的 CLI(Beeline),Beeline 是一个基于 SQLLine 的 JDBC 客户端。因为 HiveServer2 是 Hive 开发维护的重点 (Hive0.15 后就再也不支持 hiveserver),因此 Hive CLI 已经不推荐使用了,官方更加推荐使用 Beeline。
Beeline 拥有更多可以使用参数,可使用 beeline --help
查看,完整参数以下:
Usage: java org.apache.hive.cli.beeline.BeeLine -u <database url> the JDBC URL to connect to -r reconnect to last saved connect url (in conjunction with !save) -n <username> the username to connect as -p <password> the password to connect as -d <driver class> the driver class to use -i <init file> script file for initialization -e <query> query that should be executed -f <exec file> script file that should be executed -w (or) --password-file <password file> the password file to read password from --hiveconf property=value Use value for given property --hivevar name=value hive variable name and value This is Hive specific settings in which variables can be set at session level and referenced in Hive commands or queries. --property-file=<property-file> the file to read connection properties (url, driver, user, password) from --color=[true/false] control whether color is used for display --showHeader=[true/false] show column names in query results --headerInterval=ROWS; the interval between which heades are displayed --fastConnect=[true/false] skip building table/column list for tab-completion --autoCommit=[true/false] enable/disable automatic transaction commit --verbose=[true/false] show verbose error messages and debug info --showWarnings=[true/false] display connection warnings --showNestedErrs=[true/false] display nested errors --numberFormat=[pattern] format numbers using DecimalFormat pattern --force=[true/false] continue running script even after errors --maxWidth=MAXWIDTH the maximum width of the terminal --maxColumnWidth=MAXCOLWIDTH the maximum width to use when displaying columns --silent=[true/false] be more silent --autosave=[true/false] automatically save preferences --outputformat=[table/vertical/csv2/tsv2/dsv/csv/tsv] format mode for result display --incrementalBufferRows=NUMROWS the number of rows to buffer when printing rows on stdout, defaults to 1000; only applicable if --incremental=true and --outputformat=table --truncateTable=[true/false] truncate table column when it exceeds length --delimiterForDSV=DELIMITER specify the delimiter for delimiter-separated values output format (default: |) --isolation=LEVEL set the transaction isolation level --nullemptystring=[true/false] set to true to get historic behavior of printing null as empty string --maxHistoryRows=MAXHISTORYROWS The maximum number of rows to store beeline history. --convertBinaryArrayToString=[true/false] display binary column data as string or as byte array --help display this message
在 Hive CLI 中支持的参数,Beeline 都支持,经常使用的参数以下。更多参数说明能够参见官方文档 Beeline Command Options
参数 | 说明 |
---|---|
-u <database URL> | 数据库地址 |
-n <username> | 用户名 |
-p <password> | 密码 |
-d <driver class> | 驱动 (可选) |
-e <query> | 执行 SQL 命令 |
-f <file> | 执行 SQL 脚本 |
-i (or)--init <file or files> | 在进入交互模式以前运行初始化脚本 |
--property-file <file> | 指定配置文件 |
--hiveconf property=value | 指定配置属性 |
--hivevar name=value | 用户自定义属性,在会话级别有效 |
示例: 使用用户名和密码链接 Hive
$ beeline -u jdbc:hive2://localhost:10000 -n username -p password
能够经过三种方式对 Hive 的相关属性进行配置,分别介绍以下:
方式一为使用配置文件,使用配置文件指定的配置是永久有效的。Hive 有如下三个可选的配置文件:
hive-site.xml :Hive 的主要配置文件;
hiveserver2-site.xml:关于 HiveServer2 的配置。
示例以下,在 hive-site.xml 配置 hive.exec.scratchdir
:
<property> <name>hive.exec.scratchdir</name> <value>/tmp/mydir</value> <description>Scratch space for Hive jobs</description> </property>
方式二为在启动命令行 (Hive CLI / Beeline) 的时候使用 --hiveconf
指定配置,这种方式指定的配置做用于整个 Session。
hive --hiveconf hive.exec.scratchdir=/tmp/mydir
方式三为在交互式环境下 (Hive CLI / Beeline),使用 set 命令指定。这种设置的做用范围也是 Session 级别的,配置对于执行该命令后的全部命令生效。set 兼具设置参数和查看参数的功能。以下:
0: jdbc:hive2://hadoop001:10000> set hive.exec.scratchdir=/tmp/mydir; No rows affected (0.025 seconds) 0: jdbc:hive2://hadoop001:10000> set hive.exec.scratchdir; +----------------------------------+--+ | set | +----------------------------------+--+ | hive.exec.scratchdir=/tmp/mydir | +----------------------------------+--+
配置的优先顺序以下 (由低到高):
hive-site.xml
- >hivemetastore-site.xml
- > hiveserver2-site.xml
- >-- hiveconf
- > set
Hive 可选的配置参数很是多,在用到时查阅官方文档便可AdminManual Configuration
更多大数据系列文章能够参见 GitHub 开源项目: 大数据入门指南