python hive mysql_python连接mysql、hive、oracle

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @Time : 2019/5/8 2:41 PM

# @Author : ouy

# @File : connect_test.py

import logging

from impala.dbapi import connect as hive_connect

from pymysql import connect as mysql_connect

from cx_Oracle import connect as oracle_connect

def try_connect(db_type, connect_params):

"""连接测试"""

conn = None

try:

if db_type not in ['mysql', 'oracle', 'hive']:

return {"code": "N", "message": "适配器配置错误!"}

elif db_type == 'mysql':

conn = mysql_connect(**connect_params, connect_timeout=10)

elif db_type == 'oracle':

# conn=cx_Oracle.connnet('用户名/密码@IP或者服务器域名:端口号/服务名')

conn = oracle_connect('{}/{}@{}:{}/{}'.format(*connect_params.values()))

# print(conn.version)

elif db_type == 'hive':

conn = hive_connect(**connect_params, auth_mechanism='PLAIN', timeout=10)

except Exception as e:

print(e)

logging.debug(e)

return 0, None

return 1, conn

if __name__ == '__main__':

schema1 = 'SYS'

db_type1 = 'oracle'

connect_params1 = {"user": "masdata", "password": "zhaoshang001", "host": "100.69.236.2", "port": 1634,

"database": "mas"}

# connect_params1 = {"user": "test", "password": "test", "host": "100.69.149.203", "port": 1634,

# "database": "dwt"}

# connect_params1 = {"user": "metadata", "password": "Icpm9d7X", "host": "100.69.149.205", "port": 3306,

# "database": "information_schema"}

# connect_params1 = {"host": "100.69.149.207", "port": 10001, "database": "test_yh", "user": "linkx001",

# "password": "Pass@word1"}

code1, conn1 = try_connect(db_type1, connect_params1)

print(code1, conn1)

cursor = conn1.cursor() # 创建游标

cursor.execute("select owner from all_tables group by owner")

# cursor.execute("show tables")

dbs_info = cursor.fetchall()

print(dbs_info)

cursor.close()

conn1.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值