MATLAB adalah platform komputasi pemrograman dan numerik yang digunakan untuk menganalisis data, mengembangkan algoritma, dan membuat model. Artikel ini menjelaskan cara mendapatkan token otorisasi di MATLAB untuk Azure Data Explorer, dan cara menggunakan token untuk berinteraksi dengan kluster Anda.
Prasyarat
Pilih tab untuk sistem operasi yang digunakan untuk menjalankan MATLAB.
Mulai ulang IDE MATLAB. Setelah dimuat ulang, jalankan skrip startup.m yang tersedia di matlab-azure-services\Software\MATLAB\startup.m. Ini memastikan semua fungsi prasyarat disiapkan untuk akses ke layanan Azure. Output akan terlihat sebagai berikut:
Dengan autentikasi pengguna, pengguna diminta untuk masuk melalui jendela browser. Setelah berhasil masuk, token otorisasi pengguna diberikan. Bagian ini memperlihatkan cara mengonfigurasi alur masuk interaktif ini.
Tentukan konstanta yang diperlukan untuk otorisasi. Untuk informasi selengkapnya tentang nilai-nilai ini, lihat Parameter autentikasi.
% 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 ');
Di studio MATLAB, muat file DLL yang diekstrak:
% 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);
% 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);
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
Buat file Auth.json di direktori kerja Anda dengan kredensial yang relevan. KustoClientAppId yang dikembalikan dari https://<adx-cluster>.kusto.windows.net/v1/rest/auth/metadata perlu diganti dengan nilai ClientId di bawah ini:
{
"AuthMethod": "InteractiveBrowser",
"TenantId" : "<AAD Tenant / Authority ID for the login>",
"ClientId" : "<Client ID>",
"RedirectUrl": "http://localhost:8675"
}
Kueri kluster Anda sebagai berikut:
% References the credential file created in the previous step
credentials = configureCredentials('Auth.json');
% Point the scopes to the Azure Data Explorer cluster that we want to query
request = azure.core.credential.TokenRequestContext();
request.addScopes('<https://adx-cluster-changeme.kusto.windows.net/.default>');
token=credentials.getToken(request);
% Prepare to query the cluster
options=weboptions('HeaderFields',{'RequestMethod','POST';'Accept' 'application/json';'Authorization' ['Bearer ', token.getToken()]; '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://adx-cluster-changeme.kusto.windows.net/v2/rest/query>', querydata, options);
% The results row can be extracted as follows
results=querryresults{3}.Rows
Melakukan autentikasi aplikasi
Otorisasi aplikasi Microsoft Entra dapat digunakan untuk skenario di mana masuk interaktif tidak diinginkan dan eksekusi otomatis diperlukan.
Menyediakan aplikasi Microsoft Entra. Untuk URI Pengalihan, pilih Web dan input http://localhost:8675 sebagai URI.
Tentukan konstanta yang diperlukan untuk otorisasi. Untuk informasi selengkapnya tentang nilai-nilai ini, lihat Parameter autentikasi.
% 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 = '';
Di studio MATLAB, muat file DLL yang diekstrak:
% 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);
% 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);
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
Buat file Auth.json di direktori kerja Anda dengan kredensial yang relevan:
{
"AuthMethod": "ClientSecret",
"TenantId" : "<AAD Tenant / Authority ID for the login>",
"ClientId" : "<Client ID of the Azure AD application>",
"ClientSecret": "<Client Key of the Azure AD application>",
"RedirectUrl": "http://localhost:8675"
}
Kueri kluster Anda sebagai berikut:
% References the credential file created in the previous step
credentials = configureCredentials('Auth.json');
% Point the scopes to the Azure Data Explorer cluster that we want to query
request = azure.core.credential.TokenRequestContext();request.addScopes('<https://adx-cluster-changeme.kusto.windows.net/.default>');
token=credentials.getToken(request);
% Prepare to query the cluster
options=weboptions('HeaderFields',{'RequestMethod','POST';'Accept' 'application/json';'Authorization' ['Bearer ', token.getToken()]; '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://adx-cluster-changeme.kusto.windows.net/v2/rest/query>', querydata, options);
% The results row can be extracted as follows
results=querryresults{3}.Rows