005 python连接hive

本文介绍了如何在Python环境中配置并连接Hive,包括Hive的安装,解决sasl安装问题,提供Python连接Hive的测试代码,以及针对TTransportException错误的解决办法,最后给出了相关参考资料。
环境安装
  1. hive
    hive 安装包配置
  2. python配置hive环境
    pip install sasl
    pip install thrift
    pip install thrift-sasl
    pip install PyHive
    
    注:安装sasl的时候可能会报错,导致安装不上,这个时候去sasl下载地址下载配置版本的sasl
    # 安装适配版本的whl
    pip install sasl-0.2.1-cp37-cp37m-win_amd64.whl
    
python 连接hive测试
  1. 连接代码
    from pyhive import hive
    
    def main():
        conn = hive.Connection(
            host='192.168.1.18',
            port=10000,
            username='hive',
            database='default',
            auth='NOSASL'
        )
    
        cursor = conn.cursor()
        cursor.execute('show databases')
        res = cursor.fetchall()
        for x in res:
        	print(x)
        conn.close()
        
    if __name__ == "__main__":
        main()
    
问题解决
  1. thrift.transport.TTransport.TTransportException: Could not start SASL: b’Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2’
    conn = hive.Connection(
    	host='192.168.1.18',
    	port=10000,
    	username='hive',
    	database='default',
    	auth='NOSASL'  # 添加该项,则不会报上面的错误
    )
    
  2. 若连接不报错,也不返回,则需要修改hive-site.xml文件-
    # 添加
    <property>
      <name>hive.server2.authentication</name>
      <value>NOSASL</value>
      <description>
        Expects one of [nosasl, none, ldap, kerberos, pam, custom].
        Client authentication types.
          NONE: no authentication check
          LDAP: LDAP/AD based authentication
          KERBEROS: Kerberos/GSSAPI authentication
          CUSTOM: Custom authentication provider
                  (Use with property hive.server2.custom.authentication.class)
          PAM: Pluggable authentication module
          NOSASL:  Raw transport
      </description>
    </property>
    
    重启hiveserver2。
参考

1. pycharm使用pyhive连接hive
2. Python连接Hive(基于PyHive)

Python连接Hive Web通常可以借助一些Python客户端库来实现,以下是几种常见的方法: #### 使用`pyhive`库 `pyhive`是一个用于连接HiveServer2的Python客户端库。以下是使用`pyhive`连接Hive Web的示例代码: ```python from pyhive import hive # 创建hive连接 conn = hive.Connection( host='your_hive_host', # HiveServer2主机的IP地址 port=10000, # HiveServer2服务端口号 username='your_username', # 连接hive数据库的用户名 database='your_database', # 具体数据库名 auth='NOSASL' # 客户端的认证模式 ) cur = conn.cursor() sql = "show tables" cur.execute(sql) results = cur.fetchall() print(results) ``` 在上述代码中,需要将`your_hive_host`、`your_username`、`your_database`替换为实际的Hive服务器地址、用户名和数据库名。`auth`参数可根据实际的认证需求进行调整,如`CUSTOM`、`LDAP`等 [^1][^2]。 #### 使用`impyla`库 `impyla`也是一个可以用于连接HivePython库。示例代码如下: ```python from impala.dbapi import connect # 建立连接 conn = connect( host='your_hive_host', port=21050, # Impala服务端口号 database='your_database', user='your_username', password='your_password', auth_mechanism='PLAIN' ) cur = conn.cursor() cur.execute('SHOW TABLES') tables = cur.fetchall() print(tables) ``` 同样,需要将`your_hive_host`、`your_username`、`your_database`、`your_password`替换为实际的值。`auth_mechanism`参数可根据实际情况修改 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值