编程 AMO OLAP 高级对象

适用于: SQL Server Analysis Services Azure Analysis Services Fabric/Power BI Premium

本主题介绍 OLAP 高级对象的分析管理对象(AMO)编程详细信息。

作对象

浏览多维数据集的某些区域时,作类用于创建活动响应。 可以使用 AMO 定义动作对象,但这些对象由用于浏览数据的客户端应用程序来使用。 操作可以是不同类型的,必须根据其类型进行创建。 作可以是:

  • 钻取作,返回一组行,这些行表示发生作的多维数据集的选定单元格的基础数据。

  • 报告操作,这类操作返回与动作所在的多维数据集的选定部分相关的报表。

  • 标准作,该作返回与作所在的多维数据集的选定部分关联的作元素(URL、HTML、DataSet、RowSet 和其他元素)。

创建操作对象需要以下步骤:

  1. 创建派生的动作对象并填充基本属性。

    以下是基本属性:动作类型、多维数据集的目标类型或部分、动作可用的多维数据集目标或特定区域、标题以及该标题是 MDX 表达式的地方。

  2. 填充动作类型的特定属性。

    对于三种类型的操作,属性不同,请参阅后面的代码示例了解相关参数。

  3. 将操作添加到多维数据集集合,并更新多维数据集。 该操作不是可更新的对象。

测试该操作需要使用不同的应用程序。 可以在 Visual Studio 中测试操作。

以下源代码复制了 Adventure Works 分析服务项目的示例中的三个不同操作。 你可以区分这些操作,因为使用以下示例引入的操作名称都以“我的”开头。

