# Databricks

**What is Databricks?**

The Databricks node in StackAI allows you to query a Databricks workspace for data analytics and machine learning. It translates plain English or SQL queries into actionable database operations, returning both the executed SQL and the results.

***

**How to make a Databricks Connection?**

* Login to Databricks, the Workspace URL is illustrated below:
  * In this example `dbc-003abf48-15.cloud.databricks.com` is the Workspace URL<br>

    <figure><img src="https://3697023207-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFSlso1Kjob5CLDrh0dVn%2Fuploads%2FmopuDPBcfRPYio5KSbJr%2Fimage.png?alt=media&#x26;token=5989d014-c978-4dcc-994a-932b6b67be10" alt=""><figcaption></figcaption></figure>
* To get your Personal Access Token:

  1. Click on your Profile in the upper right
  2. Then click on Settings

  <figure><img src="https://3697023207-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFSlso1Kjob5CLDrh0dVn%2Fuploads%2FmEHjQlgc3kwImjfi8LVZ%2Fimage.png?alt=media&#x26;token=df2389cb-5586-4e70-b1ca-759a626afabd" alt=""><figcaption></figcaption></figure>
* Under Users, click on Developer
* Then in this menu, click on Manage

  <figure><img src="https://3697023207-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFSlso1Kjob5CLDrh0dVn%2Fuploads%2F8eXP6DW2ipr8rvdsrElO%2Fimage.png?alt=media&#x26;token=49064ac7-663c-49d4-ae65-28b3e9885d64" alt=""><figcaption></figcaption></figure>

***

**How to use it?**

1. Add the Databricks node to your StackAI workflow.
2. Provide the required database schema and your query (in plain English or SQL).
3. Connect the node to downstream nodes to process or display the results.

***

**Example of Usage**

Suppose you want to find the total revenue for 2024 from your sales table:

* **Schema Example:**

  ```
  TABLE Sales (OrderID INT, Customer STRING, Revenue DOUBLE, Year INT);
  ```
* **Query Example:**

  ```
  What is the total revenue for the year 2024?
  ```

***

**Available Actions**

#### 1. Query a Databricks Workspace

**Description:**\
Run analytics or machine learning queries on your Databricks database using natural language or SQL.

**Inputs**

* **Schema (sql\_schema)**
  * **Type:** Array of strings (textarea)
  * **Required:** Yes
  * **Description:** The database schema, including tables, columns, and types.
  * **Example:**

    ```
    TABLE Sales (OrderID INT, Customer STRING, Revenue DOUBLE, Year INT);
    ```
* **Query (query)**
  * **Type:** String
  * **Required:** Yes
  * **Description:** The question or command you want to run, in plain English or SQL.
  * **Example:**

    ```
    What is the total revenue for the year 2024?
    ```

**Configurations**

* No additional configurations are required for this action.

**Outputs**

* **Query (sql\_query)**
  * **Type:** String
  * **Required:** Yes
  * **Description:** The SQL query that was executed.
  * **Example:**

    ```
    SELECT SUM(Revenue) FROM Sales WHERE Year = 2024;
    ```
* **Results (results)**
  * **Type:** Array of objects
  * **Required:** Yes
  * **Description:** The results returned from the Databricks query.
  * **Example:**

    ```
    [
      { "SUM(Revenue)": 1250000 }
    ]
    ```

***

**Summary Table**

| Field       | Type             | Required | Description                                    | Example                                    |
| ----------- | ---------------- | -------- | ---------------------------------------------- | ------------------------------------------ |
| sql\_schema | Array of strings | Yes      | Database schema (tables, columns, types, etc.) | TABLE Sales (OrderID INT, Revenue DOUBLE); |
| query       | String           | Yes      | Query in plain English or SQL                  | What is the total revenue for 2024?        |
| sql\_query  | String           | Yes      | The SQL query that was executed                | SELECT SUM(Revenue) FROM Sales WHERE ...   |
| results     | Array of objects | Yes      | Results of the Databricks query                | \[{ "SUM(Revenue)": 1250000 }]             |

***

**Best Practices**

* Always provide a clear and complete schema for accurate query translation.
* Use natural language for ease, or SQL for precision.
* Review the returned SQL to ensure it matches your intent.

***

Use the Databricks node in StackAI to seamlessly integrate advanced analytics and machine learning queries into your automated workflows.
