hive beeline详解

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连接。用例:beeline -u db_URL

-r

重新连接到最近使用过的URL(如果用户有预先使过的用的,用!connect生成URL,用!save 生成beeline.properties.file)。

用例: beeline -r  

Version: 2.1.0 (HIVE-13670)

-n <username>

连接时使用的用户名。用例: beeline -n valid_user

-p <password>

连接时使用的密码。用例: beeline -p valid_password      可选的密码模式: 从Hive 2.2.0开始参数-p选项是可选的。

用例 : beeline -p [valid_password]

如果密码不是在-p之后提供的,Beeline将在初始化连接时提示输入密码。当密码提供后Beeline会用它来初始化连接而不提示。

-d <driver class>

配置使用的驱动类  用例: beeline -d driver_class

-e <query>

应该执行的查询。查询语句两端用单引号和双引号。这个选项被使用多次。用例: beeline -e "query_string"
支持运行复杂的SQL语句,在一个语句中通过使用分号分隔。(HIVE-9877)
Bug fix (null pointer exception): 0.13.0(HIVE-5765)
Bug fix (--headerInterval not honored): 0.14.0  (HIVE-7647)
Bug fix (running -e in background): 1.3.0 and 2.0.0 (HIVE-6758); workaround available for earlier versions

 

-f <file>

需要被执行的脚本文件。用例: beeline -f filepath
Version: 0.12.0  (HIVE-4268)
注:如果脚本里面包含tabs,版本0.12.0中查询编译失败,这个bug已经在0.13.0版本修复了。(HIVE-6359).
Bug fix (running -f in background): 1.3.0 and 2.0.0 (HIVE-6758);  workaround available for earlier versions 

-i (or) --init <file or files>

初始化需要的初始文件。用例: beeline -i /tmp/initfile
单个文件:Version: 0.14.0 (HIVE-6561)
多个文件:Version: 2.1.0 (HIVE-11336)

-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
Version: 0.13.0  (HIVE-6173)

 --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
(不支持分隔的值输出方式。See HIVE-9770)

 --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。用例: beeline --showNestedErrs=true

 --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]

展示二进制列数据为字符串或者位矩阵。用例: beeline --convertBinaryArrayToString=true

 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 –显示查询追加的明细
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Hive Beeline可以通过以下命令执行SQL文件: ``` beeline -u jdbc:hive2://<host>:<port>/<database> -n <username> -p <password> -f <sql_file> ``` 其中,`<host>`是Hive服务器的主机名或IP地址,`<port>`是Hive服务器的端口号,`<database>`是要连接的数据库名称,`<username>`和`<password>`是连接Hive服务器的用户名和密码,`<sql_file>`是要执行的SQL文件的路径和文件名。 例如,如果要执行名为`test.sql`的SQL文件,可以使用以下命令: ``` beeline -u jdbc:hive2://localhost:10000/default -n hive -p hive -f /path/to/test.sql ``` 这将连接到本地主机上的Hive服务器,使用默认数据库和用户名/密码,然后执行`/path/to/test.sql`文件中的SQL语句。 ### 回答2: Hive是一款基于Hadoop的数据仓库工具,可以帮助我们对大量结构化和半结构化数据进行分析和查询。而Beeline则是Hive的一个命令行工具,它可以连接到Hive Server,执行Hive命令和脚本,同时支持JDBC和ODBC连接,便于对外部的应用程序进行访问。 在Hive中,我们可以使用Beeline执行SQL文件来进行批量的数据处理和分析任务。具体的步骤如下: 1.创建SQL文件:首先,我们需要创建一个.sql文件,把需要执行的Hive命令写在里面。 2.启动Beeline:然后,我们需要启动Beeline,输入命令“beeline -u jdbc:hive2://[IP]:[PORT]/[DATABASE] -n [USERNAME] -p [PASSWORD]”连接到Hive Server。 3.运行SQL文件:在进入Beeline交互模式后,我们可以使用命令“!run [SQL文件路径]”来执行SQL文件。例如:“!run /home/user/sql/myfile.sql”。 4.查看执行结果:在SQL文件执行完毕后,我们可以使用Hive相关的命令来查看执行结果,如“show tables”、“select * from [table_name]”。 需要注意的是,执行SQL文件的过程中可能会出现如下问题: 1.文件编码:如果SQL文件的编码不正确,会导致Beeline无法正确解析文件中的命令。为了避免这个问题,我们应该使用UTF-8编码格式来编写SQL文件。 2.文件路径:如果SQL文件的路径不正确或无权限,Beeline也无法正确读取文件。我们应该确保SQL文件的路径正确,并且当前用户具有读取文件的权限。 3.SQL语法:如果SQL文件中语法出现问题,例如缺少分号或者表名错误等,Beeline会报错并停止执行。我们应该仔细检查SQL文件中的语法是否正确。 总之,使用Beeline执行SQL文件是Hive数据分析的重要组成部分,可以帮助我们更高效地处理大量数据,提高工作效率。 ### 回答3: Hive Beeline是一个交互式工具,用于与Hive服务器交互,可以执行SQL语句,并支持多种连接方式和配置参数。同时,Hive Beeline还具有执行本地脚本的功能,我们可以使用Hive Beeline执行SQL文件。 对于执行SQL文件的步骤,我们可以按照以下的方式进行: 1. 启动Hive Beeline。在终端或命令行窗口中,输入beeline命令,并使用连接参数连接到Hive服务器。 2. 设置Hive服务器的连接参数。根据不同的服务器配置,可以指定不同的参数,比如用户名、密码、连接地址等等,这些参数需要通过命令行参数或者配置文件指定。 3. 进入交互模式。连接成功后,进入Hive Beeline的交互式模式。 4. 加载SQL文件。在交互模式下,使用run命令或者source命令加载SQL文件。这些命令可以指定文件路径或者URL,同时也支持在命令行中指定SQL语句,这样也可以直接执行SQL语句。 5. 执行SQL语句。加载SQL文件后,我们可以使用交互式模式下的命令执行SQL语句,比如select、create、insert等等。执行完成后,我们可以得到查询结果、数据表等等结果。 6. 退出交互模式。执行完成后,我们可以使用exit或者quit等命令退出Hive Beeline的交互模式。 总的来说,Hive Beeline执行SQL文件的步骤比较简单,仅需要加载文件和执行SQL语句,如果出现错误,可以使用命令行参数或者配置文件进行参数设置和调整,以达到最佳的执行效果。同时需要注意的是,在执行过程中需要保证Hive服务器的稳定性和正确性,避免出现数据飞行、重复等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值