好做的网站,源码出售网站,wordpress 语种顺序,滴滴一年亏损109亿Spark SQL----CREATE FUNCTION 一、描述二、语法三、参数四、例子 一、描述
CREATE FUNCTION语句用于在Spark中创建临时或永久函数。临时函数的作用域位于会话级别#xff0c;永久函数在持久目录中创建#xff0c;并可用于所有会话。USING子句中指定的资源在第一次执行时可供… Spark SQL----CREATE FUNCTION 一、描述二、语法三、参数四、例子 一、描述
CREATE FUNCTION语句用于在Spark中创建临时或永久函数。临时函数的作用域位于会话级别永久函数在持久目录中创建并可用于所有会话。USING子句中指定的资源在第一次执行时可供所有执行器使用。除了SQL接口之外spark还允许用户使用Scala、Python和Java API创建自定义的用户定义scalar和聚合函数。有关更多信息请参阅Scalar UDFs和UDAFs。
二、语法
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 如果指定则仅在函数不存在时创建该函数。如果指定的函数已存在于系统中则函数的创建将成功不会引发错误。此参数与OR REPLACE互斥不能一起指定。function_name 指定要创建的函数的名称。函数名称可以选择性地用数据库名称限定。 语法[ database_name. ] function_nameclass_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 SimpleUdfUSING 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 test;
---------------------
|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.
-- Please note that the temporary function does not have a qualified
-- database associated with it.
SHOW USER FUNCTIONS;
------------------
| function|
------------------
|default.simple_udf|
| simple_temp_udf|
-------------------- 1. Modify SimpleUdfs 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 SimpleUdfRUSING JAR /tmp/SimpleUdfR.jar;-- Invoke the function. Every selected value should be incremented by 20.
SELECT simple_udf(c1) AS function_return_value FROM test;
---------------------
|function_return_value|
---------------------
| 21|
| 22|
---------------------