> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to Databricks

> How to connect Atoti DirectQuery to Databricks using OAuth machine-to-machine or a personal access token via `DatabricksConnectionProperties`, and the minimum `USE CATALOG`, `USE SCHEMA`, and `SELECT` Unity Catalog grants

The Atoti DirectQuery Databricks connector uses the
[Databricks JDBC driver](https://docs.databricks.com/aws/en/integrations/jdbc/) to connect to a lakehouse.
The driver supports several authentication types, and only some of them suit a long-running server. Read access to the
queried data is granted separately, through Unity Catalog. Creating the connector itself is described in
[Create the database connector](https://docs.activeviam.com/engine/java-sdk/6.2/directquery/getting_started.md#create-the-database-connector).

## Credentials

DirectQuery passes both the connection string parameters and the explicitly set JDBC properties to the Databricks JDBC
driver `DataSource`. An explicitly set property takes precedence over a connection string parameter of the same name.

> [!WARNING]
>
> Secrets do not belong in the connection string, which can end up in log files. Pass every secret as an
> `additionalOption` on `DatabricksConnectionProperties` instead.

### How to authenticate with OAuth machine-to-machine

OAuth machine-to-machine is the recommended authentication type for a self-hosted Atoti Server. A Databricks service
principal authenticates with a client ID and a client secret. The driver obtains and renews access tokens by itself.
No operator action is needed for the lifetime of a long-running server.

Set `AuthMech=11`, `Auth_Flow=1` and `OAuth2ClientId` in the connection string:

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
jdbc:databricks://<workspace-hostname>:443/default;transportMode=http;ssl=1;httpPath=<http-path>;AuthMech=11;Auth_Flow=1;OAuth2ClientId=<client-id>
```

Pass the client secret as an additional option so that it stays out of the connection string:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final DatabricksConnectionProperties properties =
    DatabricksConnectionProperties.builder()
        .connectionString(connectionString)
        .additionalOption("OAuth2Secret", "<client-secret>")
        .build();
```

The service principal setup is described in [How to create the service principal and OAuth
application](#how-to-create-the-service-principal-and-oauth-application).

### How to authenticate with a service principal private key

This type replaces the shared client secret with a private key held by the service principal. Add `UseJWTAssertion=1` to
the [OAuth machine-to-machine](#how-to-authenticate-with-oauth-machine-to-machine) parameters. The driver then signs a
JWT assertion with the key. Access tokens are still renewed by the driver itself.

These parameters describe the key:

* `Auth_JWT_Key_File`: path to the private key file.
* `Auth_KID`: key identifier.
* `Auth_JWT_Alg`: signing algorithm. Defaults to `RS256`.
* `Auth_JWT_Key_Passphrase`: passphrase, when the key file is encrypted.

Set `Auth_Scope=sql` as well, because the driver sends no scope of its own on this type.

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
jdbc:databricks://<workspace-hostname>:443/default;transportMode=http;ssl=1;httpPath=<http-path>;AuthMech=11;Auth_Flow=1;UseJWTAssertion=1;OAuth2ClientId=<client-id>;Auth_JWT_Key_File=<key-file-path>;Auth_KID=<key-id>;Auth_Scope=sql
```

> [!NOTE]
>
> On Azure, a connection that sets `AzureTenantId` uses the Azure service principal type instead, which ignores
> `UseJWTAssertion`.

### How to authenticate with an Azure managed identity

An Atoti Server running on Azure compute can authenticate to an Azure Databricks workspace with an
[Azure managed identity](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview).
This type is only available on Azure. No secret has to be stored in the Atoti Server configuration. Access tokens are
renewed by the driver itself.

Set `AuthMech=11` and `Auth_Flow=3`, together with `azure_workspace_resource_id`, which the driver requires for this
type. For a user-assigned managed identity, add `OAuth2ClientId` to select which identity to use.

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
jdbc:databricks://<workspace-hostname>:443/default;transportMode=http;ssl=1;httpPath=<http-path>;AuthMech=11;Auth_Flow=3;azure_workspace_resource_id=<resource-id>
```

### How to authenticate with a personal access token

A Databricks personal access token is the quickest type to set up. Set `AuthMech=3` and `UID=token` in the connection
string:

```text theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
jdbc:databricks://<workspace-hostname>:443/default;transportMode=http;ssl=1;httpPath=<http-path>;AuthMech=3;UID=token
```

Pass the token itself as the `PWD` property, available as `DatabricksConnectionProperties.PASSWORD_PROPERTY_KEY`:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final DatabricksConnectionProperties properties =
    DatabricksConnectionProperties.builder()
        .connectionString(connectionString)
        .additionalOption(
            DatabricksConnectionProperties.PASSWORD_PROPERTY_KEY, "your-plain-password")
        .build();
```

Personal access tokens expire and have to be rotated by an operator. OAuth machine-to-machine is preferable for a
long-running server.

### How to authenticate with browser-based OAuth

Browser-based OAuth uses `AuthMech=11` with `Auth_Flow=2`. It requires a person to sign in through a browser.
It is therefore unsuitable for a server deployment. Use it for local development and exploration only.

The driver caches the resulting token on disk by default, and refuses to connect until that cache is configured.
Either set `TokenCachePassPhrase`, which encrypts the cache, or turn the cache off with `EnableTokenCache=0`.

### How does Databricks token federation apply?

Token federation is not an authentication type. It is a layer that the driver applies on top of the configured
authentication type.

Token federation is enabled by default, because the driver's `EnableTokenFederation` parameter defaults to `1`.
When the workspace is set up for
[OAuth token federation](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-federation), the driver exchanges the
token for a workspace token automatically. The `Identity_Federation_Client_Id` parameter names the OAuth application
used for the exchange.

No extra configuration is needed. Configure OAuth machine-to-machine as described in [How to authenticate with OAuth
machine-to-machine](#how-to-authenticate-with-oauth-machine-to-machine), and token federation applies on top of it. The
exchange only takes place when the token was issued by a provider other than the Databricks workspace itself.

## Permissions

The user or service principal used by DirectQuery needs read access to the queried data, granted through Unity Catalog.
The minimum [privileges](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges)
are:

* `USE CATALOG` on the target catalog
* `USE SCHEMA` on the target schema
* `SELECT` on the queried tables and views

These three privileges cover the complete DirectQuery read path. They are sufficient for the default
[time-travel behavior](https://docs.activeviam.com/engine/java-sdk/6.2/directquery/databases/databricks.md#time-travel-support), which reads table history, and for the detection of
views. DirectQuery does not write to the lakehouse.

The [aggregate tables](https://docs.activeviam.com/engine/java-sdk/6.2/directquery/aggregate-tables.md) feature does not change these privileges. Atoti generates the
`CREATE TABLE` and `INSERT INTO` statements but never executes them: they are run separately, under whichever Databricks
identity is chosen for that purpose.

### How to create the service principal and OAuth application

1. Create a service principal in the Databricks account console.
   See [Manage service principals](https://docs.databricks.com/aws/en/admin/users-groups/manage-service-principals).
2. Grant the service principal `USE CATALOG`, `USE SCHEMA`, and `SELECT` on the target catalog, schema, and tables.
   See [Manage privileges in Unity Catalog](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/).
3. Create an OAuth secret for the service principal. This step yields the client ID and the client secret.
   See [Authorize service principal access to Databricks with OAuth](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m).
4. Supply the client ID as `OAuth2ClientId` and the client secret as `OAuth2Secret`, as described in
   [How to authenticate with OAuth machine-to-machine](#how-to-authenticate-with-oauth-machine-to-machine).

The service principal also needs access to the compute it queries through. Grant it at least the `CAN USE` permission on
the target SQL warehouse or cluster.
See [SQL warehouse ACLs](https://docs.databricks.com/aws/en/security/auth-authz/access-control/sql-endpoint-acl).

The compute types and the Databricks runtime versions that DirectQuery supports are listed in the
[compatibility matrix](https://docs.activeviam.com/engine/java-sdk/6.2/directquery/compatibility.md#databricks).
