创建和使用SQL Server SSAS本地多维数据集

Microsoft SQL Server SSAS的本地多维数据集(即Local Cube,也叫脱机多维数据集)和本地挖掘模型(Local Mining Models)允许在客户端机器上脱机执行离线分析(多维分析与数据挖掘),从而对有离线分析要求(能够在断开网络连接的情况下脱离SSAS服务器进行数据分析)的软件开发提供了一种备选手段。

本地多维数据集联机/脱机示意

本地多维数据集联机/脱机示意

1、创建本地多维数据集

创建本地多维数据集有多种方式,按默认约定生成的本地文件扩展名为*.cub。

1)  从SQL Server服务器上现有的SSAS多维数据集(Cube)创建

在这种方式下,创建的结果是选定的源Cube的子集,可以在创建语句中指定Local Cube中包含的度量值、维度,还可以指定维度的切片和切块等。

源Cube中需要包含在Local Cube中的计算成员和命名集无需指定,SSAS在生成Local Cube时将执行语法分析,在Local Cube中能够满足依赖关系的计算成员和命名集会被自动包含进来,而不符合依赖关系者将会在生成Local Cube时自动排除。

具体有两种比较方便的手段:

A)MDX语句

从现有SSAS Cube创建时,可使用”Create global cube” MDX语句创建Local Cube,如果数据量大,最好直接在SSAS服务器上本地执行MDX语句。

B)Excel操作

用Excel创建local Cube,该方法很方便,但只适合数据量很小的情况。步骤是:

通过Excel的“数据➡来自其他数据源➡来自Analysis Service”菜单项,启动连接SSAS数据源的向导窗体后,一路向后随意点击生成pivottable,然后通过“数据透视表工具➡OLAP工具➡脱机OLAP”菜单项启动excel“创建脱机数据文件”的向导。

2)  从关系数据库,从头定义、创建和填充多维数据集

这种方式需要使用 Analysis Services Scripting Language(ASSL)脚本语言,能够构造出非常强大、灵活的解决方案。

以下是从SQL Server 2008/2008R2/2012附带的Adventure Works多维数据库,用MDX语句创建Local Cube的示例。

1)示例1——从SSAS服务器上的[Adventure Works] 示例多维数据集创建Local Cube,结果是“Reseller Sales”度量值组的子集。

创建Local Cube
1
2
3
4
5
6
7
8
CREATE GLOBAL CUBE [LocalReseller]
Storage 'D: empLocalAWReseller.cub'
FROM [Adventure Works]
(
MEASURE  [Adventure Works].[Reseller Sales Amount],
DIMENSION [Adventure Works].[Reseller],
DIMENSION [Adventure Works].[ Date ]
)

2)示例2——从SSAS服务器上的[Adventure Works] 示例多维数据集创建Local Cube,结果是 “Sales Summary”度量值组的子集

创建Local Cube
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CREATE GLOBAL CUBE [LocalSalesSummary]
Storage 'D: empLocalSalesSummary.cub'
FROM [Adventure Works]
(
//计算成员无法包含在 Local Cube
          //MEASURE  [Adventure Works].[Average Sales Amount],
//MEASURE  [Adventure Works].[Average Unit Price],
//MEASURE  [Adventure Works].[Expense to Revenue Ratio],
MEASURE  [Adventure Works].[Extended Amount],
MEASURE  [Adventure Works].[Freight Cost],
//MEASURE  [Adventure Works].[Gross Profit],
//MEASURE  [Adventure Works].[Gross Profit Margin],
MEASURE  [Adventure Works].[ Order Quantity],
//MEASURE  [Adventure Works].[Ratio to All Products],
//MEASURE  [Adventure Works].[Ratio to Parent Product],
MEASURE  [Adventure Works].[Sales Amount],
MEASURE  [Adventure Works].[Standard Product Cost],
MEASURE  [Adventure Works].[Tax Amount],
MEASURE  [Adventure Works].[Total Product Cost],
         DIMENSION [Adventure Works].[ Date ],
DIMENSION [Adventure Works].[Delivery Date ],
//DIMENSION [Adventure Works].[Destination Currency],
DIMENSION [Adventure Works].[Product],
DIMENSION [Adventure Works].[Promotion],
DIMENSION [Adventure Works].[Sales Channel],
//DIMENSION [Adventure Works].[Sales Summary Order Details],
DIMENSION [Adventure Works].[Sales Territory],
DIMENSION [Adventure Works].[Ship Date ],
DIMENSION [Adventure Works].[Source Currency]
)

2、使用本地多维数据集

针对不同应用目的,客户端应用程序可通过以下几种方式执行加载本地多维数据集引擎、创建本地多维数据集、打开本地多维数据集、查询本地多维数据集等操作。

1)OLE DB for OLAP

2)ADMOD.NET

3)Analysis Management Objects (AMO)

在使用本地多维数据集时,如果不想在客户端机器上安装SQL Server SSAS服务器/客户端应用,则可以访问微软网站,下载SQL Server 2008/2008R2/2012特性包(sql server feature pack)中的ADOMD.net、AMO、Ole DB for Olap等组件的单独安装文件,在客户端安装即可支持。

以下ADMOD.NET方式使用Local Cube的示例

使用Local Cube
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//声明
 
using adoMdClient = Microsoft.AnalysisServices.AdomdClient;
 
private adoMdClient.AdomdConnection cnAdomd;
 
……
 
//连接 Local Cube
 
cnAdomd = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection();
cnAdomd.ConnectionString = “Data Source= D: temp LocalAWReseller.cub”;
cnAdomd. Open ();
 
……
 
//提交MDX查询并获取多维查询结果
adoMdClient.AdomdCommand mdxCommand = cnAdomd.CreateCommand();
mdxCommand.CommandText = “ select [Measures].[Sales Amount] on 0, [Product].[Product Categories].[ All Products].Children on 1 from [Adventure Works]”;
adoMdClient.CellSet adomdCellSet = mdxCommand.ExecuteCellSet();

3、补充说明

关于刷新和重新创建脱机多维数据集文件,本地多维数据集在创建、使用、安全性等方面的约束条件等话题,请查阅SSAS手册详细了解。

4、附图:Excel创建Local Cube的示意图

在Excel中创建本地多维数据集-1

在Excel中创建本地多维数据集-1

在Excel中创建本地多维数据集-2

在Excel中创建本地多维数据集-2

原文地址:https://www.cnblogs.com/jiangu66/p/3172152.html