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

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',
    # 'auth_mechanism':'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

猜你喜欢

转载自blog.csdn.net/Shyllin/article/details/87883284
今日推荐