C3 AI Documentation Home

Snowflake Connector

The C3 Agentic AI Platform has a built-in connector for integrating with Snowflake databases.

To connect to Snowflake from your application:

  1. Add a SqlSourceSystem modeling the Snowflake source system to your package.
  2. Configure the JdbcCredentials authorizing the connection to the external Snowflake table.
  3. Add a SqlSourceCollection modeling the target Snowflake table to your package.
  4. Create an External Type modeling the schema of the external Snowflake table.

The following sections include detailed instructions for configuring the connection. For more information on configuring the JDBC driver, see Configuring the JDBC Driver in the Snowflake documentation.

Model the source system

Create a SqlSourceSystem and set the name field as the identifier for the external database system.

For example, you can add the following SnowflakeSourceSystem.json to the \metadata\SqlSourceSystem directory of your package:

JSON
{
    "name": "SnowflakeSourceSystem"
}

Configure the credential used to authorize the JDBC connection

Create a JdbcCredentials Type instance to configure the connection to the external Snowflake table by passing the following fields to the JdbcCredentials.fromServerEndpoint() method:

  • serverEndpoint — Snowflake server hostname
  • port — Not required for Snowflake; use -1
  • datastoreType — Specifies that the JdbcCredentials authorizes a connection to Snowflake
  • database — The name of the Snowflake database that you are connecting to
  • schemaName — The name of the schema within the database
  • username — The account with authorization to access the Snowflake database table
  • password — The password for this account
  • warehouse — additional metadata required to make the JDBC connection

For example, run the following from console to configure the JdbcCredentials:

JavaScript
var creds = JdbcCredentials.fromServerEndpoint("my_sf_instance.snowflakecomputing.com",
                                                -1,
                                                DatastoreType.SNOWFLAKE,
                                                "SOME_DB",
                                                "SOME_SCHEMA",
                                                "some_account",
                                                "some_password");

creds = creds.withField("properties", {"warehouse" : "SOME_WH"});
JdbcStore.forName("SnowflakeSourceSystem").setCredentials(creds, ConfigOverride.APP);
JdbcStore.forName("SnowflakeSourceSystem").setExternal(ConfigOverride.APP);

Model the table containing the data

To model the external Snowflake table in your application, create a SqlSourceCollection and set the following fields:

name: Identifier for the Snowflake table source: Name of the External Type that models the schema of the external Snowflake table sourceSystem: Name of the Snowflake SqlSourceSystem

For example, to model a table called RELIABILITY, you can add the following SnowflakeTable.json to the \metadata\SqlSourceCollection directory of your package:

JSON
{
    "name" : "Reliability",
    "source" : "Reliability",
    "sourceSystem" : {
      "type" : "SqlSourceSystem",
      "name" : "SnowflakeSourceSystem"
    }
  }

Model the table schema

To model the schema of the Snowflake table in your application, create an External Entity Type with a schema name that case-sensitively matches the name of the table in the Snowflake table.

Start by adding the following Reliability.c3typ file to the \src directory of your package:

Type
entity type Reliability mixes External, NoSystemCols schema name 'PUBLIC.RELIABILITY'

For the RELIABILITY table, the schema name is a qualified name consisting of the schema name and table name separated by a dot. You can use the inferSourceType() method to access the table's C3 AI data types, which the platform infers from the source data types.

JavaScript
var schema = SqlSourceCollection.forName("Reliability").inferSourceType().declaredFieldTypes;

var myObject = {};
for (let i = 0; i < schema.length; i++) {
        schemaName = schema[i].schemaName;
        myObject[schemaName] = schema[i].valueType.name;
    }

myObject Snowflake

Snowflake data types are mapped to C3 AI data types according to the following table:

Snowflake Data TypesC3 AI Data Types
INT, INTEGER, SMALLINT, TINYINTint, int16, int32
BYTEINTbyte
BIGINTbigint
DECIMAL, NUMERIC, NUMBERdecimal
FLOAT, FLOAT4, FLOAT8float
DOUBLE, DOUBLE PRECISION, REALdouble
DATE, TIME, DATETIME, TIMESTAMP, TIMESTAMP_NTZ, TIMESTAMP_LTZ, TIMESTAMP_TZdatetime
BINARY, VARBINARYbinary
BOOLEANboolean
VARCHAR, CHAR, CHARACTER, STRING, TEXTstring
VARIANT, OBJECT, ARRAYjson
GEOGRAPHY, GEOMETRYNot Supported

You can also access the source data types to validate the type inference:

JavaScript
SqlSourceCollection.forName("Reliability").connect().columns;

Source Data Types

Complete the External Entity Type definition:

Type
entity type Reliability mixes External, NoSystemCols schema name "PUBLIC.RELIABILITY" {
    
    @db(dataTypeOverride='datetime')
    id: ~ schema name "TIMESTAMP"

    power: float schema name "ACTIVEPOWER_RESAMPLE_MEAN"

    rotationalSpeed: float schema name "GENERATORROTATIONSPEED_RESAMPLE_MEAN"

    gearOiltemp: float schema name "GEAROILTEMPERATURE_RESAMPLE_MEAN"

}

In the example Reliability Type, the TIMESTAMP column is used as the id field.

Read data from the table

After completing the External Entity Type definition, you can validate the configuration by fetching the External Type data from the Snowflake table:

JavaScript
c3Grid(Reliability.fetch());

Output

See also

Was this page helpful?