检索多维数据库对象目录

Yah*_*ufi 7 c# sql-server olap ssas ssis

我正在尝试列出在SSAS服务器中找到的所有对象(多维数据集,维度,分区等)。我可以使用以下项目来做到这一点:

我正在尝试为每个对象检索相关目录(在数据目录内)。我无法执行此操作,因为文件名包含一些递增数字,每次您对数据库中的对象进行更改时,这些数字都会更改。例:

  • 多维数据集名称:TestCube

夹:

|Data Dir|\<SSASDB>\TestCube.0.cub
Run Code Online (Sandbox Code Playgroud)

更改并重新处理多维数据集后,它将更改为另一个值

|Data Dir|\<SSASDB>\TestCube.1.cub
Run Code Online (Sandbox Code Playgroud)

AMO类中是否有一个属性可以返回每个对象的文件夹路径?文件夹名称中包含的增量编号是什么值?有解决方法吗?


由于我只安装了SQL Server数据工具商业智能工具,因此我需要一个与SSIS脚本Task兼容的解决方案,因为这是我处理数据的唯一方法。请注意,在线上有许多文章可用于通过脚本任务使用AMO

环境: SQL Server 2014

Had*_*adi 9

Since you mentioned a cube as example, it means that you are working with multidimensional model not tabular.

SSAS Data directory contents hierarchy

After building many multidimensional Cube using SSAS, i can assume that the following tree is the Data directory hierarchy:

|- Data Directory
    |- Database (.db)
        |- Dimension (.dim)
        |- Role (.role)
        |- Mining Structure (.dms)
        |- Data Source (.ds)
        |- Data Source View (.dsv)
        |- Multidimensional Cube (.cub)
            |- Measure Group (.det)
                |- Partition (.prt)
                |- AggregationDesign (.agg)
Run Code Online (Sandbox Code Playgroud)

Each object from the tree above can be stored in form of a directory or/and an XML file.

The Actions and Kpis information are stored within the Cube XML configuration file.

Example:

  • Object: Cube
  • Directory: <DataDir>\<database>\<cube ID>.cub\
  • XML file: <DataDir>\<database>\<cube ID>.cub.xml

Link SSAS AMO objects to data directory files

Reading data using AMO

To read SSAS object from deployed Analysis Cube, i improved the code of the following project to add more objects and to link them with the relevant directories/files.

Updated method

To map every AMO object to the relevant directory/XML file we have to loop over objects starting from top level (database) and retrieve the files/directories found in each level and map it using the .ID property and the extensions (as mentioned in the tree above)

The following code is written in C# and it is an updated version of the method published in the link above:

Note that the method only works on local servers or you must have a Mapped network drive with the same letter of the Original drive that contains the data directory. In addition you must have the permission to access the Analysis Server objects

The code is considered as a proof of concept, and can be improved

SSASObject Class

public class SSASObject
{

    public enum ObjectType{

        Cube = 0,
        MeasureGroup = 1,
        Dimension = 2,
        Partition = 3,
        AggregationDesign = 4,
        MiningStructure = 5,
        Role = 6,
        DataSource = 7,
        DataSourceView = 8,
        Database = 9,
        Server = 10,
        Kpi = 11,
        Action = 12

    }

    public int ID { get; set; } //incremental ID 
    public int? ParentID { get; set; } // Parent incremental ID
    public ObjectType Type { get; set; } // The Object type
    public string ObjectID { get; set; } // Object ID defined in SSAS
    public string ObjectName { get; set; } // Object Name defined in SSAS
    public string Extension { get; set; } // The Object extension
    public string FolderPath { get; set; } // The Object related directory
    public string FolderName { get; set; } // The directory name
    public DateTime? FolderModifiedDate { get; set; } // The directory last modified date
    public string FolderIncremetalID { get; set; } // The Incremental Number mentioned in the directory name
    public string XMLFilePath { get; set; } // The Object related XML file
    public string XMLFileName { get; set; } // The XML file name
    public DateTime? XmlModifiedDate { get; set; } // The XML file last modified date
    public string XmlIncremetalID { get; set; }  // The incremental number mentioned in the XML file name      


}
Run Code Online (Sandbox Code Playgroud)

SSASAMO Class

