1. Databricks SQL for Ad-hoc Querying in Machine Learning Experimentation


    Databricks SQL offers an intuitive environment for running ad-hoc queries and creating dashboards on data stored in your lakehouse. You can execute SQL commands to explore and visualize data, and these capabilities are specifically useful for data scientists or data engineers who are conducting machine learning experimentation where they need to inspect or visualize data quickly.

    For setting up a Databricks SQL environment to facilitate ad-hoc querying for machine learning experimentation, you'll need to create several resources via Pulumi:

    1. Databricks SQL Endpoint: This acts as a compute resource to run SQL queries. It can be seen as a server where your SQL queries execute.

    2. Databricks SQL Permissions: This manages access to specific SQL endpoints, tables, or other objects, to ensure that the right users and services have the necessary permissions.

    3. Databricks SQL Queries: This is where you define and store individual SQL queries that can be run against your data sources.

    4. Databricks SQL Dashboards: To visualize and interpret the resulting data from your queries, you can create dashboards.

    Below, we will write a Pulumi Python program that sets up these resources in a Databricks environment.

    import pulumi import pulumi_databricks as databricks # Create a Databricks SQL Endpoint to run SQL queries. sql_endpoint = databricks.SqlEndpoint( "sql-endpoint", name="ad-hoc-queries-endpoint", # Example cluster size, adjust according to your needs. cluster_size="Medium", # The number of minutes of inactivity before the cluster is automatically stopped. auto_stop_mins=100, # Example: Enable Photon-powered queries for improved performance. enable_photon=True, ) # Sample query to run on the Databricks SQL Endpoint for ad-hoc analysis. sql_query = databricks.SqlQuery( "sql-query", query="SELECT * FROM my_ml_table LIMIT 100", # Specify the SQL Endpoint ID. parent=sql_endpoint.id, # Optionally name your query for easier identification. name="List 100 Experiments", # The datasource against which to run the query. data_source_id="my-datasource-id", ) # Setup permissions for the SQL Endpoint, allowing specific users to access it. sql_permissions = databricks.SqlPermissions( "sql-permissions", cluster_id=sql_endpoint.id, # Example: Grant SELECT privilege to all users. privilege_assignments=[{ "principal": "users", "privileges": ["SELECT"], }], ) # Create a dashboard to display results of SQL queries. sql_dashboard = databricks.SqlDashboard( "sql-dashboard", name="ML Experimentation Overview", # Your Databricks workspace URL, used for constructing the dashboard URL. workspace_url="https://{databricks-instance}.cloud.databricks.com", ) # Export the endpoint URL and the dashboard URL so it's easy to access. pulumi.export('sql_endpoint_url', sql_endpoint.jdbc_url) pulumi.export('sql_dashboard_url', pulumi.Output.concat(sql_dashboard.workspace_url, "/sql/dashboards/", sql_dashboard.id))

    In the above program, we have assumed that you have an existing Databricks workspace and data source in place. We have created various resources:

    • A SqlEndpoint: this provides a compute resource. We have defined a medium-sized cluster and made it automatically stop after 100 minutes of inactivity to manage costs.
    • A SqlQuery: this is an actual query that selects data from a specified table. The parent property is set to the ID of the SQL endpoint, linking the query to where it will run.
    • SqlPermissions: Access control is crucial for secure use of data. Here we gave SELECT access to all users. Modify this to match your organization's security requirements.
    • SqlDashboard: Finally, we created a dashboard resource for visualizing results.

    The export at the end provides easy access URLs of the endpoint and the dashboard.

    Remember to replace placeholder IDs, instance names, and configurations with actual values from your environment. Ensure the credentials for Pulumi to access your Databricks environment are set up correctly, and run the program using Pulumi CLI commands.

    This setup allows you to iterate quickly on your machine learning experiments, leveraging SQL for immediate insights and visualizations of the results.