![](/img/trans.png)
[英]Connect to SQL Server Analysis Cube with Python (Anaconda)
[英]Use Python to Query SQL Server Analysis Services (SSAS) cube Data
我的組織中有一個SQL分析服務資源,我們可以使用excel或powerbi連接到立方體使用服務器名稱(tooldata.amr.com)並獲取data.amr.xxx。
What i want is use python or excel to automate the data query and output to a csv file for downstream application use (reporting/chart etc.)
我在下面嘗試過但失敗了:
1.Microsoft.AnalysisServices.AdomdClient
FileNotFoundException Traceback(最近一次調用最后一次)在
2. clr.AddReference ("Microsoft.AnalysisServices.AdomdClient.dll")
FileNotFoundException:找不到程序集“Microsoft.AnalysisServices.AdomdClient.dll”。 在 Python.Runtime.CLRModule.AddReference(字符串名稱)
看起來缺少一些環境。 不知道如何進行。 有什么建議嗎?
import olap.xmla.xmla as xmla
provider = olap.xmla.xmla.XMLAProvider()
connect = provider.connect(location='http://tooldata.amr.xxx.com/OLAP/msmdpump.dll',username='user',password='pwd')
source = connect.getOLAPSource()
print (source.getCatalog("TestCube"))
ConnectionError:HTTPConnectionPool(host='tooldata.amr.xxx.com',port=80):url 超過最大重試次數:/OLAP/msmdpump.dll(由 NewConnectionError(':無法建立新連接:[Error1060) ] 連接嘗試失敗,因為連接方在一段時間后沒有正確響應,或者連接失敗,因為連接的主機沒有響應'))
看起來需要從服務器端進行一些配置,但它不在我的控制范圍內,請刪除此選項。
3.since i can use excel to get the SSAS data, is that possible use python to call excel and refresh the data, then parse out the data from excel? 有人試試嗎?
謝謝。
最后,基於1.Microsoft.AnalysisServices.AdomdClient方案解決問題。
#use your own DLL path.
clr.AddReference ("r"C:\Windows\assembly\GAC_MSIL\Microsoft.AnalysisServices.AdomdClient\11.0.0.0__89845dcd8080cc91\Microsoft.AnalysisServices.AdomdClient.dll"")
clr.AddReference ("System.Data")
from Microsoft.AnalysisServices.AdomdClient import AdomdConnection , AdomdDataAdapter
from System.Data import DataSet
#use your own server name or address. and data cube name.
conn = AdomdConnection("Data Source=tooldata.amr.xxx.com;Catalog=ShiftlyCellCube;")
conn.Open()
cmd = conn.CreateCommand()
#your MDX query, if you are not familiar, you can use the excel powerpivot to build one query for you.
cmd.CommandText = "your mdx query"
adp = AdomdDataAdapter(cmd)
datasetParam = DataSet()
adp.Fill(datasetParam)
conn.Close();
# datasetParam hold your result as collection a\of tables
# each tables has rows
# and each row has columns
print (datasetParam.Tables[0].Rows[0][0])
clr 是pythonnet ,您可以通過以下方式安裝 package: pythonnet Github或pythonnet pypi
對於Microsoft.AnalysisServices.AdomdClient.dll ,您可能沒有它。 您可以通過安裝SQL_AS_ADOMD.msi 獲得 DLL 。
最后,旨在從 Cube DataSet 中解析結構化數據集。 我使用以下代碼(字段取決於您的 DAX 查詢輸出)。
with open ('xx_Pivot.csv','w') as file:
#my MDX only return 7 field as below headers.
header = 'WW,Shift,ShiftID,Factory,Entity,Cell,Data\n'
file.writelines(header)
#iteration the Dataset and get out a structure 2D data table and save to a file.
for row_n in range(len(list(datasetParam.Tables[0].Rows))):
row = ''
for column_n in range(7):
data = datasetParam.Tables[0].Rows[row_n][column_n]
row = row+str(data)+','
row = row+'\n'
file.writelines(row)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.