# Generate Snowflake UDF Scoring Code
URL: https://docs.datarobot.com/en/docs/classic-ui/integrations/snowflake/snowflake-sc.html

> Use the DataRobot Scoring Code JAR as a user-defined function (UDF) on Snowflake.

# Generate Snowflake UDF Scoring Code

> [!NOTE] Managed DataRobot installations
> The information on this page is intended for Self-Managed DataRobot installations. For DataRobot-managed Snowflake prediction environments, see [Automated deployment and replacement of Scoring Code in Snowflake](https://docs.datarobot.com/en/docs/workbench/nxt-console/nxt-prediction-environments/nxt-prediction-environment-integrations/nxt-snowflake-pred-env-integration.html).

Scoring Code makes it easy for you to perform predictions on DataRobot models anywhere you want by exporting a model to a Java JAR file. Snowflake user-defined functions (UDFs) allow you to execute arbitrary Java code on Snowflake. DataRobot provides an API to Scoring Code for you to use Scoring Code as a UDF on Snowflake without writing any additional code.

To download and execute scoring code with Snowflake UDFs, you must meet the following prerequisites:

- Prepare a DataRobot model that supports Scoring Code for deployment andcreate a model packagewith it.
- Register your Snowflake prediction environment to use with the model.

## Access Scoring Code

When you have your model package and prediction environment prepared in DataRobot, you can deploy the model in order to access the Scoring Code for use with Snowflake.

1. Navigate to theModel Registryand select your model package. On theDeploymentstab, selectCreate new deployment.
2. Complete the fields andconfigure the deploymentas desired. To generate Snowflake UDF Scoring Code, specify your Snowflake prediction environment under theInferenceheader.
3. Once fully configured, selectCreate deploymentat the top of the screen.
4. After deploying the model, access the deployment from the inventory and navigate to thePredictions > Portable Predictionstab. This is where DataRobot hosts the Scoring Code for the model.
5. (Optional) Toggle onInclude prediction explanationsto include prediction explanations with the prediction results, then clickDownload. The Scoring Code JAR file appears in your browser bar.
6. When the Scoring Code download completes, copy the installation script and update it with your Snowflake warehouse, database, schema, and the path to the Scoring Code JAR file, then execute the script. Copy and paste (for regression)-- Replace with the warehouse to use
USE WAREHOUSE my_warehouse;
-- Replace with the database to use
USE DATABASE my_database;

-- Replace with the schema to use
CREATE SCHEMA IF NOT EXISTS scoring_code_udf_schema;
USE SCHEMA scoring_code_udf_schema;

-- Update this path to match the Scoring Code JAR location
PUT 'file:///path/to/downloaded_scoring_code.jar' '@~/jars/' AUTO_COMPRESS=FALSE;

-- Create the UDF
CREATE OR REPLACE FUNCTION datarobot_udf(RowValue OBJECT)
    RETURNS FLOAT
    LANGUAGE JAVA
    IMPORTS=('@~/jars/downloaded_scoring_code.jar')
    HANDLER='com.datarobot.prediction.simple.RegressionPredictor.score'; Copy and paste (for classification)-- Replace with the warehouse to use
USE WAREHOUSE my_warehouse;
-- Replace with the database to use
USE DATABASE my_database;

-- Replace with the schema to use
CREATE SCHEMA IF NOT EXISTS scoring_code_udf_schema;
USE SCHEMA scoring_code_udf_schema;

-- Update this path to match the Scoring Code JAR location
PUT 'file:///path/to/downloaded_scoring_code.jar' '@~/jars/' AUTO_COMPRESS=FALSE;

-- Create the UDF
CREATE OR REPLACE FUNCTION datarobot_udf(RowValue OBJECT)
    RETURNS OBJECT
    LANGUAGE JAVA
    IMPORTS=('@~/jars/downloaded_scoring_code.jar')
    HANDLER='com.datarobot.prediction.simple.ClassificationPredictor.score'; The script uploads the JAR file to a Snowflake stage and creates a UDF for making predictions with Scoring Code. NoteTo run these scripts, youmustuse the SnowSQL command provided in the next step (7). You can't execute these scripts in Snowflake's UI.
7. Execute the script in SnowSQL by providing your credentials and the script location: Copy and pastesnowsql --accountname $ACCOUNT_NAME --username $USERNAME --filename $SCRIPT_PATH
8. With your UDF successfully created, you can now use Snowflake to score data. Use the UDF in any manner supported by SQL. Copy and paste/*
Scoring your data

The Scoring Code UDF accepts rows of data as objects. The OBJECT_CONSTRUCT_KEEP_NULL method can be used to turn a table row into an object.
*/

-- Scoring without specifying columns. Data can contain nulls
SELECT my_datarobot_model(OBJECT_CONSTRUCT_KEEP_NULL(*)) FROM source_table;
9. After scoring data, you canupload actuals via the Settings tabin order to enable accuracy monitoring and more.

## Feature Considerations

Consider the following when using Scoring Code as a UDF on Snowflake.

- Keras models cannot be executed in Snowflake.
- Time series Scoring Code is not supported for Snowflake.
- Scoring Code JARs created prior to the release of the Snowflake Scoring Code feature cannot be run in Snowflake.
- To retrieve Prediction Explanations for Snowflake UDF Scoring Code, first toggle onInclude Prediction Explanationsin the deployment'sPortable Predictionstab. This tab includes code snipppets you can use to retrieve Prediction Explanations. You can use thescoreWithExplanations()method and customize the number of explanations that are returned. For example, configure the function like so:scoreWithExplanations(Map<String, Object> row, Integer maxCodes, Double thresholdLow, Double thresholdHigh)

---