public static class SSASAMO
{
    public static List<SSASObject> ReadMeta(string ServerName)
    {
        try
        {


            List<SSASObject> result = new List<SSASObject>();

            String ConnStr;

            DateTime? dt = null;
            int idx = 0;
            int DbID = 0;
            int CubeID = 0;
            int ObjectID = 0;

            string DataDir;
            string OLAPServerName = ServerName;

            ConnStr = "Provider=MSOLAP;Data Source=" + OLAPServerName + ";";

            Server OLAPServer = new Server();
            OLAPServer.Connect(ConnStr);

            DataDir = OLAPServer.ServerProperties["DataDir"].Value;

            string[] DatabasesDir = System.IO.Directory.GetDirectories(DataDir, "*", System.IO.SearchOption.TopDirectoryOnly);
            string[] DatabasesFiles = System.IO.Directory.GetFiles(DataDir, "*", System.IO.SearchOption.TopDirectoryOnly);

            result.Add(new SSASObject
            {
                ID = idx,
                ParentID = null,
                FolderModifiedDate = System.IO.Directory.GetLastWriteTime(DataDir),
                FolderPath = DataDir,
                ObjectName = OLAPServerName,
                Type = SSASObject.ObjectType.Server
            });

            // Database
            foreach (Database OLAPDatabase in OLAPServer.Databases)
            {


                string CurrentDbDir = DatabasesDir.Where(x => x.StartsWith(DataDir + "\\" +  OLAPDatabase.ID.ToString() + ".") && x.EndsWith(".db")).DefaultIfEmpty("").First();
                string CurrentDbXmlFile = DatabasesFiles.Where(x => x.StartsWith(DataDir + "\\" + OLAPDatabase.ID.ToString() + ".") && x.EndsWith(".db.xml")).DefaultIfEmpty("").First();

                string[] DbObjectsDir = System.IO.Directory.GetDirectories(CurrentDbDir, "*", System.IO.SearchOption.TopDirectoryOnly);
                string[] DbObjectsFiles = System.IO.Directory.GetFiles(CurrentDbDir, "*", System.IO.SearchOption.TopDirectoryOnly);


                idx++;
                DbID = idx;
                result.Add(new SSASObject
                {
                    ID = idx,
                    ParentID = 0,
                    ObjectID = OLAPDatabase.ID,
                    FolderModifiedDate = CurrentDbDir == "" ? dt : System.IO.Directory.GetLastWriteTime(CurrentDbDir),
                    XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                  System.IO.Path.GetFileNameWithoutExtension(CurrentDbXmlFile)).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(CurrentDbXmlFile).IndexOf(".") + 1),
                    Extension = ".db",
                    FolderName = System.IO.Path.GetFileName(CurrentDbDir),
                    FolderPath = CurrentDbDir,
                    ObjectName = OLAPDatabase.Name,
                    Type = SSASObject.ObjectType.Database,
                    XMLFileName = System.IO.Path.GetFileName(CurrentDbXmlFile),
                    XMLFilePath = CurrentDbXmlFile,
                    XmlModifiedDate = CurrentDbXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(CurrentDbXmlFile),
                    FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(CurrentDbDir).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(CurrentDbDir).IndexOf(".") + 1)
                });



