Java 想要访问Hive,需要通过beeline的方式连接Hive,hiveserver2提供了一个新的命令行工具beeline,hiveserver2 对 之前的hive做了升级,功能更加强大,它增加了权限控制,要使用beeline需要先启动hiverserver2,再使用beeline连接
基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI
- CLI,即Shell命令行
- JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似
- WebGUI是通过浏览器访问 Hive
本文主要介绍的就是第二种用户接口及Beeline的简单使用。
1.Beeline的使用
1.启动hiveserver2
$ hiveserver2
2.启动beeline连接hive
$ beeline -u jdbc:hive2://192.168.150.1:10000 -n hadoop -p
参数的解释:
-u:连接url,可以使用IP,也可以使用主机名,端口默认为10000
-n:连接的用户名(注:不是登录hive的用户名,是hive所在服务器登录用户名)
-p:密码,可以不用输入
可以使用如下命令来修改端口号(默认端口号是:10000):
hiveserver2 --hiveconf hive.server2.thrift.port=10001
如果不知道beeline怎么使用,可以使用如下命令来查看beeline的使用帮助
$ beeline --help
具体如下:
[hadoop@hadoop ~]$ 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
--showDbInPrompt=[true/false] display the current database name in the prompt
--showNestedErrs=[