Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Esplora dati di Azure è un servizio di esplorazione dati rapido e a scalabilità elevata per dati di log e di telemetria. In questo articolo verranno creati criteri di database e tabelle per Esplora dati di Azure usando C# o Python.
Prerequisiti
- Una sottoscrizione di Azure. Creare un account Azure gratuito.
- Un cluster e un database di Azure Data Explorer. Creare un cluster e un database.
- Tabella di test.
Installare i pacchetti
- Installare il pacchetto NuGet Azure Data Explorer (Kusto).
- Installare il pacchetto NuGet MSAL per l'autenticazione con Azure Active Directory (Azure AD).
- Installare il pacchetto NuGet Microsoft.Rest.ClientRuntime per l'autenticazione con Azure Active Directory (Azure AD).
Autenticazione
Per eseguire gli esempi in questo articolo, sono necessari un'applicazione Azure AD e un'entità servizio che possano accedere alle risorse. Se necessario, creare un'applicazione Azure AD e concedergli le assegnazioni di ruolo appropriate nella sottoscrizione. Salva l'ID directory (tenant), l'ID applicazione e il segreto client.
Potrebbe essere necessario aggiungere la nuova applicazione Azure AD come principale nel database. Per altre informazioni, vedere Gestire le autorizzazioni del database di Esplora dati di Azure.
Modificare i criteri di conservazione del database
Nell'esempio seguente vengono impostati criteri di conservazione con un periodo di eliminazione temporanea di 10 giorni.
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Azure AD Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Application ID
var clientSecret = "PlaceholderClientSecret"; // Application secret
var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
// Create a confidential authentication client for Azure AD:
var authClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithClientSecret(clientSecret) // can be replaced by .WithCertificate to authenticate with an X.509 certificate
.Build();
// Acquire application token
var result = authClient.AcquireTokenForClient(
new[] { "https://management.core.windows.net/.default" } // Define scopes for accessing Azure management plane
).ExecuteAsync().Result;
var credentials = new TokenCredentials(result.AccessToken, result.TokenType);
var kustoManagementClient = new KustoManagementClient(credentials) { SubscriptionId = subscriptionId };
var resourceGroupName = "testrg";
// The cluster and database that are created as part of the prerequisites
var clusterName = "mykustocluster";
var databaseName = "mykustodatabase";
var databasePatch = new ReadWriteDatabase(softDeletePeriod: TimeSpan.FromDays(10));
await kustoManagementClient.Databases.UpdateAsync(resourceGroupName, clusterName, databaseName, databasePatch);
Modificare i criteri della cache del database
Nell'esempio seguente vengono impostati criteri di cache per il database. I cinque giorni precedenti di dati saranno presenti nell'unità SSD del cluster.
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Azure AD Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Application ID
var clientSecret = "PlaceholderClientSecret"; // Application secret
var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
// Create a confidential authentication client for Azure AD:
var authClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithClientSecret(clientSecret) // can be replaced by .WithCertificate to authenticate with an X.509 certificate
.Build();
// Acquire application token
var result = authClient.AcquireTokenForClient(
new[] { "https://management.core.windows.net/.default" } // Define scopes for accessing Azure management plane
).ExecuteAsync().Result;
var credentials = new TokenCredentials(result.AccessToken, result.TokenType);
var kustoManagementClient = new KustoManagementClient(credentials) { SubscriptionId = subscriptionId };
var resourceGroupName = "testrg";
// The cluster and database that are created as part of the prerequisites
var clusterName = "mykustocluster";
var databaseName = "mykustodatabase";
var databasePatch = new ReadWriteDatabase(hotCachePeriod: TimeSpan.FromDays(10));
await kustoManagementClient.Databases.UpdateAsync(resourceGroupName, clusterName, databaseName, databasePatch);
Modificare i criteri della cache delle tabelle
Nell'esempio seguente vengono impostati criteri di cache per la tabella usando Kusto Data SDK. Questo frammento di codice configura la cache ad accesso frequente del cluster (UNITÀ SSD locali) per contenere i cinque giorni più recenti di dati.
var kustoUri = "https://<clusterName>.<region>.kusto.windows.net/";
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Azure AD Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Application ID
var clientSecret = "PlaceholderClientSecret"; // Application secret
var kustoConnectionStringBuilder = new KustoConnectionStringBuilder(kustoUri)
.WithAadApplicationKeyAuthentication(clientId, clientSecret, tenantId);
using var kustoClient = KustoClientFactory.CreateCslAdminProvider(kustoConnectionStringBuilder);
var command = CslCommandGenerator.GenerateAlterTableCachingPolicyCommand(
"<tableName>", hotSpan: TimeSpan.FromDays(5)
);
await kustoClient.ExecuteControlCommandAsync("<databaseName>", command);
Aggiungere una nuova entità per il database
L'esempio seguente aggiunge una nuova applicazione Azure AD come entità di amministrazione per il database.
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Azure AD Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Application ID
var clientSecret = "PlaceholderClientSecret"; // Application secret
var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
// Create a confidential authentication client for Azure AD:
var authClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithClientSecret(clientSecret) // can be replaced by .WithCertificate to authenticate with an X.509 certificate
.Build();
// Acquire application token
var result = authClient.AcquireTokenForClient(
new[] { "https://management.core.windows.net/.default" } // Define scopes for accessing Azure management plane
).ExecuteAsync().Result;
var credentials = new TokenCredentials(result.AccessToken, result.TokenType);
var kustoManagementClient = new KustoManagementClient(credentials) { SubscriptionId = subscriptionId };
var resourceGroupName = "testrg";
// The cluster and database that are created as part of the prerequisites
var clusterName = "mykustocluster";
var databaseName = "mykustodatabase";
var databasePrincipalListRequest = new DatabasePrincipalListRequest
{
Value = new List<DatabasePrincipal> { new("Admin", "<databasePrincipalName>", "App", appId: clientId, tenantName: tenantId) }
};
await kustoManagementClient.Databases.AddPrincipalsAsync(resourceGroupName, clusterName, databaseName, databasePrincipalListRequest);