使用python连接hive


首先需要安装包:

pip install pyhive[hive]

如果安装报错的话执行:

sudo apt-get install libsasl2-dev

再安装下。

然后需要启动hive服务:

hive --service hiveserver2

修改hive验证方式: 在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>

接下来是python脚本:

from pyhive import hive

cursor = hive.connect(host='myhadoop', auth='NOSASL', database='default').cursor()
cursor.execute('select * from users limit 10')
print(cursor.fetchone())
print(cursor.fetchall())
for i in range(7, 10):
    sql = "insert into users VALUEs ({},'username{}')".format(i, str(i))
    cursor.execute(sql)