在表格式模型中建立數據表、數據分割和數據行

適用於: SQL Server 2016 和更新版本的 Analysis Services Azure Analysis Services Fabric/Power BI Premium

在表格式模型中,數據表是由數據列和數據行所組成。 數據列會組織成數據分割,以支援累加式數據重新整理。 表格式解決方案可以支援數種類型的數據表,視數據來自何處而定:

  • 一般數據表,其中數據來自關係型數據源,透過數據提供者。

  • 推送的數據表,其中數據會以程序設計方式「推送」至數據表。

  • 匯出數據表,其中數據來自 DAX 運算式,該表達式會參考模型內另一個對象的數據。

在下列程式代碼範例中,我們將定義一般數據表。

必要元素

數據表必須至少有一個數據分割。 一般數據表也必須至少定義一個數據行。

每個分割區都必須有指定數據來源的來源,但source可以設定為 null。 一般而言,來源是查詢表達式,定義相關資料庫查詢語言中的數據配量。

程式代碼範例:建立數據表、數據行、數據分割

數據表是以 Table 類別表示(在 Microsoft.AnalysisServices.Tabular 命名空間中)。

在下列範例中,我們將定義一般數據表,其中一個分割區連結至關係型數據源和一些一般數據行。 我們也會將變更提交至伺服器,並觸發將數據帶入模型的數據重新整理。 當您想要將數據從 SQL Server 關係資料庫載入表格式解決方案時,這代表最典型的案例。

using System; 
using Microsoft.AnalysisServices; 
using Microsoft.AnalysisServices.Tabular; 
 
namespace TOMSamples 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            // 
            // Connect to the local default instance of Analysis Services 
            // 
            string ConnectionString = "DataSource=localhost"; 
 
            // 
            // The using syntax ensures the correct use of the 
            // Microsoft.AnalysisServices.Tabular.Server object. 
            // 
            using (Server server = new Server()) 
            { 
                server.Connect(ConnectionString); 
 
                // 
                // Generate a new database name and use GetNewName 
                // to ensure the database name is unique. 
                // 
                string newDatabaseName = 
                    server.Databases.GetNewName("Database with a Table Definition"); 
 
                // 
                // Instantiate a new  
                // Microsoft.AnalysisServices.Tabular.Database object. 
                // 
                var dbWithTable = new Database() 
                { 
                    Name = newDatabaseName, 
                    ID = newDatabaseName, 
                    CompatibilityLevel = 1200, 
                    StorageEngineUsed = StorageEngineUsed.TabularMetadata, 
                }; 
 
                // 
                // Add a Microsoft.AnalysisServices.Tabular.Model object to the 
                // database, which acts as a root for all other Tabular metadata objects. 
                // 
                dbWithTable.Model = new Model() 
                { 
                    Name = "Tabular Data Model", 
                    Description = "A Tabular data model at the 1200 compatibility level." 
                }; 
 
                // 
                // Add a Microsoft.AnalysisServices.Tabular.ProviderDataSource object 
                // to the data Model object created in the previous step. The connection 
                // string of the data source object in this example  
                // points to an instance of the AdventureWorks2014 SQL Server database. 
                // 
                string dataSourceName = "SQL Server Data Source Example"; 
                dbWithTable.Model.DataSources.Add(new ProviderDataSource() 
                { 
                    Name = dataSourceName, 
                    Description = "A data source definition that uses explicit Windows credentials for authentication against SQL Server.", 
                    ConnectionString = "Provider=SQLNCLI11;Data Source=localhost;Initial Catalog=AdventureWorks2014;Integrated Security=SSPI;Persist Security Info=false", 
                    ImpersonationMode = Microsoft.AnalysisServices.Tabular.ImpersonationMode.ImpersonateAccount, 
                    Account = @".\Administrator", 
                    Password = "P@ssw0rd", 
                }); 
 
                //  
                // Add a table called Individual Customers to the data model 
                // with a partition that points to a [Sales].[vIndividualCustomer] view 
                // in the underlying data source. 
                // 
                dbWithTable.Model.Tables.Add(new Table() 
                { 
                    Name = dbWithTable.Model.Tables.GetNewName("Individual Customers"), 
                    Description = "Individual customers (names and email addresses) that purchase Adventure Works Cycles products online.", 
                    Partitions = { 
                        // 
                        // Create a single partition with a QueryPartitionSource for a query 
                        // that imports all customer rows from the underlying data source. 
                        // 
                        new Partition() { 
                            Name = "All Customers", 
                            Source = new QueryPartitionSource() { 
                                DataSource = dbWithTable.Model.DataSources[dataSourceName], 
                                Query = @"SELECT   [FirstName] 
                                                    ,[MiddleName] 
                                                    ,[LastName] 
                                                    ,[PhoneNumber]  
                                                    ,[EmailAddress] 
                                                    ,[City] 
                                        FROM [Sales].[vIndividualCustomer]", 
                            } 
                        } 
                    }, 
                    Columns = 
                    { 
                        // 
                       // DataColumn objects refer to regular table columns.  
                        // Each DataColumn object corresponds to a column in the underlying data source query. 
                        // 
                        new DataColumn() { 
                            Name = "FirstName", 
                            DataType = DataType.String, 
                            SourceColumn = "FirstName", 
                        }, 
                        new DataColumn() { 
                            Name = "MiddleName", 
                            DataType = DataType.String, 
                            SourceColumn = "MiddleName", 
                        }, 
                        new DataColumn() { 
                            Name = "LastName", 
                            DataType = DataType.String, 
                            SourceColumn = "LastName", 
                        }, 
                        new DataColumn() { 
                            Name = "PhoneNumber", 
                            DataType = DataType.String, 
                            SourceColumn = "PhoneNumber", 
                        }, 
                        new DataColumn() { 
                            Name = "EmailAddress", 
                            DataType = DataType.String, 
                            SourceColumn = "EmailAddress", 
                        }, 
                        new DataColumn() { 
                            Name = "City", 
                            DataType = DataType.String, 
                            SourceColumn = "City", 
                        }, 
                    } 
                }); 
 
                // 
                // Add the new database object to the server's  
                // Databases connection and submit the changes 
                // with full expansion to the server. 
                // 
                server.Databases.Add(dbWithTable); 
 
                //  
                // Request a full refresh to import the data from the data source and 
                // and perform any necessary recalculations. 
                // The refresh operation will be performed with the next 
                // invocation of Model.SaveChanges() or Database.Update(UpdateOptions.ExpandFull). 
                dbWithTable.Model.RequestRefresh(Microsoft.AnalysisServices.Tabular.RefreshType.Full); 
                dbWithTable.Update(UpdateOptions.ExpandFull); 
 
 
                Console.Write("Database "); 
                Console.ForegroundColor = ConsoleColor.Yellow; 
                Console.Write(dbWithTable.Name); 
                Console.ResetColor(); 
                Console.WriteLine(" created successfully."); 
 
                Console.WriteLine("The data model includes the following table definitions:"); 
                Console.ForegroundColor = ConsoleColor.Yellow; 
                foreach (Table tbl in dbWithTable.Model.Tables) 
                { 
                    Console.WriteLine("\tTable name:\t\t{0}", tbl.Name); 
                    Console.WriteLine("\ttbl description:\t{0}", tbl.Description); 
                } 
                Console.ResetColor(); 
                Console.WriteLine(); 
            } 
            Console.WriteLine("Press Enter to close this console window."); 
            Console.ReadLine(); 
        } 
    } 
} 

