Megosztás a következőn keresztül:


Adatok lekérdezése a MATLAB használatával

A MATLAB egy programozási és numerikus számítástechnikai platform, amellyel adatokat elemezhet, algoritmusokat fejleszthet és modelleket hozhat létre. Ez a cikk bemutatja, hogyan szerezhet be engedélyezési jogkivonatot a MATLAB for Azure Data Explorer-ben, és hogyan használhatja a tokent a fürttel való interakcióhoz.

Előfeltételek

Válassza ki a MATLAB futtatásához használt operációs rendszer lapfülét.

  1. Töltse le a Microsoft Identity Client és a Microsoft Identity Abstractions csomagokat a NuGetből.

  2. Bontsa ki a letöltött csomagokat és DLL-fájlokat a lib\net45-ből egy tetszőleges mappába. Ebben a cikkben a C:\Matlab\DLL mappát fogjuk használni.

Felhasználói hitelesítés végrehajtása

A felhasználó hitelesítésével a rendszer kérni fogja a felhasználót, hogy jelentkezzen be egy böngészőablakon keresztül. Sikeres bejelentkezés esetén a rendszer egy felhasználói engedélyezési jogkivonatot ad meg. Ez a szakasz bemutatja, hogyan konfigurálhatja ezt az interaktív bejelentkezési folyamatot.

Felhasználói hitelesítés végrehajtása:

  1. Adja meg az engedélyezéshez szükséges állandókat. További információ ezekről az értékekről: Hitelesítési paraméterek.

    % The Azure Data Explorer cluster URL
    clusterUrl = 'https://<adx-cluster>.kusto.windows.net';
    % The Azure AD tenant ID
    tenantId = '';
    % Send a request to https://<adx-cluster>.kusto.windows.net/v1/rest/auth/metadata
    % The appId should be the value of KustoClientAppId
    appId = '';
    % The Azure AD scopes
    scopesToUse = strcat(clusterUrl,'/.default ');
    
  2. A MATLAB studióban töltse be a kinyert DLL-fájlokat:

    % Access the folder that contains the DLL files
    dllFolder = fullfile("C:","Matlab","DLL");
    
    % Load the referenced assemblies in the MATLAB session
    matlabDllFiles = dir(fullfile(dllFolder,'*.dll'));
    for k = 1:length(matlabDllFiles)
        baseFileName = matlabDllFiles(k).name;
        fullFileName = fullfile(dllFolder,baseFileName);
        fprintf(1, 'Reading  %s\n', fullFileName);
    end
        % Load the downloaded assembly in MATLAB
        NET.addAssembly(fullFileName);
    
  3. A PublicClientApplicationBuilder használatával interaktív bejelentkezést kérhet a felhasználótól:

    % Create an PublicClientApplicationBuilder
    app = Microsoft.Identity.Client.PublicClientApplicationBuilder.Create(appId)...
        .WithAuthority(Microsoft.Identity.Client.AzureCloudInstance.AzurePublic,tenantId)...
        .WithRedirectUri('http://localhost:8675')...
        .Build();
    
    % System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    NET.setStaticProperty ('System.Net.ServicePointManager.SecurityProtocol',System.Net.SecurityProtocolType.Tls12)
    % Start with creating a list of scopes
    scopes = NET.createGeneric('System.Collections.Generic.List',{'System.String'});
    % Add the actual scopes
    scopes.Add(scopesToUse);
    fprintf(1, 'Using appScope  %s\n', scopesToUse);
    
    % Get the token from the service
    % and show the interactive dialog in which the user can login
    tokenAcquirer = app.AcquireTokenInteractive(scopes);
    result = tokenAcquirer.ExecuteAsync;
    
    % Extract the token and when it expires
    % and retrieve the returned token
    token = char(result.Result.AccessToken);
    fprintf(2, 'User token aquired and will expire at %s & extended expires at %s', result.Result.ExpiresOn.LocalDateTime.ToString,result.Result.ExtendedExpiresOn.ToLocalTime.ToString);
    
  4. Az engedélyezési jogkivonat használatával kérdezheti le a fürtöt a REST API-val:

    options=weboptions('HeaderFields',{'RequestMethod','POST';'Accept' 'application/json';'Authorization' ['Bearer ', token]; 'Content-Type' 'application/json; charset=utf-8'; 'Connection' 'Keep-Alive'; 'x-ms-app' 'Matlab'; 'x-ms-client-request-id' 'Matlab-Query-Request'});
    
    % The DB and KQL variables represent the database and query to execute
    querydata = struct('db', "<DB>", 'csl', "<KQL>");
    querryresults  = webwrite("https://sdktestcluster.westeurope.dev.kusto.windows.net/v2/rest/query", querydata, options);
    
    % Extract the results row
    results=querryresults{3}.Rows
    

