beeline 连接hive

HiveServer2是一个能使客户端针对hive执行查询的一种服务,与HiverServer1比较,它能够支持多个客户端的并发请求和授权的;HiveCLI 和 hive –e的方式比较单一,HS2允许远程客户端使用多种语言诸如Java,Python等向Hive提交请求,然后取回结果.

$HIVE_HOME/bin目录下面的hiveserver2

由于配置了环境变量因此可以直接使用

nohup hiveserver2 1>/hiveserver2log/log.txt 2>/hiveserver2log/log.err &

命令中的 1 和 2 的意义分别是:

1:表示标准日志输出

2:表示错误日志输出 如果我没有配置日志的输出路径,日志会生成在当前工作目录,默认的日志名称叫做: nohup.xxx

作为数据仓库的工具,hive提供了两种ETL运行方式,分别是通过Hive 命令行和beeline客户端;

命令行方式即通过hive进入命令模式后通过执行不同的HQL命令得到对应的结果;相当于胖客户端模式,即客户机中需要安装JRE环境和Hive程序。

beeline客户端方式相当于瘦客户端模式,采用JDBC方式借助于Hive Thrift服务访问Hive数据仓库。

$HIVE_HOME/bin目录下面的beeline

由于配置了环境变量因此可以直接使用,使用过程中要指定hive的链接信息,用户名,密码

测试环境无密码

[root@host ~]# beeline
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/root/hive/apache-hive-2.1.1/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/spark/spark-2.2.0-bin-hadoop2.7/jars/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/hadoop/hadoop-2.7.4/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Beeline version 2.1.1 by Apache Hive
beeline>  !connect jdbc:hive2://192.168.53.122:10000/default
Connecting to jdbc:hive2://192.168.53.122:10000/default
Enter username for jdbc:hive2://192.168.53.122:10000/default: root
Enter password for jdbc:hive2://192.168.53.122:10000/default:
Connected to: Apache Hive (version 2.1.1)
Driver: Hive JDBC (version 2.1.1)
18/09/06 16:08:26 [main]: WARN jdbc.HiveConnection: Request to set autoCommit to false; Hive does not support autoCommit=false.
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://192.168.53.122:10000/default> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
| gamedw         |
| hive_hbase     |
| sqoopdb        |
| testdb         |
| userdb         |
+----------------+--+
6 rows selected (0.335 seconds)
0: jdbc:hive2://192.168.53.122:10000/default>

也可以用下面方式:

[root@host ~]# beeline -u jdbc:hive2://192.168.53.122:10000/default -n root
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/root/hive/apache-hive-2.1.1/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/spark/spark-2.2.0-bin-hadoop2.7/jars/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/hadoop/hadoop-2.7.4/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Connecting to jdbc:hive2://192.168.53.122:10000/default
Connected to: Apache Hive (version 2.1.1)
Driver: Hive JDBC (version 2.1.1)
18/09/06 16:16:38 [main]: WARN jdbc.HiveConnection: Request to set autoCommit to false; Hive does not support autoCommit=false.
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 2.1.1 by Apache Hive
0: jdbc:hive2://192.168.53.122:10000/default> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
| gamedw         |
| hive_hbase     |
| sqoopdb        |
| testdb         |
| userdb         |
+----------------+--+
6 rows selected (0.178 seconds)
0: jdbc:hive2://192.168.53.122:10000/default>

可以直接通过-u -n-p -e等参数让其执行我们的操作,参数选项:

-u<database URL> 要连接的数据库的URL

-n<username> 指定要连接数据库的用户名

-p<password> 指定要连接数据库的密码

-d<driver class> 使用的驱动名字

-i<init file> 初始化的脚本文件

-e<query> 要执行的查询

-f<exec file> 要执行的脚本文件

-w/--password-file<password file> 从文件读取密码

--hiveconf<key=value> 指定hive配置文件

--hivevarname=value 设置hive变量

--showHeader=[true/false]控制是否在查询结果显示列名

--autoCommit=[true/false]是否自动提交事务

--force=[true/false]运行脚本出错是否继续

--outputformat=[table/vertical/csv2/tsv2/dsv]  指定结果展示的格式

 

原文地址:https://www.cnblogs.com/playforever/p/9599141.html