CREATE FUNCTION(외부)

적용 대상:검사 '예'로 표시 Databricks 런타임

임시 또는 영구 외부 함수를 만듭니다. 임시 함수는 영구 카탈로그에서 영구 함수가 만들어지고 모든 세션에서 사용할 수 있는 세션 수준에서 범위가 지정됩니다. USING 절에 지정된 리소스는 처음으로 실행될 때 모든 실행기에서 사용할 수 있습니다.

SQL 인터페이스 외에도 Spark를 사용하면 Scala, Python 및 Java API를 사용하여 사용자 지정 사용자 정의 스칼라 및 집계 함수를 만들 수 있습니다. 자세한 내용은 UDF(외부 사용자 정의 스칼라 함수)UDF(사용자 정의 집계 함수)를 참조하세요.

구문

CREATE [ OR REPLACE ] [ TEMPORARY ] FUNCTION [ IF NOT EXISTS ]
    function_name AS class_name [ resource_locations ]

매개 변수

  • OR REPLACE

    지정된 경우 함수에 대한 리소스가 다시 로드됩니다. 이는 주로 함수 구현에 대한 변경 내용을 선택하는 데 유용합니다. 이 매개 변수는 IF NOT EXISTS와 양립할 수 없으며 함께 지정할 수 없습니다.

  • TEMPORARY

    생성되는 함수의 범위를 나타냅니다. TEMPORARY가 지정되면 만들어진 함수가 유효하고 현재 세션에서 볼 수 있습니다. 이러한 종류의 함수에 대해 카탈로그에 영구 항목이 만들어지지 않습니다.

  • IF NOT EXISTS

    지정하면 존재하지 않는 경우에만 함수를 만듭니다. 지정된 함수가 시스템에 이미 있는 경우 함수 만들기가 성공합니다(오류가 throw되지 않음). 이 매개 변수는 OR REPLACE와 양립할 수 없으며 함께 지정할 수 없습니다.

  • function_name

    함수의 이름입니다. 함수 이름은 선택적으로 스키마 이름으로 규정될 수 있습니다.

    생성된 hive_metastore 함수는 영숫자 ASCII 문자와 밑줄만 포함할 수 있습니다.

  • class_name

    생성할 함수에 대한 구현을 제공하는 클래스의 이름입니다. 구현 클래스는 다음과 같이 기본 클래스 중 하나를 확장해야 합니다.

    • org.apache.hadoop.hive.ql.exec 패키지에서 UDF 또는 UDAF를 확장해야 합니다.
    • org.apache.hadoop.hive.ql.udf.generic 패키지에서 AbstractGenericUDAFResolver, GenericUDF 또는 GenericUDTF를 확장해야 합니다.
    • org.apache.spark.sql.expressions 패키지에서 UserDefinedAggregateFunction을 확장해야 합니다.
  • resource_locations

    해당 종속성과 함께 함수의 구현을 포함하는 리소스 목록입니다.

    구문:USING { { (JAR | FILE | ARCHIVE) resource_uri } , ... }

예제

-- 1. Create a simple UDF `SimpleUdf` that increments the supplied integral value by 10.
--    import org.apache.hadoop.hive.ql.exec.UDF;
--    public class SimpleUdf extends UDF {
--      public int evaluate(int value) {
--        return value + 10;
--      }
--    }
-- 2. Compile and place it in a JAR file called `SimpleUdf.jar` in /tmp.

-- Create a table called `test` and insert two rows.
> CREATE TABLE test(c1 INT);
> INSERT INTO test VALUES (1), (2);

-- Create a permanent function called `simple_udf`.
> CREATE FUNCTION simple_udf AS 'SimpleUdf'
    USING JAR '/tmp/SimpleUdf.jar';

-- Verify that the function is in the registry.
> SHOW USER FUNCTIONS;
           function
 ------------------
 default.simple_udf

-- Invoke the function. Every selected value should be incremented by 10.
> SELECT simple_udf(c1) AS function_return_value FROM t1;
 function_return_value
 ---------------------
                    11
                    12

-- Created a temporary function.
> CREATE TEMPORARY FUNCTION simple_temp_udf AS 'SimpleUdf'
    USING JAR '/tmp/SimpleUdf.jar';

-- Verify that the newly created temporary function is in the registry.
-- The temporary function does not have a qualified
-- schema associated with it.
> SHOW USER FUNCTIONS;
           function
 ------------------
 default.simple_udf
    simple_temp_udf

-- 1. Modify `SimpleUdf`'s implementation to add supplied integral value by 20.
--    import org.apache.hadoop.hive.ql.exec.UDF;

--    public class SimpleUdfR extends UDF {
--      public int evaluate(int value) {
--        return value + 20;
--      }
--    }
-- 2. Compile and place it in a jar file called `SimpleUdfR.jar` in /tmp.

-- Replace the implementation of `simple_udf`
> CREATE OR REPLACE FUNCTION simple_udf AS 'SimpleUdfR'
    USING JAR '/tmp/SimpleUdfR.jar';

-- Invoke the function. Every selected value should be incremented by 20.
> SELECT simple_udf(c1) AS function_return_value FROM t1;
 function_return_value
 ---------------------
                    21
                    22