How to write java user defined function in Snowflake

How to use strict property in Snowflake UDFs

User Defined Functions in Snowflake Video

You can watch the complete hands on video tutorial

Step-5: Java & SQL Construct

class MyJavaUDF {

    private double x;

    // Constructor
    public MyJavaUDF()  {
        x = Math.random();
    }

    // Handler
    public double myHandler() {
        return x;
    }

    public static void main(String [] args){
        MyJavaUDF mc = new MyJavaUDF();
        System.out.println(mc.myHandler());
    }
}

use role sysadmin;
use database ttips;
use schema udfs;
use warehouse compute_wh;

-- lets list if my jar is available or not
list @~;

create or replace function my_java_udf()
    returns double
    language java
    imports = ('@~/java-udf/MyUDF.jar')
    handler = 'MyJavaUDF.myHandler';
    
desc function my_java_udf();

select my_java_udf(),my_java_udf(),my_java_udf();

select my_java_udf()
union all
select my_java_udf();

show functions like 'MY_JAVA%';
  1. Part-02 User Defined Function in Snowflake SQLs
  2. Part-02 JavaScript & SQL User Defined Function in Snowflake SQLs
  3. Part-03 Strict Property User Defined Function in Snowflake SQLs
  4. Part-04 Not Null Return User Defined Function in Snowflake SQLs
  5. Part-05 Secure User Defined Function in Snowflake SQLs
  6. Part-06 Inbuilt Vs. User Defined Function in Snowflake SQLs
  7. Part-07 Table User Defined Function in Snowflake SQLs
  8. Part-08 Method Overloading SQL Script
  9. Part-09 Java User Defined Function SQL & Java Code