Aracılığıyla paylaş


MATLAB kullanarak verileri sorgulama

MATLAB verileri analiz etmek, algoritma geliştirmek ve model oluşturmak için kullanılan bir programlama ve sayısal bilgi işlem platformudur. Bu makalede Azure Veri Gezgini için MATLAB'de yetkilendirme belirteci alma ve kümenizle etkileşime geçmek için belirteci kullanma açıklanmaktadır.

Önkoşullar

MATLAB'i çalıştırmak için kullanılan işletim sisteminin sekmesini seçin.

  1. NuGet'ten Microsoft Identity client ve Microsoft Identity Abstractions paketlerini indirin.

  2. İndirilen paketleri ve DLL dosyalarını lib\net45'ten istediğiniz bir klasöre ayıklayın. Bu makalede C:\Matlab\DLL klasörünü kullanacağız.

Kullanıcı kimlik doğrulaması gerçekleştirme

Kullanıcı kimlik doğrulamasıyla, kullanıcıdan tarayıcı penceresinden oturum açması istenir. Oturum başarıyla açıldıktan sonra bir kullanıcı yetkilendirme belirteci verilir. Bu bölümde, bu etkileşimli oturum açma akışının nasıl yapılandırılır gösterilmektedir.

Kullanıcı kimlik doğrulaması gerçekleştirmek için:

  1. Yetkilendirme için gereken sabitleri tanımlayın. Bu değerler hakkında daha fazla bilgi için bkz . Kimlik doğrulama parametreleri.

    % 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. MATLAB Studio'da ayıklanan DLL dosyalarını yükleyin:

    % 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. PublicClientApplicationBuilder'ı kullanarak kullanıcı etkileşimli oturum açma isteminde bulunun:

    % 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. Rest API aracılığıyla kümenizi sorgulamak için yetkilendirme belirtecini kullanın:

    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
    

Uygulama kimlik doğrulaması gerçekleştirme

Microsoft Entra uygulama yetkilendirmesi, etkileşimli oturum açmanın istenmediği ve otomatik çalıştırmaların gerekli olduğu senaryolar için kullanılabilir.

Uygulama kimlik doğrulaması gerçekleştirmek için:

  1. Microsoft Entra uygulaması sağlama. Yeniden Yönlendirme URI'si için Web'i seçin ve URI olarak giriş http://localhost:8675 yapın.

  2. Yetkilendirme için gereken sabitleri tanımlayın. Bu değerler hakkında daha fazla bilgi için bkz . Kimlik doğrulama parametreleri.

    % 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. MATLAB Studio'da ayıklanan DLL dosyalarını yükleyin:

     % 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. Microsoft Entra uygulamasıyla etkileşimli olmayan bir otomatik oturum açma gerçekleştirmek için ConfidentialClientApplicationBuilder'ı kullanın:

    %  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. Rest API aracılığıyla kümenizi sorgulamak için yetkilendirme belirtecini kullanın:

    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