Hive Beeline使用

HiveServer2提供了一个新的命令行工具Beeline,它是基于SQLLine CLI的JDBC客户端。关于SQLLine的的知识,可以参考这个网站:http://sqlline.sourceforge.net/#manual

Beeline工作模式有两种,即本地嵌入模式和远程模式。嵌入模式情况下,它返回一个嵌入式的Hive(类似于Hive CLI)。而远程模式则是通过Thrift协议与某个单独的HiveServer2进程进行连接通信。

下面给一个简单的登录Beeline的使用实例:

% bin/beeline
Hive version 0.11.0-SNAPSHOT by Apache
beeline> !connect jdbc:hive2://localhost:10000/default 
!connect jdbc:hive2://localhost:10000/default 
Connecting to jdbc:hive2://localhost:10000/default
Connected to: Hive (version 0.10.0)
Driver: Hive (version 0.10.0-SNAPSHOT)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://localhost:10000> show tables;
show tables;
+-------------------+
|     tab_name      |
+-------------------+
| primitives        |
| src               |
| src1              |
| src_json          |
| src_sequencefile  |
| src_thrift        |
| srcbucket         |
| srcbucket2        |
| srcpart           |
+-------------------+
9 rows selected (1.079 seconds)

我们项目在使用的过程中,因为整合了Hamza的权限控制, 使用的时候需要传入用户名和密码,(常用的方式有单次查询-e,也可以把命令写到一个文件中-f),远程连接到HiveServer2:

beeline -u "jdbc:hive2://hadoop1:10000/hamza_group_1" --hivevar hamza.usr="hamza_group" --hivevar hamza.passwd="hMyLXJ6uddQSy1WU" --color=true;

退出beeline命令行则是!quit, 很多命令都是前面需要加一个感叹号, 但对于登录了后的DDL,DML,则直接运行SQL语句即可,语句后带上一个分号,然后回车执行。


The Beeline CLI 支持以下命令行参数:
Option
Description
--autoCommit=[true/false]	
Enable/disable automatic transaction commit. Default is false.
Usage: beeline --autoCommit=true
--autosave=[true/false]	
Automatically save preferences (true) or do not autosave (false). Default is false.
Usage: beeline --autosave=true
--color=[true/false]	
Control whether color is used for display. Default is false.
Usage: beeline --color=true
(Not supported for Separated-Value Output formats. See HIVE-9770)
--delimiterForDSV= DELIMITER	
The delimiter for delimiter-separated values output format. Default is '|' character.
Version: 0.14.0 (HIVE-7390)
--fastConnect=[true/false]	
When connecting, skip building a list of all tables and columns for tab-completion of 
HiveQL statements (true) or build the list (false). Default is true.
Usage: beeline --fastConnect=false
--force=[true/false]	
Continue running script even after errors (true) or do not continue (false). Default is false.
Usage: beeline--force=true
--headerInterval=ROWS	
The interval for redisplaying column headers, in number of rows, when outputformat is table. 
Default is 100.
Usage: beeline --headerInterval=50
(Not supported for Separated-Value Output formats. See HIVE-9770)
--help
Display a usage message.
Usage: beeline --help
--hiveconf property=value	
Use value for the given configuration property. Properties that are listed in hive.conf.restricted.list cannot be reset with hiveconf (see Restricted List and Whitelist).
Usage: beeline --hiveconf prop1=value1
Version: 0.13.0 (HIVE-6173)
--hivevar name=value	
Hive variable name and value. This is a Hive-specific setting in which variables can be set 
at the session level and referenced in Hive commands or queries.
Usage: beeline --hivevar var1=value1
--incremental=[true/false]
Print output incrementally.
--isolation=LEVEL	
Set the transaction isolation level to TRANSACTION_READ_COMMITTED 
or TRANSACTION_SERIALIZABLE. 
See the "Field Detail" section in the Java Connection documentation.
Usage: beeline --isolation=TRANSACTION_SERIALIZABLE
--maxColumnWidth=MAXCOLWIDTH	
The maximum column width, in characters, when outputformat is table. Default is 15.
Usage: beeline --maxColumnWidth=25
--maxWidth=MAXWIDTH	
The maximum width to display before truncating data, in characters, when outputformat is table. 
Default is to query the terminal for current width, then fall back to 80.
Usage: beeline --maxWidth=150
--nullemptystring=[true/false]	
Use historic behavior of printing null as empty string (true) or use current behavior of printing 
null as NULL (false). Default is false.
Usage: beeline --nullemptystring=false
Version: 0.13.0 (HIVE-4485)
--numberFormat=[pattern]	
Format numbers using a DecimalFormat pattern.
Usage: beeline --numberFormat="#,###,##0.00"
--outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2]	
Format mode for result display. Default is table. See Separated-Value Output Formats below for description of recommended sv options.
Usage: beeline --outputformat=tsv
Version: dsv/csv2/tsv2 added in 0.14.0 (HIVE-8615)
--showHeader=[true/false]	
Show column names in query results (true) or not (false). Default is true.
Usage: beeline --showHeader=false
--showNestedErrs=[true/false]	
Display nested errors. Default is false.
Usage: beeline --showNestedErrs=true
--showWarnings=[true/false]	
Display warnings that are reported on the connection after issuing any HiveQL commands. 
Default is false.
Usage: beeline --showWarnings=true
--silent=[true/false]	
Reduce the amount of informational messages displayed (true) or not (false). It also stops displaying the log messages for the query from HiveServer2 (Hive 0.14 and later). Default is false.
Usage: beeline --silent=true
--truncateTable=[true/false]	
If true, truncates table column in the console when it exceeds console length.
Version: 0.14.0 (HIVE-6928)
--verbose=[true/false]	
Show verbose error messages and debug information (true) or do not show (false). 
Default is false.
Usage: beeline --verbose=true
-d <driver class>
The driver class to use.
Usage: beeline -d driver_class
-e <query>
Query that should be executed. Double or single quotes enclose the query string. This option can be specified multiple times.
Usage: beeline -e "query_string"
Icon
Only a single command per -e option is supported. You can't provide multiple semicolon separated commands. Use the -e option multiple times if you want to achieve this.
Bug fix (null pointer exception): 0.13.0 (HIVE-5765)
Bug to be fixed (running -e in background): workaround available (HIVE-6758)
Bug fix (--headerInterval not honored): 0.14.0 (HIVE-7647)
-f <file>	
Script file that should be executed.
Usage: beeline -f filepath
Version: 0.12.0 (HIVE-4268)
Note: If the script contains tabs, query compilation fails in version 0.12.0. This bug is fixed in version 0.13.0 (HIVE-6359).
Bug to be fixed (running -f in background): workaround available (HIVE-6758)
-n <username>
The username to connect as.
Usage: beeline -n valid_user
-p <password>
The password to connect as.
Usage: beeline -p valid_password
-u <database URL>
The JDBC URL to connect to.
Usage: beeline -u db_URL 





  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答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服务器的稳定性和正确性,避免出现数据飞行、重复等问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值