Rekursion zum Auslesen der Metadaten-Verzeichnisstruktur

Aus BI-Snippets - Business Intelligence Code und Module
Wechseln zu: Navigation, Suche

Das folgende Makro read_MDFolderstructure ließt ausgehend von einem via Metadaten-ID übergebenen Ordner, die darunterliegende Verzeichnisstruktur rekursiv aus und gibt diese in einem Dataset zurück. Das Output-Dataset wird zweistufig (LIBRARY.DATASETNAME) über die Markrovariable _OUTPUT belegt.


<syntaxhighlight lang="SAS" line="1" >

/********************* Standard Programm Header *****************************************
* Programm     : read_MDFolderstructure  
* Beschreibung : Das Makro read_MDFolderstructure ließt ausgehend von einem via Metadaten-ID übergebenen Ordner, 
*                die darunterliegende Verzeichnisstruktur rekursiv aus und gibt diese in einem Dataset zurück.  
*
* Ersteller    : Christopher Gies | accantec consulting AG
* Datum        : 22.01.15
*
* Parameter    :  _tree_uri       = Metadaten-ID des Einstiegsverzeichnisses
*                                   FORMAT: A50SK7XC.AJ0009SG
*                 _tree_name      = nicht belegen! Dieser Parameter wird beim rekursiven Aufruf verwendet
*                                   um den Verzeichnispfad zu konkatenieren
*                                   FORMAT: 
*
***************************** Änderungen ************************************************
*
*  Version    Datum      Autor   Beschreibung der Änderung
*
*  V0.1       22.01.15   ChG     Initale Erstellung 
*
****************************************************************************************/

options cmplib=work.funcs;

/****************************************************************/
/*           Verbindungsdaten zum MD-Server                     */
/****************************************************************/
options metaserver="MY_METADATA_SERVER"
metaport=8561
metaprotocol=bridge
metarepository="MY_METADATA_REPOSITORY";
/****************************************************************/
/*                  LIBRARY intitalisieren                      */
/****************************************************************/
%macro initLib;
 %if %sysfunc(exist(&_OUTPUT.)) %then %do;
proc datasets lib=%sysfunc(scan(&_OUTPUT.,1,'.')) nolist;
  delete %sysfunc(scan(&_OUTPUT.,2,'.'));
quit;
run;
 %end;
%mend initLib;
%initLib;

%macro append_ds;
 %let dsname = %sysfunc(dequote(&dsname.));
data &dsname.;
 %if %sysfunc(exist(&dsname.)) = 0 %then %do;
  length  tree_uri $256
          tree_name $256
          next_member_uri $256
          member_name $256
          member_type $256
          rc 8
          anz_reports 8
          anz_subfolders 8
          s 8;
 %end;
 %if %sysfunc(exist(&dsname.)) = 1 %then %do;
   modify &dsname.;
 %end;
tree_uri = strip(&tree_uri.);
tree_name = strip(&tree_name.);
next_member_uri = strip(&next_member_uri.);
member_name = strip(&member_name.);
member_type = strip(&member_type.);
rc = strip(&rc.);
anz_reports = strip(&anz_reports.);
anz_subfolders = strip(&anz_subfolders.);
s = strip(&s.);
output;
stop;
run;
%mend append_ds;

/* Call the APPEND_DS macro from function writeDataset in PROC FCMP. */
proc fcmp outlib = work.funcs.ChG;
function writeDataset (dsname $, tree_uri $, tree_name $, next_member_uri $, 
                      member_name $ , member_type $, rc, anz_reports, anz_subfolders, s);
  rc_wd = run_macro('append_ds', dsname, tree_uri, tree_name, next_member_uri, 
                    member_name, member_type, rc, anz_reports, anz_subfolders, s);
  return(rc_wd);
endsub;
run;

%macro read_MDFolderstructure(_tree_uri=, _tree_name=);
 %let _tree_uri_part = %substr(&_tree_uri.,10,8);

data _null_;

length  tree_uri $256
        tree_name $256
        uri $256
        next_member_uri $256
        next_member_uri_part $256
        member_name $256
        member_type $256
        nobj 8;

drop uri nobj;
nobj = metadata_getnobj("OMSOBJ:Tree\&_tree_uri." , 1, tree_uri);
put tree_uri=;

if nobj gt 0 then do; 
  /* get the tree name */ 
  rc = metadata_getattr(tree_uri, 'Name', tree_name);
  tree_name=catx('/',"&_tree_name.",tree_name);
  put tree_name=;
  /* get the members of this tree*/
  anz_reports = metadata_getnasn(tree_uri, 'Members', 1, uri);
  put anz_reports=;
  /* get the subtree uri */ 
  anz_subfolders = metadata_getnasn(tree_uri, 'SubTrees', 1, uri);
  put anz_subfolders=;

          
  do s = 1 to anz_subfolders; 
    /* get details of each member*/                       
    rc = metadata_getnasn(tree_uri, 'SubTrees', s, next_member_uri);
    rc = metadata_getattr(next_member_uri, 'Name', member_name);

    /* get Member_Type from the Metadata-Object (Job, Subfolder, Table etc.) */
    rc = metadata_getattr(next_member_uri, 'PublicType', member_type);
    next_member_uri = scan(next_member_uri,2,'\');
    member_type = trim(member_type);

    rc_wd = writeDataset("&_OUTPUT.", tree_uri, tree_name, next_member_uri, 
                         member_name, member_type, rc, anz_reports, anz_subfolders, s);
    output;
    call execute('%read_MDFolderstructure(_tree_uri=' || next_member_uri || ', _tree_name=' || tree_name || ')');
  end;  

  do s = 1 to anz_reports; 
    /* get details of each member*/                       
    rc = metadata_getnasn(tree_uri, 'Members', s, next_member_uri);
    rc = metadata_getattr(next_member_uri, 'Name', member_name);

    /* get Member_Type from the Metadata-Object (Job, Subfolder, Table etc.) */
    rc = metadata_getattr(next_member_uri, 'PublicType', member_type);
    next_member_uri = scan(next_member_uri,2,'\');
    member_type = trim(member_type);

    rc_wd = writeDataset("&_OUTPUT.", tree_uri, tree_name, next_member_uri, 
                         member_name, member_type, rc, anz_reports, anz_subfolders, s);
    output;
  end; 
end;
run;

%mend read_MDFolderstructure;
%read_MDFolderstructure(_tree_uri=A50SK7XC.AJ0009SG, _tree_name=);

</syntaxhighlight>