2017-11-27 12 views
0

Dask read_sql_table 메서드에서 머리를 감싸는 데 어려움이 있습니다. 필자는 데이터베이스에 잘 연결하지만 인덱스 열로 사용하고자하는 열은 정수 만 포함하는 char 형식입니다.Dask/Python : read_sql_table의 인덱스 열에서 char을 int로 변환

나는 시도했다 :

내가 오류가

from urllib.parse import quote_plus 
server = 'SERVER_NAME' 
db = 'DB_NAME' 
table = 'TABLE_NAME' 
connection_string = 'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes' 
connection_string = quote_plus(connection_string) 
connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string 
df = dd.read_sql_table(table,connection_string,'sql.cast(sql.column("ID"),int).label("ID")') 

(가 회사 데이터베이스이기 때문에 서버, DB, 테이블 및 열 이름이 모두 여기에 인형으로 대체되었습니다)

KeyError         Traceback (most recent call last) 
<ipython-input-25-8e261dcd8696> in <module>() 
    6 connection_string = quote_plus(connection_string) 
    7 connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string 
----> 8 df = dd.read_sql_table(table,connection_string,'sql.cast(sql.column("ID"),int).label("ID")') 

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\dask\dataframe\io\sql.py in read_sql_table(table, uri, index_col, divisions, npartitions, limits, columns, bytes_per_chunk, **kwargs) 
73       schema=schema) 
74 
---> 75  index = (table.columns[index_col] if isinstance(index_col, six.string_types) 
76    else index_col) 
77  if not isinstance(index_col, six.string_types + (elements.Label,)): 

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\sqlalchemy\util\_collections.py in __getitem__(self, key) 
192 
193  def __getitem__(self, key): 
--> 194   return self._data[key] 
195 
196  def __delitem__(self, key): 

KeyError: 'sql.cast(sql.column("ID"),int).label("ID")' 

누구나 수정 방법을 알고 계십니까?

답변

2

OK ... 그게 전부 였어. 오류는 SQLalchemy 표현식을 문자열로 전달하려고 시도했기 때문입니다. SQLalchemy 모듈을로드하고 적절한 표현식으로 작성해야합니다.

from urllib.parse import quote_plus 
from sqlalchemy import sql, types 
server = 'SERVER_NAME' 
db = 'DB_NAME' 
table = 'TABLE_NAME' 
connection_string = 'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes' 
connection_string = quote_plus(connection_string) 
connection_string = 'mssql+pyodbc:///?odbc_connect='+connection_string 
df = dd.read_sql_table(table,connection_string,sql.cast(sql.column("ID"),types.BigInteger).label("ID")) 
+0

문서 개선 사항은 물론 인정됩니다. 함수의 이름이 암시 하듯이, 표현식없이 테이블을로드하는 것만이 우세한 사용이 될 것으로 예상됩니다. – mdurant

+0

확실히! 나는 DB로 저주를 받았기 때문에 자신을 제어 할 수 없다. 우리가 기계 학습 워크 플로우에서 dask를 사용하기 시작할 때 문서에 대한 예제를 제공하게되어 기쁩니다 –

+0

당신 자신의 대답을 받아 들일 수 있습니다 – mdurant