# Snowflake

This data destination connector sends data to a [Snowflake](https://www.snowflake.com/) database. It can either write data
to an existing Snowflake database, or to a new database it provisions to you. The latter case is useful for sharing your data in form of an SQL database with some service.
For example, you can use it to send data to [Tableau](https://www.tableau.com/), [PowerBI](https://powerbi.microsoft.com/en-us/), etc.
Projects using [Multi-tenant](https://help.keboola.com/storage/#multi-tenant-mt) Snowflake backend can use [Data Gateway](https://help.keboola.com/components/applications/data-gateway/) component instead.



**Before you start**

You need:

- **The mode you need,** one of the two (#configuration). A **Keboola Snowflake database** needs nothing outside Keboola, but the project must use its own Snowflake backend (KBDB (/storage/#keboola-brings-database-kbdb)/BYODB (/storage/#bring-your-own-database-byodb)). Your **own Snowflake database** needs the items below.

For your own Snowflake database:

- **A Snowflake account, and a database, schema and warehouse (https://docs.snowflake.com/en/user-guide/warehouses)** for the data.
- **A user for the connector** whose default role has `USAGE` on the database and the warehouse and all privileges on the schema. The connector runs as the user's default role unless the configuration names another one. Create a dedicated user (#create-a-dedicated-user) has the SQL for such a role and user.
- **A key pair (https://docs.snowflake.com/en/user-guide/key-pair-auth) for that user:** the SQL in Create a dedicated user (#create-a-dedicated-user) registers the public key, and the connector gets the private key. The private key must be unencrypted (generated with `-nocrypt`): the connector has no passphrase setting. Snowflake blocks password sign-in for service users and no longer accepts `TYPE = LEGACY_SERVICE` (Snowflake MFA rollout (https://docs.snowflake.com/en/user-guide/security-mfa-rollout)).
- **On AWS and Azure stacks, the account parameters <a href="https://docs.snowflake.com/en/sql-reference/parameters#require-storage-integration-for-stage-creation">`REQUIRE_STORAGE_INTEGRATION_FOR_STAGE_CREATION`</a> and <a href="https://docs.snowflake.com/en/sql-reference/parameters#require-storage-integration-for-stage-operation">`REQUIRE_STORAGE_INTEGRATION_FOR_STAGE_OPERATION`</a> left at their default, `FALSE`.** There the connector creates an external stage with temporary credentials for Keboola's staging storage on every run, which Snowflake refuses when either parameter is `TRUE`.
- **If your account restricts access with network policies (https://docs.snowflake.com/en/user-guide/network-policies),** allow the Keboola IP addresses (/components/ip-addresses/) of your stack.

What each tab needs:

- **Prompt tab:** Kai, the **Kai Agent** button in the top bar, on supported stacks. A new Free Plan project has it from the start; elsewhere an organization admin turns it on once: Get started with Kai (/kai/getting-started/).
- **UI tab:** a browser.
- **CLI / API tab:** kbagent (/cli/getting-started/), connected to your project. The checks only read.



## Create a dedicated user

We highly recommend that you create a dedicated user for the connector in your Snowflake database. You can use the following SQL code to get started:

```sql
CREATE ROLE WRITER_SAMPLE;
CREATE DATABASE WRITER_SAMPLE;
GRANT USAGE ON DATABASE WRITER_SAMPLE TO ROLE WRITER_SAMPLE;
CREATE SCHEMA WRITER_SAMPLE.WRITER_SAMPLE;
GRANT ALL PRIVILEGES ON SCHEMA WRITER_SAMPLE.WRITER_SAMPLE TO ROLE WRITER_SAMPLE;
GRANT USAGE ON WAREHOUSE dev TO ROLE WRITER_SAMPLE;
-- RSA_PUBLIC_KEY: your public key without the -----BEGIN/END PUBLIC KEY----- lines, joined into one line
CREATE USER WRITER_SAMPLE
			TYPE = SERVICE
			RSA_PUBLIC_KEY = 'MIIBIjANBgkqh...'
			DEFAULT_ROLE = WRITER_SAMPLE
			DEFAULT_WAREHOUSE = dev
			DEFAULT_NAMESPACE = WRITER_SAMPLE.WRITER_SAMPLE;
GRANT ROLE WRITER_SAMPLE TO USER WRITER_SAMPLE;
```

## Configuration
[Create a new configuration](https://help.keboola.com/components/configurations/#create-a-configuration) of the **Snowflake** data destination connector.

The first step is to **set up credentials**:

![Screenshot - Main page](https://help.keboola.com/components/writers/database/snowflake/snowflake-1.png)

There are two modes of operation of the connector:

- **Own Snowflake database** — Use this when you have your own Snowflake database -- i.e., you have a contract with Snowflake, or someone gave you credentials of a database to write to.
- **Keboola Snowflake database** — In this mode, the connector will create a new schema in project's database for you and **give you credentials** to it. This option is available **only for projects using own Snowflake backend** ([KBDB](https://help.keboola.com/storage/#keboola-brings-database-kbdb)/[BYODB](https://help.keboola.com/storage/#bring-your-own-database-byodb)).

### Own Snowflake Database
You need to provide a *host name* (account name), *user name*, *private key*, *database name*, *schema*, and *[warehouse](https://docs.snowflake.com/en/user-guide/warehouses)*.

![Screenshot - Own Credentials](https://help.keboola.com/components/writers/database/snowflake/snowflake-2.png)

Keep in mind that Snowflake is case sensitive and if identifiers are not quoted, they are converted to upper case. So if you run, for example, a
query `CREATE SCHEMA john.doe;`, you need to enter the schema name as `DOE` in the connector configuration.

### Keboola Snowflake Database
A Keboola Snowflake schema is created in project's database by the component and the credentials are provisioned for you:

![Screenshot - Provisioned Credentials](https://help.keboola.com/components/writers/database/snowflake/snowflake-3.png)

You can share the credentials with whatever service that needs to access your data.

## Table Configuration
The next step is to configure the tables you want to write. Click **Add Table** and select an existing table from Storage:

![Screenshot - Select Table](https://help.keboola.com/components/writers/database/snowflake/snowflake-4.png)

The next step is to specify table configuration. Use the **preview** icon to peek at the column contents.

![Screenshot - Table Columns](https://help.keboola.com/components/writers/database/snowflake/snowflake-5.png)

For each column you can specify its

- **Column Name** in the destination database; you can also use the select box in the table header to bulk convert the case of all names.
- **Data Type** (one of [Snowflake data types](https://docs.snowflake.com/en/sql-reference-data-types)); you can also use the select box in the table header to bulk set the type for all columns. Setting the data type to `IGNORE` means that column will not be present in the destination table.
- **Nullable**; when checked, the column will be marked as nullable and empty values (`''`) in that column will be converted to `NULL`. Use this for non-string columns with missing data.
- **Default Value**; the provided value will be set as the [default value of the column](https://docs.snowflake.com/en/sql-reference/sql/create-table#optional-parameters) in the target table.

The Snowflake connector can take advantage of the [column metadata](https://help.keboola.com/storage/tables/#metadata). If they are available, the
column types are pre-filled automatically. Make sure to verify the suggested types, however. These data types are taken
from the data source and may not be the best choice for the data destination.

When done configuring the columns, don't forget to **save** the settings.

### Load Settings
At the top of the page, you can specify the target table name and additional load settings. There are two main options how the connector
can write data to tables — **Full Load** and **Incremental Load**.

![Screenshot - Table Options](https://help.keboola.com/components/writers/database/snowflake/snowflake-6.png)

In the **Incremental load** mode, the data are bulk inserted into
the destination table and the table structure must match (including the data types). That means the structure of the target table
will not be modified. If the target table doesn't exist, it will be created. If a primary key is defined on the table, the
data is [upserted](https://en.wikipedia.org/wiki/Merge_(SQL)). If no primary key is defined, the data is inserted.

In the **Full load** mode, the table is completely overwritten including the table structure. The table is overwritten
using the [`ALTER SWAP`](https://docs.snowflake.com/en/sql-reference/sql/alter-table#parameters) command, which ensures
the shortest unavailability of the target table. However, this operation still drops the table.

Additionally, you can specify a **Primary key** of the table, a simple column **Data filter**, and a **Data changed in last** filter for
[incremental processing](https://help.keboola.com/storage/tables/incremental-loading/#incremental-processing).

**Data changed in last** filter is not available when using **Automatic incremental load**, as the component will use the last run date to determine the data to be loaded.

Don't forget to **Save** the configuration, then click **Run Component**.

**Check:**

#### Prompt

```text title="Prompt" wrap
Did the last job of the Snowflake data destination configuration my-snowflake-config end with success? Give its end time.
```

Kai only reads here, so it asks for no approval, and its answer links to what it found in your project.

#### UI

The job ends with **Success**, and the table appears in the target schema with the
rows from the Storage table.

#### CLI / API

```bash
kbagent job list --component-id keboola.wr-db-snowflake --limit 1
```

This is the component ID on AWS stacks; on another stack, use the `component_id` that `kbagent config list` shows for your configuration. If you have several configurations, add `--config-id` with yours. Look for `status` `success`, and then check the destination as in the UI tab.

---


## Using Keboola Snowflake Database
The connector offers the option to create a [Keboola-provisioned database](https://help.keboola.com/components/writers/database/snowflake/#keboola-snowflake-database) for you. You can
use this database to connect Keboola to a wide range of consumers — especially Business Intelligence tools and Analytics.
The database can be queried in real time, but is still completely isolated from your project Storage. The database query execution time is limited to
900 seconds (15 minutes).

Of course, you can use your own Snowflake database for this purpose. And if you are using [Multi-tenant](https://help.keboola.com/storage/#multi-tenant-mt) Snowflake backend, you can use [Data Gateway](https://help.keboola.com/components/applications/data-gateway/) component instead.

### Connect to Looker
It is possible to use the Snowflake connector to share data with [Looker](https://looker.com/).
To share data between your Keboola project and Looker, choose the **Keboola Snowflake database** when configuring the credentials.
The connector will create a dedicated database for you and give you credentials. Run the connector and when it is finished, 
connect the Looker data sources. Follow the official [instructions](https://docs.looker.com/setup-and-management/connecting-to-db).
There are also some [Snowflake specific settings](https://docs.looker.com/setup-and-management/database-config/snowflake#adding_the_connection). Note that you
can skip a number of steps because a dedicated user is already created for you.

### Connect to Power BI Desktop
It is possible to use the Snowflake connector to share data with [Power BI Desktop](https://powerbi.microsoft.com/en-us/desktop/).
To share data between your Keboola project and PowerBI, choose the **Keboola Snowflake database** when configuring the credentials.
The connector will create a dedicated database for you and give you credentials. Run the connector and when it is finished, connect the Power BI data sources. Follow the official 
[instructions for Power BI Desktop](https://docs.microsoft.com/en-us/power-bi/desktop-connect-snowflake).

### Connect to Qlik
It is possible to use the Snowflake connector to share data with [Qlik Sense](https://www.qlik.com/us/products/qlik-sense)
and [QlikView](https://www.qlik.com/us/products/qlikview).
To share data between your Keboola project and Qlik, choose the **Keboola Snowflake database** when configuring the credentials.
The connector will create a dedicated database for you and give you credentials. Run the connector and when it is finished, connect the Qlik data sources. Follow the Qlik official
[integration guide](https://help.qlik.com/en-US/connectors/Subsystems/ODBC_connector_help/Content/Connectors_ODBC/Snowflake/Create-Snowflake-connection.htm).

### Connect to Tableau
It is possible to use the Snowflake connector to share data with [Tableau Desktop](https://www.tableau.com/products/desktop) or
[Tableau Online](https://www.tableau.com/products/cloud-bi). This is usually more efficient and
faster than loading data through [TDE files](https://www.tableau.com/about/blog/2014/7/understanding-tableau-data-extracts-part1)
with the [TDE data destination connector](https://help.keboola.com/components/writers/bi-tools/tableau/).

To share data between your Keboola project and Tableau, choose the **Keboola Snowflake database** when configuring the credentials.
The connector will create a dedicated database for you and give you credentials. Run the connector and when it is finished, connect the Tableau data sources. Follow the official 
[instructions for Tableau Desktop](https://help.tableau.com/current/pro/desktop/en-us/examples_snowflake.htm)
or for [Tableau Online](https://help.tableau.com/current/online/en-us/to_connect_live_sql.htm). Use the username/password authentication method.


### Connect to Retool
It is possible to use the Snowflake connector to share data with [Retool](https://retool.com/). Retool requires specifying the User Role when creating the resource. The user role will always be the same as the USERNAME provided by the **Keboola Snowflake database** credentials. [Read more about connecting Snowflake to Retool](https://docs.retool.com/docs/snowflake-integration)