Alkalmazáshitelesítés végrehajtása

Microsoft Entra alkalmazásengedélyezési lehetőség olyan esetekben használható, amikor nem kívánatos az interaktív bejelentkezés, és automatizált futtatásokra van szükség.

Alkalmazáshitelesítés végrehajtása:

  1. Microsoft Entra alkalmazás kiépítése. Az Átirányítási URI esetében válassza a Web lehetőséget, és adja meg a bemenetet http://localhost:8675 URI-ként.

  2. Adja meg az engedélyezéshez szükséges állandókat. További információ ezekről az értékekről: Hitelesítési paraméterek.

    % The Azure Data Explorer cluster URL
    clusterUrl = 'https://<adx-cluster>.kusto.windows.net';
    % The Azure AD tenant ID
    tenantId = '';
    % The Azure AD application ID and key
    appId = '';
    appSecret = '';
    
  3. A MATLAB studióban töltse be a kinyert DLL-fájlokat:

     % Access the folder that contains the DLL files
     dllFolder = fullfile("C:","Matlab","DLL");
    
     % Load the referenced assemblies in the MATLAB session
     matlabDllFiles = dir(fullfile(dllFolder,'*.dll'));
     for k = 1:length(matlabDllFiles)
         baseFileName = matlabDllFiles(k).name;
         fullFileName = fullfile(dllFolder,baseFileName);
         fprintf(1, 'Reading  %s\n', fullFileName);
     end
         % Load the downloaded assembly
         NET.addAssembly(fullFileName);
    
  4. A ConfidentialClientApplicationBuilder használatával nem interaktív, automatizált bejelentkezést végezhet az Microsoft Entra alkalmazással:

    %  Create an ConfidentialClientApplicationBuilder
    app = Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Create(appId)...
        .WithAuthority(Microsoft.Identity.Client.AzureCloudInstance.AzurePublic,tenantId)...
        .WithRedirectUri('http://localhost:8675')...
        .WithClientSecret(appSecret)...
        .Build();
    
    % System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    NET.setStaticProperty ('System.Net.ServicePointManager.SecurityProtocol',System.Net.SecurityProtocolType.Tls12)
    % Start with creating a list of scopes
    scopes = NET.createGeneric('System.Collections.Generic.List',{'System.String'});
    % Add the actual scopes
    scopes.Add(scopesToUse);
    fprintf(1, 'Using appScope  %s\n', scopesToUse);
    
    % Get the token from the service and cache it until it expires
    tokenAcquirer = app.AcquireTokenForClient(scopes);
    result = tokenAcquirer.ExecuteAsync;
    
    % Extract the token and when it expires
    % retrieve the returned token
    token = char(result.Result.AccessToken);
    fprintf(2, 'User token aquired and will expire at %s & extended expires at %s', result.Result.ExpiresOn.LocalDateTime.ToString,result.Result.ExtendedExpiresOn.ToLocalTime.ToString);
    
  5. Az engedélyezési jogkivonat használatával kérdezheti le a fürtöt a REST API-val:

    options=weboptions('HeaderFields',{'RequestMethod','POST';'Accept' 'application/json';'Authorization' ['Bearer ', token]; 'Content-Type' 'application/json; charset=utf-8'; 'Connection' 'Keep-Alive'; 'x-ms-app' 'Matlab'; 'x-ms-client-request-id' 'Matlab-Query-Request'});
    
    % The DB and KQL variables represent the database and query to execute
    querydata = struct('db', "<DB>", 'csl', "<KQL>");
    querryresults  = webwrite("https://sdktestcluster.westeurope.dev.kusto.windows.net/v2/rest/query", querydata, options);
    
    % Extract the results row
    results=querryresults{3}.Rows