數據表中的數據分割

分割區是由 Partition 類別表示(在 Microsoft.AnalysisServices.Tabular 命名空間中)。 Partition 類別會公開 PartitionSource 類型的 Source 属性,提供將數據內嵌至分割區的不同方法的抽象概念。 數據分割 實例可以具有 Source 屬性做為 null,表示數據會藉由將數據區塊傳送至伺服器,作為 Analysis Services 所公開的數據 API 一部分,將數據推送至分割區。 在 SQL Server 2016 中,PartitionSource 類別有兩個衍生類別,代表將數據系結至數據分割的方法:QueryPartitionSourceCalculatedPartitionSource

數據表中的數據行

數據行是由數個衍生自基底 Column 類別的類別來表示(在 Microsoft.AnalysisServices.Tabular 命名空間中):

  • DataColumn (適用於一般數據表中的一般數據行)
  • CalculatedColumn (適用於 DAX 表達式支持的數據行)
  • CalculatedTableColumn (適用於匯出數據表中的一般數據行)
  • RowNumberColumn (SSAS 為每個數據表建立的特殊數據行類型)。

數據表中的數據列編號

伺服器上每個 Table 物件都有用於編製索引的 RowNumberColumn。 您無法明確建立或新增它。 當您儲存或更新物件時,會自動建立資料列:

  • db。SaveChanges

  • db。Update(ExpandFull)

呼叫任一方法時,伺服器會自動建立數據列號碼數據行,這會顯示為 RowNumberColumn 數據表的 Columns 集合。

匯出數據表

匯出數據表的來源是DAX表達式,該表達式會從模型中現有數據結構或離線系結重新建立用途的數據。 若要以程式設計方式建立匯出數據表,請執行下列動作:

  • 建立一般 資料表

  • 使用 CalculatedPartitionSource 類型的 Source,將分割區新增至其中,其中來源是 DAX 表達式。 分割區的來源是將一般數據表與導出數據表區別開來。

當您將變更儲存到伺服器時,伺服器會傳回 CalculatedTableColumns 的推斷清單(計算數據表是由導出數據表數據行所組成),可透過數據表的 Columns 集合顯示。

下一步

檢閱用於處理 TOM 中例外狀況的類別:處理 TOM 中的錯誤