我想使用Dask来提取一堆数据。
我正在使用
SQLAlchemy==1.3.9
dask==2.5.2
pyodbc==4.0.27
我在同一目录下有以下文件作为草稿:
odbc.ini
odbcinst.ini
我无法正确配置。我看到了一堆SQLAlchemy的连接字符串,但我没能让其中任何一个工作。
此代码有效:
#!/usr/bin/env python
import pyodbc
import getpass
#odbc is the prefered method for contacting microsoft sqlserver
sql_server_cnxn_str = 'DRIVER={ODBC Driver 17 for SQL Server};' +
'SERVER=XXXX;DATABASE=YYYY;' +
'Trusted_Connection=yes;'
#get username
print ("Input username then hit enter: ")
name = input()
#Get password
psswd = getpass.getpass()
#Create connection using odbc
conn = pyodbc.connect(sql_server_cnxn_str)
cursor = conn.cursor()
query = "SELECT * from Foo.Testing"
cursor.execute(query)
data = cursor.fetchall()
print(data)
所以我知道这种联系是有效的。
这是我试图开始工作的代码:
#!/usr/bin/env python
import pyodbc
import getpass
import dask.dataframe as dd
from dask.diagnostics import ProgressBar
#odbc is the prefered method for contacting microsoft sqlserver
sql_server_cnxn_str = 'DRIVER={ODBC Driver 17 for SQL Server};' +
'SERVER=XXXX;DATABASE=YYYY;' +
'Trusted_Connection=yes;'
#get username
print ("Input username then hit enter: ")
name = input()
#Get password
psswd = getpass.getpass()
#If neither divisions or npartitions is given, the memory footprint of the first few rows will be determined, and partitions of size ~256MB will be used.
data = dd.read_sql_table("Foo.Testing", sql_server_cnxn_str, index_col="Test")
我一直收到这个错误:
File "src/pymssql.pyx", line 642, in pymssql.connect
sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (18456, b"Login failed for user 'foobar'.DB-Lib error message 20018, severity 14:nGeneral SQL Server error: Check messages from the SQL ServernDB-Lib error message 20002, severity 9:nAdaptive Server connection failed (ROEFDN819Q)nDB-Lib error message 20002, severity 9:nAdaptive Server connection failed (ROEFDN819Q)n")
我尝试过许多连接字符串格式,包括:'mssql+pyodbc://server_name/database_name?driver=SQL服务器Trusted_Connection=yes'
"mssql+pymssql://{user}:{password}@{host}:{port}/{database}".format(user=username, password=password, host=server, database=database, port=port)
("mssql+pyodbc://%s:%s@%s/%s?driver=%s" % (username, password, server, database, driver ) )
"mssql+pyodbc://{0}:{1}@XXXX/YYYY".format(username,password)
什么都不管用。难道我就不能以某种方式从pyodbc中提取正确的配置吗?非常感谢你的帮助!
这个怎么样?
import pypyodbc
cnxn = pypyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=Server_Name;"
"Database=DB_Name;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Actions')
for row in cursor:
print('row = %r' % (row,))
或者,使用登录凭据,试试这个?
pyodbc.connect("Driver = {SQL Server Native Client 11.0};"
"Server = Server_Name;"
"Database = Database_Name;"
"username = User_Name;"
"password = User_Password;"
"Trusted_Connection = yes;")
我以前从未使用过Dask,但这看起来很有希望。
username = 'jesse'
password = 'DataScienceRulez'
hostname = 'localhost'
database_name = 'DSAS'
odbc_driver = 'ODBC+Driver+13+for+SQL+Server'
connection_string = 'mssql+pyodbc://{0}:{1}@{2}/{3}?driver={4}'.format(username, password, hostname, database_name, odbc_driver)
data = dd.read_sql_table('violations', connection_string, index_col='Summons Number')
资源:
https://livebook.manning.com/book/data-science-at-scale-with-python-and-dask/chapter-4/72