static public void CreateActions(Cube cube)  
{  
    #region Adding a drillthrough action  
    // Verify That action exists and drop it  
    if (cube.Actions.ContainsName("My Reseller Details"))  
        cube.Actions.Remove("My Drillthrough Action",true);  
  
    //Create a Drillthrough action  
    DrillThroughAction dtaction = new DrillThroughAction("My Reseller Details", "My Drillthrough Action");  
  
    //Define the Action  
    dtaction.Type = ActionType.DrillThrough;  
    dtaction.TargetType = ActionTargetType.Cells;  
    dtaction.Target = "MeasureGroupMeasures(\"Reseller Sales\")";  
    dtaction.Caption = "My Drillthrough...";  
    dtaction.CaptionIsMdx = false;  
  
    #region create drillthrough action specifics  
    //Adding Drillthrough columns  
    //Adding Measure columns to the drillthrough  
    MeasureGroup mg = cube.MeasureGroups.FindByName("Reseller Sales");  
    MeasureBinding mb1 = new MeasureBinding();  
    mb1.MeasureID = mg.Measures.FindByName( "Reseller Sales Amount").ID;  
    dtaction.Columns.Add(mb1);  
  
    MeasureBinding mb2 = new MeasureBinding();  
    mb2.MeasureID = mg.Measures.FindByName("Reseller Order Quantity").ID;  
    dtaction.Columns.Add(mb2);  
  
    MeasureBinding mb3 = new MeasureBinding();  
    mb3.MeasureID = mg.Measures.FindByName("Reseller Unit Price").ID;  
    dtaction.Columns.Add(mb3);  
  
    //Adding Dimension Columns to the drillthrough  
    CubeAttributeBinding cb1 = new CubeAttributeBinding();  
    cb1.CubeID = cube.ID;  
    cb1.CubeDimensionID = cube.Dimensions.FindByName("Reseller").ID;  
    cb1.AttributeID = "Reseller Name";  
    cb1.Type = AttributeBindingType.All;  
    dtaction.Columns.Add(cb1);  
  
    CubeAttributeBinding cb2 = new CubeAttributeBinding();  
    cb2.CubeID = cube.ID;  
    cb2.CubeDimensionID = cube.Dimensions.FindByName("Product").ID;  
    cb2.AttributeID = "Product Name";  
    cb2.Type = AttributeBindingType.All;  
    dtaction.Columns.Add(cb2);  
    #endregion  
  
    //Add the defined action to the cube  
    cube.Actions.Add(dtaction);  
    #endregion  
  
    #region Adding a Standard action  
    // Verify That action exists and drop it  
    if (cube.Actions.ContainsName("My City Map"))  
        cube.Actions.Remove("My Action", true);  
  
    //Create a Drillthrough action  
    StandardAction stdaction = new StandardAction("My City Map", "My Action");  
  
    //Define the Action  
    stdaction.Type = ActionType.Url;  
    stdaction.TargetType = ActionTargetType.AttributeMembers;  
    stdaction.Target = "[Geography].[City]";  
    stdaction.Caption = "\"My View Map for \" + [Geography].[City].CurrentMember.Member_Caption + \"...\"";  
    stdaction.CaptionIsMdx = true;  
  
    #region create standard action specifics  
    stdaction.Expression = "\"http://maps.msn.com/home.aspx?plce1=\" + " +  
        "[Geography].[City].CurrentMember.Name + \",\" + " +  
        "[Geography].[State-Province].CurrentMember.Name + \",\" + " +  
        "[Geography].[Country].CurrentMember.Name + " +  
        "\"&regn1=\" + " +  
        "Case " +  
            "When [Geography].[Country].CurrentMember Is " +  
                    "[Geography].[Country].&[Australia] " +  
                "Then \"3\" " +  
            "When [Geography].[Country].CurrentMember Is " +  
                    "[Geography].[Country].&[Canada] " +  
                "Or [Geography].[Country].CurrentMember Is " +  
                    "[Geography].[Country].&[United States] " +  
                "Then \"0\" " +  
                "Else \"1\" " +  
        "End ";  
    #endregion  
  
    //Add the defined action to the cube  
    cube.Actions.Add(stdaction);  
  
    #endregion  
  
    #region Adding a Reporting action  
    // Verify That action exists and drop it  
    if (cube.Actions.ContainsName("My Sales Reason Comparisons"))  
        cube.Actions.Remove("My Report Action", true);  
  
    //Create a Report action  
    ReportAction rsaction = new ReportAction("My Sales Reason Comparisonsp", "My Report Action");  
  
    //Define the Action  
    rsaction.Type = ActionType.Report;  
    rsaction.TargetType = ActionTargetType.AttributeMembers;  
    rsaction.Target = "[Product].[Category]";  
    rsaction.Caption = "\"My Sales Reason Comparisons for \" + [Product].[Category].CurrentMember.Member_Caption + \"...\"";  
    rsaction.CaptionIsMdx = true;  
  
    #region create Report action specifics  
    rsaction.ReportServer = "MyRSSamplesServer";  
    rsaction.Path = "ReportServer?/AdventureWorks Sample Reports/Sales Reason Comparisons";  
    rsaction.ReportParameters.Add("ProductCategory", "UrlEscapeFragment( [Product].[Category].CurrentMember.UniqueName )");  
    rsaction.ReportFormatParameters.Add("rs:Command", "Render");  
    rsaction.ReportFormatParameters.Add("rs:Renderer", "HTML5");  
    #endregion  
  
    //Add the defined action to the cube  
    cube.Actions.Add(rsaction);  
  
    #endregion  
}  

KPI 对象

关键绩效指标(KPI)是与多维数据集中的度量值组关联的计算集合,用于评估业务成功。 Kpi 对象可以由 AMO 定义,但实际用于浏览数据的是客户端应用程序。