                //Data Source
                foreach (DataSource OLAPDataSource in OLAPDatabase.DataSources)
                {
                    idx++;
                    string CurrentDataSourceDir = DbObjectsDir.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDataSource.ID.ToString() + ".") && x.EndsWith(".ds")).DefaultIfEmpty("").First();
                    string CurrentDataSourceXmlFile = DbObjectsFiles.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDataSource.ID.ToString() + ".") && x.EndsWith(".ds.xml")).DefaultIfEmpty("").First();
                    result.Add(new SSASObject
                    {
                        ID = idx,
                        ParentID = DbID,
                        ObjectID = OLAPDataSource.ID,
                        FolderModifiedDate = CurrentDataSourceDir == "" ? dt : System.IO.Directory.GetLastWriteTime(CurrentDataSourceDir),
                        XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceXmlFile)).Substring(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceXmlFile).IndexOf(".") + 1),
                        Extension = ".ds",
                        FolderName = System.IO.Path.GetFileName(CurrentDataSourceDir),
                        FolderPath = CurrentDbDir,
                        ObjectName = OLAPDataSource.Name,
                        Type = SSASObject.ObjectType.DataSource,
                        XMLFileName = System.IO.Path.GetFileName(CurrentDataSourceXmlFile),
                        XMLFilePath = CurrentDataSourceXmlFile,
                        XmlModifiedDate = CurrentDataSourceXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(CurrentDataSourceXmlFile),
                        FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceDir).Substring(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceDir).IndexOf(".") + 1)
                    });


                }

                //Data Source View
                foreach (DataSourceView OLAPDataSourceView in OLAPDatabase.DataSourceViews)
                {
                    idx++;
                    string CurrentDataSourceViewDir = DbObjectsDir.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDataSourceView.ID.ToString() + ".") && x.EndsWith(".dsv")).DefaultIfEmpty("").First();
                    string CurrentDataSourceViewXmlFile = DbObjectsFiles.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDataSourceView.ID.ToString() + ".") && x.EndsWith(".dsv.xml")).DefaultIfEmpty("").First();
                    result.Add(new SSASObject
                    {
                        ID = idx,
                        ParentID = DbID,
                        ObjectID = OLAPDataSourceView.ID,
                        FolderModifiedDate = CurrentDataSourceViewDir == "" ? dt : System.IO.Directory.GetLastWriteTime(CurrentDataSourceViewDir),
                        XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceViewXmlFile)).Substring(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceViewXmlFile).IndexOf(".") + 1),
                        Extension = ".dsv",
                        FolderName = System.IO.Path.GetFileName(CurrentDataSourceViewDir),
                        FolderPath = CurrentDbDir,
                        ObjectName = OLAPDataSourceView.Name,
                        Type = SSASObject.ObjectType.DataSourceView,
                        XMLFileName = System.IO.Path.GetFileName(CurrentDataSourceViewXmlFile),
                        XMLFilePath = CurrentDataSourceViewXmlFile,
                        XmlModifiedDate = CurrentDataSourceViewXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(CurrentDataSourceViewXmlFile),
                        FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceViewDir).Substring(
                                                      System.IO.Path.GetFileNameWithoutExtension(CurrentDataSourceViewDir).IndexOf(".") + 1)
                    });


                }


                //Dimension
                foreach (Dimension OLAPDimension in OLAPDatabase.Dimensions)
                {

                    idx++;
                    string DimensionDir = DbObjectsDir.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDimension.ID.ToString() + ".") && x.EndsWith(".dim")).DefaultIfEmpty("").First();
                    string DimensionXmlFile = DbObjectsFiles.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPDimension.ID.ToString() + ".") && x.EndsWith(".dim.xml")).DefaultIfEmpty("").First();

                    result.Add(new SSASObject
                    {
                        ID = idx,
                        ParentID = DbID,
                        ObjectID = OLAPDimension.ID,
                        FolderModifiedDate = DimensionDir == "" ? dt : System.IO.Directory.GetLastWriteTime(DimensionDir),
                        XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                  System.IO.Path.GetFileNameWithoutExtension(DimensionXmlFile)).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(DimensionXmlFile).IndexOf(".") + 1),
                        Extension = ".dim",
                        FolderName = System.IO.Path.GetFileName(DimensionDir),
                        FolderPath = DimensionDir,
                        ObjectName = OLAPDimension.Name,
                        Type = SSASObject.ObjectType.Dimension,
                        XMLFileName = System.IO.Path.GetFileName(DimensionXmlFile),
                        XMLFilePath = DimensionXmlFile,
                        XmlModifiedDate = DimensionXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(DimensionXmlFile),
                        FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(DimensionDir).Substring(
                                             System.IO.Path.GetFileNameWithoutExtension(DimensionDir).IndexOf(".") + 1)
                    });
                }

                // Cube
                foreach (Cube OLAPCubex in OLAPDatabase.Cubes)
                {


                    idx++;
                    CubeID = idx;

                    string CubeDir = DbObjectsDir.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPCubex.ID.ToString() + ".") && x.EndsWith(".cub")).DefaultIfEmpty("").First();
                    string CubeXmlFile = DbObjectsFiles.Where(x => x.StartsWith(CurrentDbDir + "\\" + OLAPCubex.ID.ToString() + ".") && x.EndsWith(".cub.xml")).DefaultIfEmpty("").First();

                    string[] CubeMeasureGroupsDir = System.IO.Directory.GetDirectories(CubeDir, "*", System.IO.SearchOption.TopDirectoryOnly);
                    string[] CubeMeasureGroupsFiles = System.IO.Directory.GetFiles(CubeDir, "*", System.IO.SearchOption.TopDirectoryOnly);


                    result.Add(new SSASObject
                    {
                        ID = idx,
                        ParentID = DbID,
                        ObjectID = OLAPCubex.ID,
                        FolderModifiedDate = CubeDir == "" ? dt : System.IO.Directory.GetLastWriteTime(CubeDir),
                        XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                  System.IO.Path.GetFileNameWithoutExtension(CubeXmlFile)).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(CubeXmlFile).IndexOf(".") + 1),
                        Extension = ".cub",
                        FolderName = System.IO.Path.GetFileName(CubeDir),
                        FolderPath = CubeDir,
                        ObjectName = OLAPCubex.Name,
                        Type = SSASObject.ObjectType.Cube,
                        XMLFileName = System.IO.Path.GetFileName(CubeXmlFile),
                        XMLFilePath = CubeXmlFile,
                        XmlModifiedDate = CubeXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(CubeXmlFile),
                        FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(CubeDir).Substring(
                                             System.IO.Path.GetFileNameWithoutExtension(CubeDir).IndexOf(".") + 1)
                    });

                    //Measure Group
                    foreach (MeasureGroup OLAPMeasureGroup in OLAPCubex.MeasureGroups)
                    {

                        idx++;
                        ObjectID = idx;
                        string MeasureGroupDir = CubeMeasureGroupsDir.Where(x => x.StartsWith(CubeDir + "\\" + OLAPMeasureGroup.ID.ToString() + ".") && x.EndsWith(".det")).DefaultIfEmpty("").First();
                        string MeasureGroupXmlFile = CubeMeasureGroupsFiles.Where(x => x.StartsWith(CubeDir + "\\" + OLAPMeasureGroup.ID.ToString() + ".") && x.EndsWith(".det.xml")).DefaultIfEmpty("").First();

                        string[] GroupPartitionDir = System.IO.Directory.GetDirectories(MeasureGroupDir, "*", System.IO.SearchOption.TopDirectoryOnly);
                        string[] GroupPartitionFiles = System.IO.Directory.GetFiles(MeasureGroupDir, "*", System.IO.SearchOption.TopDirectoryOnly);

                        result.Add(new SSASObject
                        {
                            ID = idx,
                            ParentID = CubeID,
                            ObjectID = OLAPMeasureGroup.ID,
                            FolderModifiedDate = MeasureGroupDir == "" ? dt : System.IO.Directory.GetLastWriteTime(MeasureGroupDir),
                            XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                  System.IO.Path.GetFileNameWithoutExtension(MeasureGroupXmlFile)).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(MeasureGroupXmlFile).IndexOf(".") + 1),
                            Extension = ".det",
                            FolderName = System.IO.Path.GetFileName(MeasureGroupDir),
                            FolderPath = MeasureGroupDir,
                            ObjectName = OLAPMeasureGroup.Name,
                            Type = SSASObject.ObjectType.MeasureGroup,
                            XMLFileName = System.IO.Path.GetFileName(MeasureGroupXmlFile),
                            XMLFilePath = MeasureGroupXmlFile,
                            XmlModifiedDate = MeasureGroupXmlFile == "" ? dt : System.IO.File.GetLastWriteTime(MeasureGroupXmlFile),
                            FolderIncremetalID = System.IO.Path.GetFileNameWithoutExtension(MeasureGroupDir).Substring(
                                                 System.IO.Path.GetFileNameWithoutExtension(MeasureGroupDir).IndexOf(".") + 1)
                        });

                        //Aggregations
                        foreach (AggregationDesign OLAPAggregationDesign in OLAPMeasureGroup.AggregationDesigns)
                        {

                            string AggregationDir = GroupPartitionDir.Where(x => x.StartsWith(MeasureGroupDir + "\\" + OLAPAggregationDesign.ID.ToString() + ".") && x.EndsWith(".agg")).DefaultIfEmpty("").First();
                            string AggregationXmlFile = GroupPartitionFiles.Where(x => x.StartsWith(MeasureGroupDir + "\\" + OLAPAggregationDesign.ID.ToString() + ".") && x.EndsWith(".agg.xml")).DefaultIfEmpty("").First();

                            idx++;

                            result.Add(new SSASObject
                            {
                                ID = idx,
                                ParentID = ObjectID,
                                ObjectID = OLAPAggregationDesign.ID,
                                FolderModifiedDate = AggregationDir == "" ? dt : System.IO.Directory.GetLastWriteTime(AggregationDir),
                                XmlIncremetalID = System.IO.Path.GetFileNameWithoutExtension(
                                                  System.IO.Path.GetFileNameWithoutExtension(AggregationXmlFile)).Substring(
                                                  System.IO.Path.GetFileNameWithoutExtension(AggregationXmlFile).IndexOf(".") + 1),
                                

  • 请我们需要github链接 (2认同)