hive beeline详解
来源于 https://www.cnblogs.com/lenmom/p/11218807.html
Hive客户端工具后续将使用Beeline 替代HiveCLI ,并且后续版本也会废弃掉HiveCLI 客户端工具,Beeline是 Hive 0.11版本引入的新命令行客户端工具,它是基于SQLLine CLI的JDBC客户端。
Beeline支持嵌入模式(embedded mode)和远程模式(remote mode)。在嵌入式模式下,运行嵌入式的Hive(类似Hive CLI),而远程模式可以通过Thrift连接到独立的HiveServer2进程上。从Hive 0.14版本开始,Beeline使用HiveServer2工作时,它也会从HiveServer2输出日志信息到STDERR。
1. beeline 的常用参数
Usage: java org.apache.hive.cli.beeline.BeeLine -u <database url> the JDBC URL to connect to -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. --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 Note that csv, and tsv are deprecated - use csv2, tsv2 instead --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 --addlocaldriverjar=DRIVERJARNAME Add driver jar file in the beeline client side --addlocaldrivername=DRIVERNAME Add drvier name needs to be supported in the beeline client side --help display this message Beeline version 2.3.4.spark2 by Apache Hive
参数详解
选项 |
描述 |
-u <database URL> |
用于JDBC URL连接。用例: |
-r |
重新连接到最近使用过的URL(如果用户有预先使过的用的,用!connect生成URL,用 用例: Version: 2.1.0 (HIVE-13670) |
-n <username> |
连接时使用的用户名。用例: |
-p <password> |
连接时使用的密码。用例: beeline -p valid_password 可选的密码模式: 从Hive 2.2.0开始参数-p选项是可选的。 用例 : beeline -p [valid_password] 如果密码不是在-p之后提供的,Beeline将在初始化连接时提示输入密码。当密码提供后Beeline会用它来初始化连接而不提示。 |
-d <driver class> |
配置使用的驱动类 用例: |
-e <query> |
应该执行的查询。查询语句两端用单引号和双引号。这个选项被使用多次。用例: beeline -e "query_string"
|
-f <file> |
需要被执行的脚本文件。用例: beeline -f filepath |
-i (or) --init <file or files> |
初始化需要的初始文件。用例: beeline -i /tmp/initfile |
-w (or) --password-file <password file> | 从文件中读取密码。Version: 1.2.0 (HIVE-7175) |
-a (or) --authType <auth type> | jdbc的认证类型是一个身份认证属性。Version: 0.13.0 (HIVE-5155) |
--property-file <file> |
读取配置属性的文件用例: beeline --property-file /tmp/a Version: 2.2.0 (HIVE-13964) |
--hiveconf property=value | 为给定的配置属性赋值。 在hive.conf.restricted.list列表中的属性不能通过hiveconf的方式重置。 (see Restricted List and Whitelist).
用例: beeline --hiveconf prop1=value1 |
--hivevar name=value | Hive的变量名和变量值。这是一个Hive指定的设置,在这变量能够在会话级别被设置和被Hive命令和查询引用。
用例: beeline --hivevar var1=value1 |
--color=[true/false] | 制颜色是否被用来展示。默认是false 用例: beeline --color=true
(不支持分隔的值输出方式。See HIVE-9770) |
--showHeader=[true/false] | 展示列名是否在查询结果中。默认是true。用例: beeline --showHeader=false |
--headerInterval=ROWS |
当输出为表格时,重新显示列头时他们之间的间隔,用行数计算。默认值为100 用例: beeline --headerInterval=50 |
--fastConnect=[true/false] | 连接时,跳过为HiveQL语法的tab键自动补全功能而构建所有表和列的清单,默认为true不构建该列表。 用例: beeline --fastConnect=false |
--autoCommit=[true/false] | 允许或者禁止自动事务执行。默认是false 用例: beeline --autoCommit=true |
--verbose=[true/false] | 展示冗长的报错信息和调试信息(true)或者不展示(false),默认是false 用例: beeline --verbose=true |
--showWarnings=[true/false] | Default is false.连接时,在执行任意HiveQL命令后展示警告信息。默认是false。 用例: beeline --showWarnings=true |
--showDbInPrompt=[true/false] | 在提示符里面展示当前数据库名字。默认是false。用例: beeline --showDbInPrompt=true
Version: 2.2.0 (HIVE-14123) |
--showNestedErrs=[true/false] |
展示内部错误,默认是false。用例: |
--numberFormat=[pattern] | 用一个小数格式的模板来格式化数字。用例: beeline --numberFormat="#,###,##0.00" |
--force=[true/false] | 出错后继续运行脚本(true),或者不运行(false)。默认是false。用例: beeline--force=true |
--maxWidth=MAXWIDTH | 当输出格式是表格时,在截断数据前展示的最大宽度。默认是查询时的终端的当前宽度,然后回到80。用例: beeline --maxWidth=150 |
--maxColumnWidth=MAXCOLWIDTH | 当输出是表格时,最大列宽,Hive 2.2.0以后默认是50,之前的版本是15。用例: beeline --maxColumnWidth=25 |
--silent=[true/false] | 是(true)否(false)减少展示的信息量。它也会停止展示HiveServer2(Hive 0.14及之后的版本)的查询和命令(Hive 1.2.0及之后的版本)日志信息,默认是false。用例: beeline --silent=true |
--autosave=[true/false] | 自动保存参数选择(true)或者不保存(false)。默认是false。用例: beeline --autosave=true |
--outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2] | 结果展示的模式。默认是表格。查阅下方的Separated-Value Output Formats获取更多信息和推荐选项。用例: beeline --outputformat=tsv
版本号: dsv/csv2/tsv2 added in 0.14.0 (HIVE-8615) |
--truncateTable=[true/false] | 如果是true,那么当表格超出终端显示宽度时,截断表格的列在终端上展示。版本号: 0.14.0 (HIVE-6928) |
--delimiterForDSV= DELIMITER | 用于输出格式中划分值的界定符。默认是‘|’ 版本号: 0.14.0 (HIVE-7390) |
--isolation=LEVEL |
设置事务隔离级别为TRANSACTION_READ_COMMITTED或者TRANSACTION_SERIALIZABLE. 可以查阅Java连接文档中“Field Detail”那一章节。 用例: beeline --isolation=TRANSACTION_SERIALIZABLE |
--nullemptystring=[true/false] | 使用历史的打印空字符null的形式(true)还是使用当前打印空值的方式(false),默认是false。 用例: beeline --nullemptystring=false
Version: 0.13.0 (HIVE-4485) |
--incremental=[true/false] | 从Hive 2.3版本往后默认是true,在它之前是默认为false。当设置为false时,为了最佳的展示列宽,完整的结果集会在展示之前被收集然后缓存起来。当设置为true时,结果集一旦被抓取到就会立即展示, 为了在展示列的填充额外消耗更少的延迟和内存。当你在客户端遭遇一个内存溢出时,推荐设置--incremental=true (因为抓取到的结果集非常大)。 |
--incrementalBufferRows=NUMROWS |
当打印行到标准输出时,保存在缓存中的行数,默认是1000。只有当 --incremental=true 和 --outputformat=table才适用。 用例: beeline --incrementalBufferRows=1000 Version: 2.3.0 (HIVE-14170) |
--maxHistoryRows=NUMROWS | 存储Beeline 历史记录的最大行数。Version: 2.3.0 (HIVE-15166) |
--delimiter=; | 设置Beeline的查询语句分隔符。允许用多个字符的分隔符,但是引号,斜杠和--是不允许的,默认是分号; 用例: beeline --delimiter=$$
Version: 3.0.0 (HIVE-10865) |
--convertBinaryArrayToString=[true/false] |
展示二进制列数据为字符串或者位矩阵。用例: Version: 3.0.0 (HIVE-14786) |
--help | 展示一个帮助信息。用例: beeline --help |
2. beeline 使用示例
2.1 不配置用户名密码
beeline connect有几种方式,见hive-site.xml,缺省为NONE
<property> <name>hive.server2.authentication</name> <value>NONE</value> <description> Expects one of [nosasl, none, ldap, kerberos, pam, custom]. Client authentication types. NONE: no authentication check LDAP: LDAP/AD based authentication KERBEROS: Kerberos/GSSAPI authentication CUSTOM: Custom authentication provider (Use with property hive.server2.custom.authentication.class) PAM: Pluggable authentication module NOSASL: Raw transport </description> </property>
此时连接方式为
lenmom@Mi1701 ~$ beeline Beeline version 1.2.1.spark2 by Apache Hive beeline> !connect jdbc:hive2://localhost:10000/default Connecting to jdbc:hive2://localhost:10000/default Enter username for jdbc:hive2://localhost:10000/default: Enter password for jdbc:hive2://localhost:10000/default: 。。。。 Connected to: Apache Hive (version 2.3.4) Driver: Hive JDBC (version 1.2.1.spark2) Transaction isolation: TRANSACTION_REPEATABLE_READ 0: jdbc:hive2://localhost:10014/default> show databases; +----------------+--+ | database_name | +----------------+--+ | default | | orc | +----------------+--+
2.2 用户名密码登录
hive-site.xml配置
<property> <name>hive.server2.thrift.client.user</name> <value>lenmom</value> <description>Username to use against thrift client</description> </property> <property> <name>hive.server2.thrift.client.password</name> <value>123456</value> <description>Password to use against thrift client</description> </property>
使用密码连接
beeline> !connect jdbc:hive2://localhost:10000/default Connecting to jdbc:hive2://localhost:10000/default Enter username for jdbc:hive2://localhost:10000/default: lenmom Enter password for jdbc:hive2://cdh-server2:10000/default: ***** Connected to: Apache Hive (version 2.3.4) Driver: Hive JDBC (version 2.3.4) Transaction isolation: TRANSACTION_REPEATABLE_READ
注意这里设置的用户要求对inode="/tmp/hive" 有执行权限,否则会出现下列问题:
Connecting to jdbc:hive2://localhost:10000/default Enter username for jdbc:hive2://localhost:10000/default: lenmom Enter password for jdbc:hive2://localhost:10000/default: ** Error: Failed to open new session: java.lang.RuntimeException: org.apache.hadoop.security.AccessControlException: Permission denied: user=lenmom, access=EXECUTE, inode="/tmp/hive":root:supergroup:drwxrwx--- at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:319) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkTraverse(FSPermissionChecker.java:259) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:205) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:190) at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkPermission(FSDirectory.java:1698) at org.apache.hadoop.hdfs.server.namenode.FSDirStatAndListingOp.getFileInfo(FSDirStatAndListingOp.java:108) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getFileInfo(FSNamesystem.java:3817) at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.getFileInfo(NameNodeRpcServer.java:1005) at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.getFileInfo(ClientNamenodeProtocolServerSideTranslatorPB.java:843)
2.3 beeline连接hiveserver2使用用户名密码
beeline -u "jdbc:hive2://localhost:10000" -n lenmom -p 123456 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0 scan complete in 2ms Connecting to jdbc:hive2://localhost:10000 Connected to: Apache Hive (version 2.3.4) Driver: Hive JDBC (version 2.3.4) Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 2.3.4 by Apache Hive 0: jdbc:hive2://localhost:10000>
2.4 类似hive-cli 的执行脚本功能
nohup beeline -u jdbc:hive2://127.0.0.1:10000 -n lenmom -p 123456 --color=true --silent=false \ --hivevar p_date=${partitionDate} --hivevar f_date=${fileLocDate} \ -f hdfs_add_partition_dmp_clearlog.hql >> $logdir/load_${curDate}.log
3. beeline支持的操作
type command !help in beeline terminal
!help
output:
!addlocaldriverjar Add driver jar file in the beeline client side. !addlocaldrivername Add driver name that needs to be supported in the beeline client side. !all Execute the specified SQL against all the current connections !autocommit Set autocommit mode on or off !batch Start or execute a batch of statements !brief Set verbose mode off !call Execute a callable statement !close Close the current connection to the database !closeall Close all current open connections !columns List all the columns for the specified table !commit Commit the current transaction (if autocommit is off) !connect Open a new connection to the database. !dbinfo Give metadata information about the database !describe Describe a table !dropall Drop all tables in the current database !exportedkeys List all the exported keys for the specified table !go Select the current connection !help Print a summary of command usage !history Display the command history !importedkeys List all the imported keys for the specified table !indexes List all the indexes for the specified table !isolation Set the transaction isolation for this connection !list List the current connections !manual Display the BeeLine manual !metadata Obtain metadata information !nativesql Show the native SQL for the specified statement !nullemptystring Set to true to get historic behavior of printing null as empty string. Default is false. !outputformat Set the output format for displaying results (table,vertical,csv2,dsv,tsv2,xmlattrs,xmlelements, and deprecated formats(csv, tsv)) !primarykeys List all the primary keys for the specified table !procedures List all the procedures !properties Connect to the database specified in the properties file(s) !quit Exits the program !reconnect Reconnect to the database !record Record all output to the specified file !rehash Fetch table and column names for command completion !rollback Roll back the current transaction (if autocommit is off) !run Run a script from the specified file !save Save the current variabes and aliases !scan Scan for installed JDBC drivers !script Start saving a script to a file !set Set a beeline variable !sh Execute a shell command !sql Execute a SQL command !tables List all the tables in the database !typeinfo Display the type map for the current connection !verbose Set verbose mode on
4. 常用的几个command
- !connect url –连接不同的Hive2服务器
- !exit –退出shell
- !help –显示全部命令列表
- !verbose –显示查询追加的明细