Kpi创建对象需要以下步骤:

  1. Kpi创建对象并填充基本属性。

    下面是基本属性的列表:说明、显示文件夹、关联的度量值组和值。 显示文件夹指示客户端应用程序,KPI 应该放置在何处以便终端用户找到它。 关联的度量值组是指所有 MDX 计算应参考的度量值组。 值将性能指标的实际值显示为 MDX 表达式。

  2. 定义 KPI 指标:目标、状态和趋势。

    指示器是应在 -1 到 1 之间计算的 MDX 表达式,但实际定义指示器取值范围的是浏览应用程序。

  3. 浏览 KPI 时,小于 -1 的值被视为 -1,大于 1 的值被视为 1。

  4. 定义图形图像。

    图形图像是字符串值,用作客户端应用程序中的引用,用于标识要显示的正确图像集。 图形图像字符串还定义显示函数的行为。 通常,范围被划分为一个奇数数量的状态,从坏到好,并且从图像集中为每个状态分配一个图像。

    如果使用 Visual Studio 浏览 KPI,则根据名称,指示器范围拆分为三种状态或五种状态。 此外,还有一些名称将反转范围,即 -1 为“Good”,1 为“Bad”。 在 Visual Studio 中,范围内的三种状态如下所示:

    • Bad = -1 到 -0.5

    • OK = -0.4999 到 -0.4999

    • Good = 0.50 到 1

    在 Visual Studio 中,范围内的五个状态如下所示:

    • Bad = -1 到 -0.75

    • 风险 = -0.7499 到 -0.25

    • OK = -0.2499 到 0.2499

    • 提升 = 0.25 到 0.7499

    • Good = 0.75 到 1

下表列出了与映像关联的使用情况、名称和状态数。

映像使用情况 映像名称 状态数
状态 Shapes 3
状态 交通灯 3
状态 路标 3
状态 仪表 3
状态 反向仪表 5
状态 温度计 3
状态 缸 3
状态 Faces 3
状态 用于指示方差的箭头 3
趋势 标准箭头 3
趋势 状态箭头 3
趋势 反向状态箭头 5
趋势 Faces 3
  1. 将 KPI 添加到多维数据集集合并更新多维数据集,因为 KPI 不是可更新的对象。

测试 KPI 需要不同的程序应用程序。 可以在 Visual Studio 中测试 KPI。

以下示例代码在 Adventure Works Analysis Services 示例项目中包含的 Adventure Works 多维数据集的“财务视角/增长收入”文件夹中创建一个KPI。

static public void CreateKPIs(Cube cube)  
{  
    Kpi kpi = cube.Kpis.Add("My Internet Revenue", "My Internet Revenue");  
    kpi.Description = "(My) Revenue achieved through direct sales via Interner";  
    kpi.DisplayFolder = "\\Financial Perspective\\Grow Revenue";  
    kpi.AssociatedMeasureGroupID = "Internet Sales";  
    kpi.Value = "[Measures].[Internet Sales Amount]";  
    #region Goal  
    kpi.Goal = "Case" +  
               "     When IsEmpty" +  
               "          (" +  
               "            ParallelPeriod" +  
               "            (" +  
               "              [Date].[Fiscal Time].[Fiscal Year]," +  
               "              1," +  
               "              [Date].[Fiscal Time].CurrentMember" +  
               "            )" +  
               "          )" +   
               "     Then [Measures].[Internet Sales Amount]" +  
               "     Else 1.10 *" +  
               "          (" +  
               "            [Measures].[Internet Sales Amount]," +  
               "            ParallelPeriod" +  
               "            (" +  
               "              [Date].[Fiscal Time].[Fiscal Year]," +  
               "              1," +  
               "              [Date].[Fiscal Time].CurrentMember" +  
               "            )" +  
               "          ) " +  
               " End";  
    #endregion  
    #region Status  
    kpi.Status = "Case" +  
                 "   When KpiValue( \"Internet Revenue\" ) / KpiGoal ( \"Internet Revenue\" ) >= .95 " +  
                 "   Then 1 " +  
                 "   When KpiValue( \"Internet Revenue\" ) / KpiGoal ( \"Internet Revenue\" ) <  .95 " +  
                 "        And  " +  
                 "        KpiValue( \"Internet Revenue\" ) / KpiGoal ( \"Internet Revenue\" ) >= .85 " +  
                 "   Then 0 " +  
                 "   Else -1 " +  
                 "End";  
    #endregion  
    #region Trend  
    kpi.Trend = "Case " +  
                "    When VBA!Abs " +  
                "         ( " +  
                "           KpiValue( \"Internet Revenue\" ) -  " +  
                "           ( " +  
                "             KpiValue ( \"Internet Revenue\" ), " +  
                "             ParallelPeriod " +  
                "             (  " +  
                "               [Date].[Fiscal Time].[Fiscal Year], " +  
                "               1, " +  
                "               [Date].[Fiscal Time].CurrentMember " +  
                "             ) " +  
                "           ) / " +  
                "           ( " +  
                "             KpiValue ( \"Internet Revenue\" ), " +  
                "             ParallelPeriod " +  
                "             (  " +  
                "               [Date].[Fiscal Time].[Fiscal Year], " +  
                "               1, " +  
                "               [Date].[Fiscal Time].CurrentMember " +  
                "             ) " +  
                "           )   " +  
                "         ) <=.02  " +  
                "    Then 0 " +  
                "    When KpiValue( \"Internet Revenue\" ) -  " +  
                "         ( " +  
                "           KpiValue ( \"Internet Revenue\" ), " +  
                "           ParallelPeriod " +  
                "           (  " +  
                "             [Date].[Fiscal Time].[Fiscal Year], " +  
                "             1, " +  
                "             [Date].[Fiscal Time].CurrentMember " +  
                "           ) " +  
                "         ) / " +  
                "         ( " +  
                "           KpiValue ( \"Internet Revenue\" ), " +  
                "           ParallelPeriod " +  
                "           (  " +  
                "             [Date].[Fiscal Time].[Fiscal Year], " +  
                "             1, " +  
                "             [Date].[Fiscal Time].CurrentMember " +  
                "           ) " +  
                "         )  >.02 " +  
                "    Then 1 " +  
                "    Else -1 " +  
                "End";  
    #endregion  
    kpi.TrendGraphic = "Standard Arrow";  
    kpi.StatusGraphic = "Cylinder";  
}.  

