#!/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()