简体   繁体   English

使用 Python 查询 SQL 服务器分析服务 (SSAS) 多维数据集数据

[英]Use Python to Query SQL Server Analysis Services (SSAS) cube Data

There is a SQL analysis service resource in my organization, we can use excel or powerbi to connect to the cube use a server name (tooldata.amr.xxx.com) and get the 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.) 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.)

I've tried below but failed:我在下面尝试过但失败了:

1. Microsoft.AnalysisServices.AdomdClient 1.Microsoft.AnalysisServices.AdomdClient

FileNotFoundException Traceback (most recent call last) in FileNotFoundException Traceback(最近一次调用最后一次)在

2. clr.AddReference ("Microsoft.AnalysisServices.AdomdClient.dll")

FileNotFoundException: Unable to find assembly 'Microsoft.AnalysisServices.AdomdClient.dll'. FileNotFoundException:找不到程序集“Microsoft.AnalysisServices.AdomdClient.dll”。 at Python.Runtime.CLRModule.AddReference(String name)在 Python.Runtime.CLRModule.AddReference(字符串名称)

look like lack of some env.看起来缺少一些环境。 not sure how to proceed.不知道如何进行。 any suggestion?有什么建议吗?

2. use olap.xmla 2.使用olap.xmla

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')) ConnectionError:HTTPConnectionPool(host='tooldata.amr.xxx.com',port=80):url 超过最大重试次数:/OLAP/msmdpump.dll(由 NewConnectionError(':无法建立新连接:[Error1060) ] 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应'))

Looks like need some configuration from server side, but it's out of my control, drop this option.看起来需要从服务器端进行一些配置,但它不在我的控制范围内,请删除此选项。

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? 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? have any one try that?有人试试吗?

thanks.谢谢。

Finally, the problem solved based on 1.Microsoft.AnalysisServices.AdomdClient solution.最后,基于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 is pythonnet , you can install the package via: pythonnet Github or pythonnet pypi clr 是pythonnet ,您可以通过以下方式安装 package: pythonnet Githubpythonnet pypi

And for the Microsoft.AnalysisServices.AdomdClient.dll probably you don't have it.对于Microsoft.AnalysisServices.AdomdClient.dll ,您可能没有它。 you can get the DLL by install SQL_AS_ADOMD.msi .您可以通过安装SQL_AS_ADOMD.msi 获得 DLL

Lastly, aim to parse a structured dataset from the Cube DataSet.最后,旨在从 Cube DataSet 中解析结构化数据集。 i use below code (field depends on your DAX query output).我使用以下代码(字段取决于您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM