{"library":"azure-kusto-data","code":"import os\nfrom azure.kusto.data import KustoClient, KustoConnectionStringBuilder\nfrom azure.kusto.data.exceptions import KustoServiceError\n\n# Replace with your Kusto cluster URI\nCLUSTER_URI = os.environ.get('KUSTO_CLUSTER_URI', 'https://<your_cluster_name>.kusto.windows.net')\n# Replace with your AAD application ID (client ID)\nCLIENT_ID = os.environ.get('KUSTO_CLIENT_ID', 'your_aad_application_id')\n# Replace with your AAD application key (client secret)\nCLIENT_SECRET = os.environ.get('KUSTO_CLIENT_SECRET', 'your_aad_application_key')\n# Replace with your AAD tenant ID\nTENANT_ID = os.environ.get('KUSTO_TENANT_ID', 'your_aad_tenant_id')\n\nDB_NAME = 'Samples'\nQUERY = 'StormEvents | take 5'\n\ndef main():\n    if 'your_aad_application_id' in CLIENT_ID or 'your_aad_application_key' in CLIENT_SECRET:\n        print(\"Please set KUSTO_CLUSTER_URI, KUSTO_CLIENT_ID, KUSTO_CLIENT_SECRET, and KUSTO_TENANT_ID environment variables or replace placeholders.\")\n        return\n\n    # Build connection string for AAD application key authentication\n    kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(\n        CLUSTER_URI, CLIENT_ID, CLIENT_SECRET, TENANT_ID\n    )\n\n    # It is good practice to re-use the KustoClient instance, as it maintains a pool of connections.\n    try:\n        with KustoClient(kcsb) as client:\n            print(f\"Executing query on database '{DB_NAME}'...\")\n            response = client.execute(DB_NAME, QUERY)\n\n            for row in response.primary_results[0]:\n                print(f\"Timestamp: {row['StartTime']}, EventType: {row['EventType']}, State: {row['State']}\")\n\n    except KustoServiceError as e:\n        print(f\"Kusto service error: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n\nif __name__ == '__main__':\n    main()\n","lang":"python","description":"This quickstart demonstrates how to connect to an Azure Data Explorer (Kusto) cluster using Azure Active Directory (AAD) application key authentication, execute a Kusto Query Language (KQL) query, and print the results. Ensure you replace the placeholder values for `KUSTO_CLUSTER_URI`, `KUSTO_CLIENT_ID`, `KUSTO_CLIENT_SECRET`, and `KUSTO_TENANT_ID` with your actual credentials, ideally via environment variables for security. The example queries the 'Samples' database for 5 'StormEvents' records.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}