This repo contains Python scripts for exporting Kusto (Azure Data Explorer) table and function schemas to individual KQL files.
KustoExporter is designed to help you manage your Kusto (Azure Data Explorer) database schemas and functions as code. The primary goals and scenarios include:
- Version Control for KQL Objects: It is a best practice to maintain all your KQL objects (tables, functions, etc.) under version control. This enables you to track changes, collaborate with your team, and roll back to previous versions if needed.
- Enabling Code Reviews and Collaboration: By exporting your KQL schema to files, you can leverage pull requests, code reviews, and branch workflowsโjust like with application code.
- AI-Assisted Development: With your KQL schema and functions stored in a repository, AI coding tools such as GitHub Copilot can reason over your KQL system. This allows Copilot to provide context-aware suggestions, generate queries, and assist with schema evolution based on the definitions in your repo.
- Documentation and Auditing: Keeping your KQL definitions in a repo provides a clear, auditable history and serves as up-to-date documentation for your data platform.
KustoExporter streamlines the process of exporting, validating, and organizing your KQL objects, making it easier to adopt modern DevOps and AI-powered workflows for your data analytics environment.
- Color-coded Logging: Visual feedback for status, progress, warnings, and errors
- Robust Error Handling: Comprehensive exception handling with stack traces
- KQL Validation: Syntax validation for generated KQL commands
- Automatic README Generation: Each export creates a detailed README.md in the output directory
- Detailed Progress Tracking: Real-time feedback on processing status
export-table-schemas.py
โ Exports table CREATE commands (uses shared utilities)export-function-schemas.py
โ Exports function CREATE-OR-ALTER commands (uses shared utilities)kusto_export_utils.py
โ Shared utilities module
run_export_cluster_objects.bat
โ Batch script to run exports
# Base classes and utilities
โโโ Colors # ANSI color codes for console output
โโโ Logger # Centralized logging with color-coded messages
โโโ KustoExporter # Base class with common export functionality
โโโ ReadmeGenerator # Consistent README.md generation
โโโ ExportSummary # Standardized summary reporting
# Inherits from KustoExporter
โโโ TableExporter
โโโ get_table_create_commands() # Fetch and format CREATE TABLE commands
โโโ _format_schema_definition() # Ensure proper KQL syntax formatting
โโโ export_tables() # Main export orchestration
โโโ _export_table_files() # File writing logic
# Inherits from KustoExporter
โโโ FunctionExporter
โโโ get_functions() # Fetch function list
โโโ get_function_details() # Extract function metadata
โโโ parse_parameters_field() # Parse function parameters
โโโ validate_kql_function() # KQL syntax validation
โโโ build_function_kql() # Generate complete KQL function
โโโ export_functions() # Main export orchestration
โโโ _export_function_files() # File writing logic
python export-table-schemas.py -c "https://cluster.kusto.windows.net" -d "DatabaseName" -o "table_exports"
python export-function-schemas.py -c "https://cluster.kusto.windows.net" -d "DatabaseName" -o "function_exports"
You can use the provided batch script to export both tables and functions in one step:
run_export_cluster_objects.bat -c "https://cluster.kusto.windows.net" -d "DatabaseName" -o "exports"
This will run both export scripts with the specified cluster and database.
-c, --cluster
: Kusto cluster URL (required)-d, --database
: Database name (required)-o, --output-dir
: Output directory name (optional)
Each export creates a dedicated directory with:
output_directory/
โโโ README.md # Auto-generated documentation
โโโ ObjectName1.kql # Individual KQL files
โโโ ObjectName2.kql
โโโ ...
Table Export:
// CREATE TABLE command for MyTable
// Generated on 2025-01-14 15:30:25
.create table MyTable (
Column1: string,
Column2: datetime,
Column3: real
)
Function Export:
// CREATE-OR-ALTER FUNCTION command for MyFunction
// Generated on 2025-01-14 15:30:25
.create-or-alter function with (docstring = "Function description", folder = "Analytics")
MyFunction(param1: string, param2: int = 10) {
MyTable
| where Column1 == param1
| take param2
}
- Uses Azure CLI authentication (
az login
required) - Leverages
KustoConnectionStringBuilder.with_az_cli_authentication()
azure-kusto-data
argparse (built-in)
re (built-in)
os (built-in)
datetime (built-in)
traceback (built-in)
- Kusto API Errors: Handles connection and query issues
- Schema Validation: Ensures proper KQL syntax before export
- File System Errors: Graceful handling of directory creation and file writing issues
- Progress Tracking: Real-time feedback with colored console output
The modular architecture makes it easy to add new exporters:
# Example: New Policy Exporter
class PolicyExporter(KustoExporter):
def export_policies(self):
# Inherits all base functionality
# Just implement policy-specific logic
pass
- Authentication Failed: Run
az login
and ensure you have access to the cluster - Empty Schema: Check that tables/functions exist in the specified database
- Permission Denied: Ensure you have read permissions on the cluster/database
- File Write Errors: Check that the output directory is writable
Both scripts provide detailed logging. Look for:
- Blue [INFO]: General information
- Cyan [PROGRESS]: Operation progress
- Green [SUCCESS]: Successful operations
- Yellow [WARNING]: Non-fatal issues
- Red [ERROR]: Fatal errors