连接hive有2种形式,hive cli 和 beeline
hive提供了以表的形式访问hdfs文件的功能,metastore对外提供表的元数据信息。有了元数据,就可以结构化的表的形式访问hdfs上的文件了
cli模式是通过metaStroe来访问元数据;beeline模式是通过hiveServer2==》metastore访问元数据。
hs2依赖于metastore。
官方建议使用beeline模式连接hive执行操作。
hive-cli和beeline区别 官网说明
1.在使用hive命令的时候,会提示CLI已经过期,建议用beeline
通过hadoop version 找到hive的home,发现有一个hive-old 的文件,通过检查测试发现这就是原来hive cli的文件。hive3.0只支持beeline
2.cli连接不需要hs2,而beeline需要启动hs2服务
将hs2关闭后
hive可以正常连,但beeline连不上
3.CDH的beeline在运行load data local inpath的时候总是提示找不到文件的错误(可以用hdfs上的地址),但在同一节点使用hive却可以。这是因为启动hivecli的时候,是会在本地启动一个metastore的服务,但hs2可以远程的,自然加载不到本地的文件。
可以使用 hive -e ‘load data local inpath’ 和 hive -f
hive cli 之于hive 就相当于sql plus之于oracle. 需要和server安装在一起。
而beeline只需要一个jdbc jar包就可以了,可以不需要与hive server 2装在一台server.
他们认证的方式也不同,hive cli可以直接访问hdfs和hive meta store,感觉更稳定快捷一些。
而beeline 则需要把请求提交到hive server2,通过安全验证,然后在队列中等待。
beeline的并发性和安全性得到了提高。
4.beeline的功能更强大,支持jdbc,并发性和安全性更高,cs架构,更轻。但速度理论上更慢。
hs2提供了代理功能,能够以一个更安全的用户访问hive。
本身是基于jdbc的,并发性能有保障。
beeline功能更多,比如,可以设置输出数据的分隔符、高亮显示、数据转换等功能
一、hive cli
二、hive beeline
1. overview
beeline 可以连接hive和impala,基于SQLLine CLI的JDBC客户端,beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式两种,也即既可以像hive client一样访问本机的hive服务,也可以通过指定ip和端口远程访问某个hive服务。
metaStroe是提供元数据访问服务,hiveServer2是提供jdbc的方式访问hive。
Beeline支持嵌入模式(embedded mode)和远程模式(remote mode)。在嵌入式模式下,运行嵌入式的Hive(类似Hive CLI),而远程模式可以通过Thrift连接到独立的HiveServer2进程上。从Hive 0.14版本开始,Beeline使用HiveServer2工作时,它也会从HiveServer2输出日志信息到STDERR。
hive 官网是推荐使用beeline,它还提供了更为友好的显示方式(类似mysql client)。Beeline是从 Hive 0.11版本引入的,它是基于SQLLine CLI的JDBC客户端。后续将使用Beeline 替代HiveCLI ,并且后续版本也会废弃掉HiveCLI 客户端工具。
2. usage
nohup beeline
-u jdbc:hive2://10.180.0.26:10000;principal=hive/z4.idss.com@TEST.COM
-n hive -p hive
--color=true
--silent=false
--hivevar p_date=${partitionDate} --hivevar f_date=${fileLocDate}
-f hdfs_add_partition_dmp_clearlog.hql
>> $logdir/load_${curDate}.log
&
指定要连接的hiveserver2的主机、端口
beeline -u jdbc:hive2://hd1:10000
如果是本机的hiveserver2,则可省略主机、端口
beeline -u jdbc:hive2://
整合kerberos时设置要principal是hive服务的,登录用户是krb认证的,krb认证谁beeline当前用户就是谁。
如果principal这个单词写错了
beeline -u “jdbc:hive2://n105:10000/;principal=hive/node105@XYYH.COM”
3.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
-w (or) --password-file <password file> the password file to read password from
-d <driver class> the driver class to use
-e <query> query that should be executed
-i <init file> script file for initialization
一般用来和-f配合,用来给-f中的sql文件设置变量,值也是一个文件
-f <exec file> script file that should be executed
可以设置多个 -f sqlFile1 -f sqlFile2
--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
用于在beeline中设置多种颜色,便于查看
--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 1.2.1.spark2 by Apache Hive
进入beeline后的命令
!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
Comments, bug reports, and patches go to ???
三、WebHCat
WebHCat是为HCatalog提供REST API的服务,自hive 0.11.0 版本之后,hive 中也自带了 webhcat (官网介绍说明),如下图,通过WebHCat,程序能够通过REST的API很安全的链接和操作HCatalog提供的服务,方便Hive、Pig、MapReduce等应用使用。(类似于通过WebHDFS以web的方式来操作HDFS)