Dask 如何从 SQL 服务器读取 Python

How does Dask read from SQL Server in Python

我必须使用 dask dataframe,因为我的数据非常庞大,有 1.5 亿行和 50,000 列

我尝试使用

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DELL;'
                      'Database=DB;'
                      'Trusted_Connection=yes;')
df_features = dd.read_sql_table(table="Features" , con=conn , index_col="ID")

我遇到了这个错误

TypeError: read_sql_table() missing 1 required positional argument: 'uri'

所以我尝试了这个

df_features = dd.read_sql_table(table="Features" , uri="mssql+pyodbc:///?odbc_connect=Driver={SQL Server}; Server=DELL; Database=DB; Trusted_Connection=yes;", index_col="ID")

遇到这个错误

OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/e3q8)

如何在 dask 数据帧中连接到 SQL 服务器?

@Giorgos-Myrianthous 说得对:您需要一个 URI,而不是某个连接对象。此 URI 的格式需要是 SQLalchemy 可以理解的格式,因此请阅读他们的文档以了解如何为 ODBC 设置格式。

文档:https://docs.sqlalchemy.org/en/13/dialects/mysql.html#module-sqlalchemy.dialects.mysql.pyodbc

原因是,Dask 需要能够序列化和传递任务,但无法序列化连接对象。但是,如果您只使用线程,则可以传递一个 SQLalchemy 引擎对象(在 master 中)。

Windows ODBC 驱动程序管理器要求关键字和 =

之间没有空格

以下应该可以解决问题:

df_features = dd.read_sql_table(
    table="Features",
    uri="mssql+pyodbc:///?odbc_connect=DRIVER={SQL Server};SERVER=my.db.server;DATABASE=DB;Trusted_Connection=yes;",
    index_col="ID"
)