python连接clickhouse、mysql、达芬奇库Davinci、hive

存一下用到过的连接方式

1.ck库

#连接ck库
import pandas as pd
from clickhouse_driver import Client
import re
client=Client(host='10.1.8.126', port='端口', user='用户名', password=r'密码')
def read_cksql(sql):
    data, columns = client.execute(sql, columnar=True, with_column_types=True)
    df = pd.DataFrame({re.sub(r'\W', '_', col[0]): d for d, col in zip(data, columns)})
    return df
sql = '''
select *
from table
'''
data = read_cksql(sql)

2.mysql

import numpy as np
import pandas as pd
import pymysql
from pymysql.cursors import DictCursor
import datetime
import time
#链接数据库
conn = pymysql.connect(host="10.1.4.108",port=1234,user="用户名",passwd="密码",charset="utf8")
cur = conn.cursor(DictCursor)
sql = '''
select *
from table
'''
# 执行sql语句
cur.execute(sql3)
# 获取查询的所有结果
data=pd.DataFrame(cur.fetchall())

3.达芬奇Davinci

import pandas as pd
from clickhouse_driver import Client
import re
from datetime import *
from dateutil.relativedelta import relativedelta
import pandas as pd
from clickhouse_driver import Client
import psycopg2
## 读取达芬奇数据
def read_pgsql(sql):
    conn = psycopg2.connect(database="使用的库", user="用户名", password="密码",
                            host="10.1.1.173",
                            port="端口"
                            )
    cur = conn.cursor()
    cur.execute(sql)
    df = pd.DataFrame(cur.fetchall(), columns=dict([desc[:2] for desc in cur.description]))
    return df
pgsql ='''
	select *
	from table 
'''
data=read_pgsql(pgsql)

4.hive

from pyhive import hive
import pandas as pd
# 读取数据
def select_pyhive(sql):
    # 创建hive连接
    conn = hive.Connection(host='10.1.8.247', port=端口, username='用户名', password='密码', database='使用的库', auth='LDAP')
    cur = conn.cursor()
    try:
        #c = cur.fetchall()
        df = pd.read_sql(sql, conn)
        return df
    finally:
        if conn:
            conn.close()
sql=''' 
select * 
from table
'''
data = select_pyhive(sql)

你可能感兴趣的:(python,clickhouse,mysql,hive,数据库)