本地连接 NOSASL 验证的 Hive 服务

本地连接Hive, 验证方式是 NOSASL,可选Java或Python模式。

Java

查看服务器的Hive版本:

  1. 登录可使用Hive的机器;
  2. 查看Hive的Jar包:ls /usr/lib/hive/lib/
  3. 找到hive-common-1.1.0-cdh5.7.4.jar
  4. 则,当前Hive的版本是1.1.0-cdh5.7.4

  1. 启动Hive,hive
  2. 日志:jar:file:/usr/lib/hive/lib/hive-common-1.1.0-cdh5.7.4.jar!/
  3. 则,当前Hive的版本是1.1.0-cdh5.7.4

本地Maven库的Jar包版本,需要与服务器的版本相同,在pom.xml中,添加依赖hive-exec、hive-jdbc。

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>1.1.0-cdh5.7.4</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0-cdh5.7.4</version>
</dependency>

关于JDBC的数据源:

  1. Hive使用的是HiveServer2,所以JDBC的源是jdbc:hive2
  2. HiveServer2配置于md3服务器;
  3. HiveServer2的端口是10000,也是默认端口;
  4. HiveServer2的验证模式是NOSASL,在筛选器的高级中查看。
  5. HiveServer2的数据库名称是default,也是Hive默认;

因此,JDBC的源是:”jdbc:hive2://md3:10000/default;auth=noSasl” ;同时要注意的是,NOSASL模式需要任意一个用户名(如Hive),不需要密码,不填写用户名会报错。

关于HiveDriver:

本地能访问HiveDriver类,则表明Java支持Hive调用,Maven中的Jar包已经安装成功;

测试代码:

public class HiveTest {
    private static final String DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(DRIVER_NAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        String url = "jdbc:hive2://md3:10000/default;auth=noSasl"; // NOSASL模式需要添加noSasl
        String user = "hive"; // NOSASL模式随意填写

        Connection con = DriverManager.getConnection(url, user, "");
        Statement stmt = con.createStatement();

        String sql = "describe orc_elapsed_log";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        System.out.println("++++++++++++++++++++ Result ++++++++++++++++++++");
        while (res.next()) {
            System.out.println(res.getString(1));
        }
        
        stmt.close();
        con.close();
    }
}

HiveServer2在ClouderaManager中的配置位置:

Hive的状态

《本地连接 NOSASL 验证的 Hive 服务》 States

HiveServer2的信息

《本地连接 NOSASL 验证的 Hive 服务》 HiveServer2

Hive的端口号

《本地连接 NOSASL 验证的 Hive 服务》 Port

Hive的验证方式

《本地连接 NOSASL 验证的 Hive 服务》 Authentication

Python

相对而言,Python版本比较简单,主要是要获取Hive的服务器相关信息,然后调用pyhs2.connect()。

源码参考:
https://github.com/SpikeKing/MachineLearningTutorial/blob/master/tests/hive_test.py

pyhs2.connect:

  1. host:HiveServer2服务器的部署地址,如md3;
  2. port:HiveServer2的端口号,如10000;
  3. authMechanism:验证模式,NOSASL;
  4. database:数据库的名称,如default,也是Hive默认;

代码逻辑,与Java类似。

HADOOP_SUBMIT_NODE = 'hive_server'
HADOOP_HIVE_SERVER_PORT = 10000
# 需要与/etc/hive/conf/hive-site.xml中的配置hive.server2.authentication保持一致
AUTH_MECHANISM = 'NOSASL'
DEFAULT_HIVE_FIELD_SEPARATOR = u'\t'  # 默认字段分隔符


def pre_process_hql(hql):
    """
    预处理,去掉最后的';'
    :param hql:
    :return:
    """
    hql = hql.strip()
    return hql[:-1] if hql[-1] == ';' else hql


def execute_hql(hql, raise_exception=True):
    """
    执行hql
    :param hql:
    :param raise_exception:
    :return: result, error_msg
    """
    # 放内部调是为了测试用例不报import error
    import pyhs2
    from pyhs2.error import Pyhs2Exception

    hql = pre_process_hql(hql)
    print 'HQL:\n  ', hql
    try:
        with pyhs2.connect(host=HADOOP_SUBMIT_NODE,
                           port=HADOOP_HIVE_SERVER_PORT,
                           authMechanism=AUTH_MECHANISM,
                           database='default') as conn:
            with conn.cursor() as cur:
                # Execute query
                cur.execute(hql)
                # Fetch table results
                result = cur.fetchall()
                return result, ''
    except Exception as e:
        if raise_exception:
            raise e
        msg = e.errorMessage if isinstance(e, Pyhs2Exception) else '%s' % e.message
        return None, '%s\nException: %s' % (hql, msg)


print execute_hql("describe orc_elapsed_log")

OK,that’s all! Enjoy it!

    原文作者:SpikeKing
    原文地址: https://www.jianshu.com/p/2f36487a8025
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