透视对象

Perspective 对象可以由 AMO 定义,但实际用于浏览数据的是客户端应用程序。

Perspective创建对象需要以下步骤:

  1. Perspective创建对象并填充基本属性。

    下面是基本属性的列表:名称、默认度量值、说明和注释。

  2. 将可供最终用户查看的父多维数据集中的所有对象添加进去。

    添加多维数据集维度(属性和层次结构)、度量值组(指标和度量值组)、动作、KPI和计算。

  3. 将透视添加到多维数据集集合并更新多维数据集,因为透视不是可更新的对象。

测试透视需要不同的应用程序。

下面的代码示例为提供的多维数据集创建了一个名为“Direct Sales”的视角。

static public void CreatePerspectives(Cube cube)  
{  
    Perspective perspective = cube.Perspectives.Add("Direct Sales", "Direct Sales");  
    CubeDimension dim1 = cube.Dimensions.GetByName("Date");  
    PerspectiveDimension pdim1 = perspective.Dimensions.Add(dim1.DimensionID);  
    pdim1.Attributes.Add("Date");  
    pdim1.Attributes.Add("Calendar Year");  
    pdim1.Attributes.Add("Fiscal Year");  
    pdim1.Attributes.Add("Calendar Quarter");  
    pdim1.Attributes.Add("Fiscal Quarter");  
    pdim1.Attributes.Add("Calendar Month Number");  
    pdim1.Attributes.Add("Fiscal Month Number");  
    pdim1.Hierarchies.Add("Calendar Time");  
    pdim1.Hierarchies.Add("Fiscal Time");  
  
    CubeDimension dim2 = cube.Dimensions.GetByName("Product");  
    PerspectiveDimension pdim2 = perspective.Dimensions.Add(dim2.DimensionID);  
    pdim2.Attributes.Add("Product Name");  
    pdim2.Attributes.Add("Product Line");  
    pdim2.Attributes.Add("Model Name");  
    pdim2.Attributes.Add("List Price");  
    pdim2.Attributes.Add("Size");  
    pdim2.Attributes.Add("Weight");  
    pdim2.Hierarchies.Add("Product Model Categories");  
    pdim2.Hierarchies.Add("Product Categories");  
  
    PerspectiveMeasureGroup pmg = perspective.MeasureGroups.Add("Internet Sales");  
    pmg.Measures.Add("Internet Sales Amount");  
    pmg.Measures.Add("Internet Order Quantity");  
    pmg.Measures.Add("Internet Unit Price");  
  
    pmg = perspective.MeasureGroups.Add("Reseller Sales");  
    pmg.Measures.Add("Reseler Sales Amount");  
    pmg.Measures.Add("Reseller Order Quantity");  
    pmg.Measures.Add("Reseller Unit Price");  
  
    PerspectiveAction pact = perspective.Actions.Add("Drillthrough Action");  
  
    PerspectiveKpi pkpi = perspective.Kpis.Add("Internet Revenue");  
    Cube.Update();  
}  

