我单位有SQL分析服务资源,我们可以用excel或者powerbi通过服务器名(tooldata.amr.xxx.com)连接cube获取数据。
我想要的是使用 python 或 excel 自动进行数据查询并输出到 csv 文件以供下游应用程序使用(报告/图表等)
我试过以下但失败了:
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): Max retries exceeded with url: /OLAP/msmdpump.dll (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
看起来需要从服务器端进行一些配置,但这是我无法控制的,请删除此选项。
3.既然可以用excel获取SSAS数据,那可不可以用python调用excel刷新数据,再从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,您可以通过以下方式安装软件包: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)
关于python - 使用 Python 查询 SQL Server Analysis Services (SSAS) 多维数据集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316192/