为了让它工作,我必须从 Condaforge 添加安装 vertica-python 模块。
Redsift、MySQL 和 MSSQL 使用简单的连接字符串工作
def _get_generic_connection(self):
"""
Creates a connection that can be used directly by the sqlalchemy library.
Returns: A sqlalchemy database connection
"""
return create_engine(<<your connection string>>)
您必须执行以下操作
from sqlalchemy import create_engine
import vertica_python
def _get_vertica_connection(self):
"""
Creates a connection appropriate for HP Vertica based on the vertica_python library.
Returns: A vertica_python database connection
"""
conn_info = {'host': <<your host>>,
'port': << Vertica port>>,
'user': << appropriate user >>,
'password': << appropriate password >>,
'database': << your db name >>,
# 10 minutes timeout on queries
'read_timeout': 600,
# default throw error on invalid UTF-8 results
'unicode_error': 'strict',
# SSL is disabled by default
'ssl': False,
'connection_timeout': 300
# connection timeout is not enabled by default
}
return vertica_python.connect(**conn_info)
我有一个具有这两个功能的类
def __init__(self, app_config):
"""
Args:
app_config( ApplicationConfiguration): Object to handle the configuration of the system
"""
self._app_config = app_config
self._platform = app_config.db_server.db_platform
self._connection_function_dict = {
"vertica": self._get_vertica_connection,
"redshift": self._get_generic_connection,
"mssql": self._get_generic_connection
}
def get_db_connection(self):
"""
Acts as the public method to retrieve a database connection for use by Pandas.
Returns: A database connection of a type dictated by the database platform
"""
db_connection = self._connection_function_dict[self._platform]()
if db_connection is None:
raise NameError("Database platform \"{}\" not known".format(self._platform))
return db_connection
这足以产生与我们使用的任何平台的有效连接,并且 SQLAlchemy 对这些连接感到满意。这意味着你可以这样做
pandas.read_sql(<<your SQL query>>, << your connection>>)