通過HiveServer2訪問Hive

Hive系列文章

  1. Hive表的基本操作
  2. Hive中的集合數(shù)據(jù)類型
  3. Hive動(dòng)態(tài)分區(qū)詳解
  4. hive中orc格式表的數(shù)據(jù)導(dǎo)入
  5. Java通過jdbc連接hive
  6. 通過HiveServer2訪問Hive
  7. SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)
  8. hive關(guān)聯(lián)hbase表
  9. Hive udf 使用方法
  10. Hive基于UDF進(jìn)行文本分詞
  11. Hive窗口函數(shù)row number的用法
  12. 數(shù)據(jù)倉(cāng)庫(kù)之拉鏈表

先解釋一下幾個(gè)名詞:

  • metadata :hive元數(shù)據(jù),即hive定義的表名,字段名,類型,分區(qū),用戶這些數(shù)據(jù)。一般存儲(chǔ)關(guān)系型書庫(kù)mysql中,在測(cè)試階段也可以用hive內(nèi)置Derby數(shù)據(jù)庫(kù)。
  • metastore :hivestore服務(wù)端。主要提供將DDL,DML等語句轉(zhuǎn)換為MapReduce,提交到hdfs中。
  • hiveserver2:hive服務(wù)端。提供hive服務(wù)??蛻舳丝梢酝ㄟ^beeline,jdbc(即用java代碼鏈接)等多種方式鏈接到hive。
  • beeline:hive客戶端鏈接到hive的一個(gè)工具??梢岳斫獬蒻ysql的客戶端。如:navite cat 等。

其它語言訪問hive主要是通過hiveserver2服務(wù),HiveServer2(HS2)是一種能使客戶端執(zhí)行Hive查詢的服務(wù)。HiveServer2可以支持對(duì) HiveServer2 的嵌入式和遠(yuǎn)程訪問,支持多客戶端并發(fā)和身份認(rèn)證。旨在為開放API客戶端(如JDBC和ODBC)提供更好的支持。

會(huì)啟動(dòng)一個(gè)hive服務(wù)端默認(rèn)端口為:10000,可以通過beeline,jdbc,odbc的方式鏈接到hive。hiveserver2啟動(dòng)的時(shí)候會(huì)先檢查有沒有配置hive.metastore.uris,如果沒有會(huì)先啟動(dòng)一個(gè)metastore服務(wù),然后在啟動(dòng)hiveserver2。如果有配置hive.metastore.uris。會(huì)連接到遠(yuǎn)程的metastore服務(wù)。這種方式是最常用的。部署在圖如下:

Python連接Hive

Python3訪問hive需要安裝的依賴有:

  • pip3 install thrift
  • pip3 install PyHive
  • pip3 install sasl
  • pip3 install thrift_sasl

這里有一個(gè)Python訪問Hive的工具類:

# -*- coding:utf-8 -*-

from pyhive import hive

class HiveClient(object):
    """docstring for HiveClient"""
    def __init__(self, host='hadoop-master',port=10000,username='hadoop',password='hadoop',database='hadoop',auth='LDAP'):
        """ 
        create connection to hive server2 
        """  
        self.conn = hive.Connection(host=host,  
            port=port,  
            username=username,  
            password=password,  
            database=database,
            auth=auth) 

    def query(self, sql):
        """ 
        query 
        """ 
        with self.conn.cursor() as cursor: 
            cursor.execute(sql)
            return cursor.fetchall()

    def insert(self, sql):
        """
        insert action
        """
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            # self.conn.commit()
            # self.conn.rollback()

    def close(self):
        """ 
        close connection 
        """  
        self.conn.close()
SQL

使用的時(shí)候,只需要導(dǎo)入,然后創(chuàng)建一個(gè)對(duì)象實(shí)例即可,傳入sql調(diào)用query方法完成查詢。

# 拿一個(gè)連接
hclient = hive.HiveClient()

# 執(zhí)行查詢操作
...

# 關(guān)閉連接
hclient.close()
Python

注意:在insert插入方法中,我將self.conn.commit()self.conn.rollback()即回滾注釋了,這是傳統(tǒng)關(guān)系型數(shù)據(jù)庫(kù)才有的事務(wù)操作,Hive中是不支持的。

Java連接Hive

Java作為大數(shù)據(jù)的基礎(chǔ)語言,連接hive自然是支持的很好的,這里介紹通過jdbc和mybatis兩種方法連接hive。

1. Jdbc連接

java通過jdbc連接hiveserver,跟傳統(tǒng)的jdbc連接mysql方法一樣。

需要hive-jdbc依賴:

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.2.1</version>
</dependency>
XML

代碼跟連接mysql套路一樣,都是使用的DriverManager.getConnection(url, username, password)

@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class HiveConfigModel {

    private String url = "jdbc:hive2://localhost:10000";
    private String username = "hadoop";
    private String password = "hadoop";

}

@Test
public void test(){
    // 初始化配置
    HiveConfigModel hiveConfigModel = ConfigureContext.getInstance("hive-config.properties")
            .addClass(HiveConfigModel.class)
            .getModelProperties(HiveConfigModel.class);

    try {
        Connection conn = DriverManager.getConnection(hiveConfigModel.getUrl(),
                hiveConfigModel.getUsername(), hiveConfigModel.getPassword());

        String sql = "show tables";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();
        List<String> tables = new ArrayList<>();
        while (rs.next()){
            tables.add(rs.getString(1));
        }

        System.out.println(tables);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Java

hive-jdbc-1.2.1.jarMETA-INF下有個(gè)services目錄,里面有個(gè)java.sql.Driver文件,內(nèi)容是:

org.apache.hive.jdbc.HiveDriver

java.sql.DriverManager使用spi實(shí)現(xiàn)了服務(wù)接口與服務(wù)實(shí)現(xiàn)分離以達(dá)到解耦,在這里jdbc的實(shí)現(xiàn)org.apache.hive.jdbc.HiveDriver根據(jù)java.sql.Driver提供的統(tǒng)一規(guī)范實(shí)現(xiàn)邏輯。客戶端使用jdbc時(shí)不需要去改變代碼,直接引入不同的spi接口服務(wù)即可。

DriverManager.getConnection(url, username, password)
Java

這樣即可拿到連接,前提是具體實(shí)現(xiàn)需要遵循相應(yīng)的spi規(guī)范。

2. 整合mybatis

通常都會(huì)使用mybatis來做dao層訪問數(shù)據(jù)庫(kù),訪問hive也是類似的。

配置文件sqlConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="production">
        <environment id="production">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.apache.hive.jdbc.HiveDriver"/>
                <property name="url" value="jdbc:hive2://master:10000/default"/>
                <property name="username" value="hadoop"/>
                <property name="password" value="hadoop"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/hive/test/test.xml"/>
    </mappers>
</configuration>
XML

mapper代碼省略,實(shí)現(xiàn)代碼:

public classTestMapperImpl implements TestMapper {

    private static SqlSessionFactory sqlSessionFactory = HiveSqlSessionFactory.getInstance().getSqlSessionFactory();

    @Override
    public int getTestCount(String dateTime) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TestMapper testMapper = sqlSession.getMapper(TestMapper.class);

        int count = testMapper.getTestCount(dateTime);

        sqlSession.close();

        return count;
    }
}



作者:柯廣的網(wǎng)絡(luò)日志

微信公眾號(hào):Java大數(shù)據(jù)與數(shù)據(jù)倉(cāng)庫(kù)