比较语义模型横向扩展副本

本文提供了一些 Visual Studio 应用示例,用于在启用 Power BI 语义模型横向扩展时比较语义模型属性。

syncStatus REST API 显示读写语义模型和只读副本是否同步。你还可以使用表格对象模型 (TOM) 生成一个自定义应用程序,用于连接到这两个语义模型,并比较它们之间的时间戳、元数据和查询结果。

应用 1 - 检查数据库对象属性

使用以下代码生成一个应用,用于检查语义模型的 LastUpdateLastProcessedLastSchemaUpdate 属性。 应用在执行检查之前,需要调用 Refresh() 方法以获取副本的元数据。

<WorkspaceUrl> 替换为你的工作区的 URL,将 <Semantic modelName> 替换为你的语义模型的名称。

string workspaceUrl = "<WorkspaceUrl>";  // Replace <WorkspaceUrl> with the URL of your workspace
string datasetName = "<Semantic modelName>";  // Replace <Semantic modelName> with the name of your semantic model 
using (var workspace_readwrite = new Microsoft.AnalysisServices.Tabular.Server()) 
using (var workspace_readonly = new Microsoft.AnalysisServices.Tabular.Server()) 
{
    workspace_readwrite.Connect(workspaceUrl + "?readwrite"); 
    workspace_readonly.Connect(workspaceUrl + "?readonly"); 
    var datasetRW = workspace_readwrite.Databases.FindByName(semantic modelName); 
    var datasetRO = workspace_readonly.Databases.FindByName(semantic modelName); 

    if (datasetRW == null || datasetRO == null) 
    { 
        throw new ApplicationException("Database cannot be found!"); 
    }
    datasetRW.Refresh(); 
    datasetRO.Refresh(); 
    Console.WriteLine($"LastUpdated: {datasetRW.LastUpdate} (readwrite) {datasetRO.LastUpdate} (readonly)"); 
    Console.WriteLine($"LastProcessed: {datasetRW.LastProcessed} (readwrite) {datasetRO.LastProcessed} (readonly)"); 
    Console.WriteLine($"LastSchemaUpdate: {datasetRW.LastSchemaUpdate} (readwrite) {datasetRO.LastSchemaUpdate} (readonly)\n"); 
} 
Console.WriteLine("Test completed. Press any key to exit."); 
Console.Read(); 

应用 2 - 比较语义模型的元数据

使用以下代码将读/写主语义模型的元数据与只读副本的元数据进行比较。 将 <WorkspaceUrl> 替换为你的工作区的 URL,将 <DatasetName> 替换为你的语义模型的名称。

string workspaceUrl = "<WorkspaceUrl>";  // Replace <WorkspaceUrl> with the URL of your workspace 
string datasetName = "<DatasetName>";  // Replace <DatasetName> with the name of your semantic model 
using (var workspace_readwrite = new Microsoft.AnalysisServices.Tabular.Server()) 
using (var workspace_readonly = new Microsoft.AnalysisServices.Tabular.Server()) 
{ 
    workspace_readwrite.Connect(workspaceUrl + "?readwrite"); 
    workspace_readonly.Connect(workspaceUrl + "?readonly"); 
    var datasetRW = workspace_readwrite.Databases.FindByName(datasetName); 
    var datasetRO = workspace_readonly.Databases.FindByName(datasetName); 

    if (datasetRW == null || datasetRO == null) 
    { 
        throw new ApplicationException("Database cannot be found!"); 
    } 

    string tmslRW = Microsoft.AnalysisServices.Tabular.JsonSerializer.SerializeDatabase(datasetRW); 
    string tmslRO = Microsoft.AnalysisServices.Tabular.JsonSerializer.SerializeDatabase(datasetRO); 

    if (tmslRW != tmslRO) 
    { 
        Console.WriteLine("The replicas are out of sync.\n"); 
    } 
    else 
    { 
        Console.WriteLine("The replicas are in sync.\n"); 
    } 
} 
Console.WriteLine("Test completed. Press any key to exit."); 
Console.Read(); 

应用 3 - 查询语义模型数据

使用 ADOMD.NET 查询副本中的数据。 将 <WorkspaceUrl> 替换为你的工作区的 URL,将 <DatasetName> 替换为你的语义模型的名称。

string workspaceUrl = "<WorkspaceUrl>";  // Replace WorkspaceUrl with the URL of your workspace 
string datasetName = "<DatasetName>";  // Replace DatasetName with the name of your semantic model 
string daxQuery = "Evaluate SUMMARIZECOLUMNS(RefreshTimeTable[Time])"; 
using (var connectionRW = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection()) 
using (var connectionRO = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection()) 
{ 
    connectionRW.ConnectionString = $"Data Source={workspaceUrl}?readwrite;Catalog={datasetName}"; 
    connectionRO.ConnectionString = $"Data Source={workspaceUrl}?readonly;Catalog={datasetName}"; 
    connectionRW.Open(); 
    connectionRO.Open(); 
    var cmd = new Microsoft.AnalysisServices.AdomdClient.AdomdCommand(daxQuery); 
    string resultRW = string.Empty; 
    string resultRO = string.Empty; 
    cmd.Connection = connectionRW; 
    using (var reader = cmd.ExecuteReader()) 
    { 
        while (reader.Read()) 
        { 
            resultRW = reader.GetString(0); 
        } 
    } 

    cmd.Connection = connectionRO; 
    using (var reader = cmd.ExecuteReader()) 
    { 
        while (reader.Read()) 
        { 
            resultRO = reader.GetString(0); 
        } 
    } 

    if (resultRW != resultRO) 
    { 
        Console.WriteLine("The replicas are out of sync.\n"); 
    } 
    else 
    { 
        Console.WriteLine("The replicas are in sync.\n"); 
    } 
} 
Console.WriteLine("Test completed. Press any key to exit."); 
Console.Read();