Back to snippets

azure_kusto_data_query_with_interactive_login.py

python

This quickstart demonstrates how to query data from Azure Data Explorer

15d ago34 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azure_kusto_data_query_with_interactive_login.py
1from azure.kusto.data import KustoClient, KustoConnectionStringBuilder
2from azure.kusto.data.exceptions import KustoServiceError
3from azure.kusto.data.helpers import dataframe_from_result_table
4
5# Replace the following with your own cluster and database details
6CLUSTER_URL = "https://help.kusto.windows.net"
7DATABASE = "Samples"
8QUERY = "StormEvents | take 10"
9
10# To authenticate with a tenant ID, client ID, and client secret, use:
11# kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(CLUSTER_URL, "client_id", "client_secret", "tenant_id")
12
13# For interactive login authentication:
14kcsb = KustoConnectionStringBuilder.with_interactive_login(CLUSTER_URL)
15
16try:
17    # Initialize the Kusto Client
18    client = KustoClient(kcsb)
19
20    # Execute the query
21    response = client.execute(DATABASE, QUERY)
22
23    # Access the primary results table
24    # Each row in the response is accessible by index or column name
25    for row in response.primary_results[0]:
26        print(row)
27        # Example: print(row["StartTime"], row["State"])
28
29    # Optional: Convert the result to a Pandas DataFrame
30    # df = dataframe_from_result_table(response.primary_results[0])
31    # print(df)
32
33except KustoServiceError as error:
34    print(f"Error: {error}")
azure_kusto_data_query_with_interactive_login.py - Raysurfer Public Snippets