impyla的安装方式及Python连接hive/impala/pg的方式

python impyla的安装方式

pip install  six bit_array thrift==0.9.3 thrift_sasl==0.2.1 pure_sasl impyla==0.14.0

Python连接hive/impala/pg数据库的方式

python连接hive,impala

# hive 默认端口为10000,impala端口为21050

from impala.dbapi import connect

cdh_params = {
    'host': '10.xxx.xx.xx',
    'port':10000,
    'database': 'xx',
    'timeout': 60,
    'auth_mechanism':'PLAIN',    # hive  PLAIN
    'user': 'xx',
    'password': 'xx'
}


cdh_params = {
    'host': '10.xxx.xx.xx',
    'port':21050,
    'database': 'xx',
    'timeout': 60,
    'auth_mechanism':'NOSASL',   # impala  NOSASL
    'user': 'xx',
    'password': 'xx'
}

cdh_conn = connect(**cdh_params)
cdh_cursor = cdh_conn.cursor()

cdh_cursor.execute('SHOW Tables')
cdh_tabs = cdh_cursor.fetchall()

python连接pg(PostgreSQL )

import psycopg2

cdh_params = {
    'host': '10.xx.xx.xx',
    'port':5432,
    'database': 'xx',
    'user': 'xx',
    'password': 'xx'
}

cdh_conn=psycopg2.connect(**cdh_params)
cdh_cursor = cdh_conn.cursor()

# 获取xx模式下的所有表
cdh_cursor.execute("select tablename from pg_tables where schemaname = 'xxx'")
cdh_tabs = cdh_cursor.fetchall()

python 通过Presto连接hive

import jaydebeapi

# 参数1:驱动类
# 参数2:jdbc 
# 参数3:user password
# 参数4:jar包本地路径

bdp_conn = jaydebeapi.connect('com.facebook.presto.jdbc.PrestoDriver',
                          'jdbc:presto://10.xx.xx.xx:4380/hive/dc_src?&SSL=true&SSLKeyStorePath=/Applications/DBeaver.app/Contents/MacOS/keystore.jks&SSLKeyStorePassword=xxxx',
                          {'user': "xxx", 'password': "xxx",},
                          "/Users/shylin/.dbeaver-drivers/maven/maven-central/com.facebook.presto/presto-jdbc-0.216.jar"
                          )
bdp_cursor = bdp_conn.cursor()

bdp_cursor.execute('SHOW Tables')

Shylin

你可能感兴趣的:(Python,数据库)