ProactiveCaching 对象

ProactiveCaching 对象可由 AMO 定义。

ProactiveCaching创建对象需要以下步骤:

  1. ProactiveCaching创建对象。

    没有要定义的基本属性。

  2. 添加缓存规范。

规格 Description
聚合存储 聚合的存储类型。

仅适用于分区。 在维度上,它必须是 常规。
静音间隔 (SilenceInterval) MOLAP 映像启动前缓存存在的时间下限。
延迟 最早通知与销毁 MOLAP 映像之间的时间间隔。
SilenceOverrideInterval 初始通知后的时间段,MOLAP 图像处理过程无条件启动。
ForceRebuildInterval 时间(在删除新的 MOLAP 图像后开始)之后,MOLAP 映像会无条件启动(无通知)。
在线模式 MOLAP 映像可用时。

可以是 即时 或 OnCacheComplete。
  1. 将 ProactiveCaching 对象添加到父集合。 需要更新父级,因为 ProactiveCaching 不是可更新的对象。

在指定的数据库中,下面的代码示例在 Adventure Works 多维数据集的 Internet Sales 度量值组的所有分区中创建一个 ProactiveCaching 对象。

static public void SetProactiveCachingSettings(Database db)  
{  
    ProactiveCaching pc;  
    if (db.Cubes.ContainsName("Adventure Works") && db.Cubes.FindByName("Adventure Works").MeasureGroups.ContainsName("Internet Sales"))  
    {  
        ProactiveCachingTablesBinding pctb;  
        TableNotification tn;  
  
        MeasureGroup mg = db.Cubes.FindByName("Adventure Works").MeasureGroups.FindByName("Internet Sales");  
        foreach(Partition part in mg.Partitions)  
        {  
            pc = new ProactiveCaching();  
            pc.AggregationStorage = ProactiveCachingAggregationStorage.MolapOnly;  
            pc.SilenceInterval = TimeSpan.FromSeconds(10);  
            pc.Latency = TimeSpan.FromSeconds(-1);  
            pc.SilenceOverrideInterval = TimeSpan.FromMinutes(10);  
            pc.ForceRebuildInterval = TimeSpan.FromSeconds(-1);  
            pc.Enabled = true;  
            pc.OnlineMode = ProactiveCachingOnlineMode.OnCacheComplete;  
            pctb = new ProactiveCachingTablesBinding();  
            pctb.NotificationTechnique = NotificationTechnique.Server;  
            tn = new TableNotification("[FactInternetSales]", "dbo");  
            pctb.TableNotifications.Add( tn);  
            pc.Source = pctb;  
  
            part.ProactiveCaching = pc;  
            part.Update();  
        }  
    }  
}  

翻译对象

AMO 可以定义翻译对象,但这些对象是在用于浏览数据的客户端应用程序中使用的。 翻译对象是代码的简单对象。 对象说明的翻译由区域标识符和翻译说明的对提供。 对于任何字幕,可以启用多个翻译。 可为大多数对象提供翻译,例如维度、属性、层次结构、多维数据集、度量值组、度量值和其他对象。

下面的代码示例提供属性产品名称名称的西班牙语翻译。

static public void CreateTranslations(Database db)  
{  
    //Spanish Tranlations for Product Category in Product Dimension  
    Dimension dim = db.Dimensions["Product"];  
    DimensionAttribute atr = dim.Attributes["Product Name"];  
    Translation tran = atr.Translations.Add(3082);  
    tran.Caption = "Nombre Producto";  
  
    dim.Update(UpdateOptions.ExpandFull);  
  
}