一模蜡、簡(jiǎn)介
學(xué)習(xí)和實(shí)踐Hive中,使用的都是CLI或者h(yuǎn)ive –e的方式慕淡,該方式僅允許使用HiveQL執(zhí)行查詢、更新等操作沸毁,并且該方式比較笨拙單一峰髓。幸好Hive提供了輕客戶端的實(shí)現(xiàn),通過HiveServer或者HiveServer2息尺,客戶端可以在不啟動(dòng)CLI的情況下對(duì)Hive中的數(shù)據(jù)進(jìn)行操作儿普,兩者都允許遠(yuǎn)程客戶端使用多種編程語言如Java、Python向Hive提交請(qǐng)求掷倔,取回結(jié)果。HiveServer或者HiveServer2都是基于Thrift的个绍,但HiveSever有時(shí)被稱為Thrift server勒葱,而HiveServer2卻不會(huì)。既然已經(jīng)存在HiveServer為什么還需要HiveServer2呢巴柿?這是因?yàn)镠iveServer不能處理多于一個(gè)客戶端的并發(fā)請(qǐng)求凛虽,這是由于HiveServer使用的Thrift接口所導(dǎo)致的限制,不能通過修改HiveServer的代碼修正广恢。因此在Hive-0.11.0版本中重寫了HiveServer代碼得到了HiveServer2凯旋,進(jìn)而解決了該問題。HiveServer2支持多客戶端的并發(fā)和認(rèn)證钉迷,為開放API客戶端如JDBC至非、ODBC提供了更好的支持。
二糠聪、修改hive-site.xml
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>slave1</value> <!-- 默認(rèn)是localhost荒椭,但我手動(dòng)改成了本機(jī)的地址 -->
</property>
CDH中修改(應(yīng)該都不需要配置CDH5.10默認(rèn)都開啟了)
三、啟動(dòng)
CDH中已經(jīng)啟動(dòng)舰蟆,可忽略此操作
$HIVE_HOME/bin/hive --service hiveserver2
四趣惠、測(cè)試連接
#由于沒有配置密碼,所以默認(rèn)user為default狸棍,密碼為null
[root@slave1 bin]# ./beeline
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Beeline version 1.1.0-cdh5.10.0 by Apache Hive
beeline> !connect jdbc:hive2://slave1:10000 -udefault
scan complete in 1ms
Connecting to jdbc:hive2://slave1:10000
Enter password for jdbc:hive2://slave1:10000:
Connected to: Apache Hive (version 1.1.0-cdh5.10.0)
Driver: Hive JDBC (version 1.1.0-cdh5.10.0)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://slave1:10000>
0: jdbc:hive2://slave1:10000> show databases;
INFO : Compiling command(queryId=hive_20170414150606_500d6a49-96cc-4f52-9994-8a95830fe887): show databases
INFO : Semantic Analysis Completed
INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:database_name, type:string, comment:from deserializer)], properties:null)
INFO : Completed compiling command(queryId=hive_20170414150606_500d6a49-96cc-4f52-9994-8a95830fe887); Time taken: 0.004 seconds
INFO : Executing command(queryId=hive_20170414150606_500d6a49-96cc-4f52-9994-8a95830fe887): show databases
INFO : Starting task [Stage-0:DDL] in serial mode
INFO : Completed executing command(queryId=hive_20170414150606_500d6a49-96cc-4f52-9994-8a95830fe887); Time taken: 0.01 seconds
INFO : OK
+----------------+--+
| database_name |
+----------------+--+
| cards |
| default |
| testsqoop |
| tonghang |
+----------------+--+
5 rows selected (0.1 seconds)
0: jdbc:hive2://slave1:10000>
至此,hiveserver2搭建成功
五、客戶端連接
1味悄、pom.xml文件增加依賴
由于使用CDH版本草戈,所以maven依賴都是依賴的cdh版本
<!-- 連接hive所需依賴 -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0-cdh5.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>2.6.0-mr1-cdh5.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.10.0</version>
</dependency>
2、代碼
package com.chainfin.hive;
>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
>
public class TestConnHive {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
>
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection conn = DriverManager.getConnection(
"jdbc:hive2://slave1:10000/testsqoop", "default", "");
Statement stmt = conn.createStatement();
ResultSet res = null;
String sql = "select * from dydata limit 1";
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
System.out.println(res.getString(2));
System.out.println(res.getString(3));
System.out.println(res.getString(4));
}
conn.close();
}
}
>