MaxCompute开发UDF和UDTF案例

article/2025/6/7 23:20:07

文章目录

  • 一、Java开发UDF
    • 1、创建Maven项目
    • 2、创建UDF类
    • 3、打包上传资源
    • 4、创建函数MyUDF
    • 5、SQL验证
  • 二、Java开发UDTF
    • 1、创建Maven项目
    • 2、创建UDTF类
    • 3、打包上传更新资源
    • 4、创建函数MyUDTF
    • 5、SQL验证
  • 三、常见问题
    • 1、发布函数报错

一、Java开发UDF

1、创建Maven项目

创建Maven项目,名称为 MaxComputeUDF 配置pom文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>MaxComputeUDF</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.11</version></dependency><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.7.0</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.62</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-core</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-udf</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-udf-local</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-mapred</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-mapred-local</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-graph</artifactId><version>${sdk.version}</version></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-graph-local</artifactId><version>${sdk.version}</version></dependency></dependencies><properties><sdk.version>0.38.3-public</sdk.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><build><plugins><!-- Scala Compiler --><plugin><groupId>net.alchim31.maven</groupId><artifactId>scala-maven-plugin</artifactId><version>3.2.2</version><executions><execution><id>scala-compile-first</id><phase>process-resources</phase><goals><goal>compile</goal></goals></execution><execution><id>scala-test-compile</id><phase>process-test-resources</phase><goals><goal>testCompile</goal></goals></execution></executions><configuration><args><arg>-nobootcp</arg></args></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.0.0</version><executions><!-- Run shade goal on package phase --><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><excludes><exclude>org.apache.flink:force-shading</exclude><exclude>com.google.code.findbugs:jsr305</exclude><!--                                    <exclude>org.slf4j:*</exclude>--><!--                                    <exclude>log4j:*</exclude>--></excludes></artifactSet><filters><filter><!-- Do not copy the signatures in the META-INF folder.Otherwise, this might cause SecurityExceptions when using the JAR. --><artifact>*:*</artifact><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*.RSA</exclude></excludes></filter></filters><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass></mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build>
</project>

2、创建UDF类

逻辑简单,仅供测试使用

package com.aliyun;import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.udf.annotation.Resolve;@Resolve({"string->string"})
public final class MyUDF extends UDF {public String evaluate(String s) {if (s == null) {return null;}return s.toLowerCase();}
}

3、打包上传资源

在Dataworks数据开发页面上传资源并发布
在这里插入图片描述

4、创建函数MyUDF

配置如下
在这里插入图片描述

5、SQL验证

--函数名不区分大小写
SELECT myudf("HELLO")

在这里插入图片描述

二、Java开发UDTF

1、创建Maven项目

上面已创建项目,pom文件也配置好了,直接跳过该步骤

2、创建UDTF类

package com.aliyun;import com.aliyun.odps.udf.ExecutionContext;
import com.aliyun.odps.udf.UDFException;
import com.aliyun.odps.udf.UDTF;
import com.aliyun.odps.udf.annotation.Resolve;
import com.alibaba.fastjson.*;@Resolve({"string->bigint,string,string"})
public class MyUDTF extends UDTF {@Overridepublic void process(Object[] args) throws UDFException {String event = (String) args[0];JSONArray jsonArray = JSON.parseArray(event);for (int i = 0; i < jsonArray.size(); i++) {JSONObject jsonObject = jsonArray.getJSONObject(i);String ett = (String) jsonObject.getString("aa");String eventName = (String) jsonObject.getString("bb");String eventJson = (String) jsonObject.getString("cc");forward(Long.parseLong(ett), eventName, eventJson);}}
}

在这里插入图片描述

3、打包上传更新资源

打包后直接更新第一次上传的jar包,重新发布
在这里插入图片描述

4、创建函数MyUDTF

配置如下
在这里插入图片描述

5、SQL验证

测试数据如下

{"a": "","b": "app","key": [{"aa": "1","bb": "张三","cc": {"age": "21","des": "工人"}},{"aa": "2","bb": "李四","cc": {"age": 24,"des": "大学生"}},{"aa": "3","bb": "王五","cc": {"age": "33","des": "老师"}}]
}

测试SQL如下

SELECT  MyUDTF(GET_JSON_OBJECT('{"a": "","b": "app","key": [{"aa": "1","bb": "张三","cc": {"age": "21","des": "工人"}},{"aa": "2","bb": "李四","cc": {"age": 24,"des": "大学生"}},{"aa": "3","bb": "王五","cc": {"age": "33","des": "老师"}}]
}','$.key')
) AS (id,name,jsonvalue)
;

测试结果如下
在这里插入图片描述

三、常见问题

1、发布函数报错

失败原因:Fail to add or update function MyUDF. Error message is MaxCompute exception happened. ErrorCode: InvalidParameter, ErrorMessage: ODPS-0421111: Resource not found - ‘maxcomputeudf.jar’.
在这里插入图片描述
问题原因: 引用的资源没有发布,导致找不到资源
解决方案: 先发布引用的资源, 再重试发布函数即可,这里注意未发布的资源会有上传的符号
在这里插入图片描述


http://www.hkcw.cn/article/MWHLOehCsJ.shtml

相关文章

ROS2学习(17)------ROS 2 Gazebo 三维物理仿真平台简介及举例使用

操作系统&#xff1a;ubuntu22.04 IDE:Visual Studio Code 编程语言&#xff1a;C11 ROS版本&#xff1a;2 ROS 2 Gazebo 三维物理仿真平台简介 Gazebo 是一个强大的三维机器人仿真环境&#xff0c;它能够模拟复杂的机器人系统和环境。结合 ROS 2&#xff0c;你可以使用 Gaze…

定时通知群内值班人功能

from app.external.zhiban import default_zhiban_api_client import requests import json from datetime import datetimedef send_daily_reminder():# app_map [# {"name": "平台-存储云平台服务号", "type": "app"},# {&…

现代密码学 | 椭圆曲线密码学—附py代码

Elliptic Curve Cryptography 椭圆曲线密码学&#xff08;ECC&#xff09;是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础&#xff0c;例如椭圆曲线数字签…

04 APP 自动化- Appium toast 元素定位列表滑动

文章目录 一、toast 元素的定位二、滑屏操作 一、toast 元素的定位 toast 元素就是简易的消息提示框&#xff0c;toast 显示窗口显示的时间有限&#xff0c;一般3秒左右 # -*- codingutf-8 -*- from time import sleep from appium import webdriver from appium.options.an…

C++ -- 继承

继承 1. 继承的概念及定义1.1 概念1.2 继承定义 1.2.1 格式1.2.2 继承基类成员访问方式的变化2. 基类和派生类对象赋值转换3. 继承中的作用域4. 派生类的默认成员函数5. 继承与友元6. 继承与静态成员7. 菱形继承7.1 单继承7.2 多继承7.3 菱形继承7.4 虚拟继承virtual 1. 继承的…

K8S上使用helm部署 Prometheus + Grafana

一、使用 Helm 安装 Prometheus 1. 配置源 地址&#xff1a;prometheus 27.19.0 prometheus/prometheus-community # 添加repo $ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts "prometheus-community" has been added…

湖北理元理律师事务所:系统性债务化解中的法律技术革新

一、打破债务困局的核心&#xff1a;精准责任切割 传统债务处理常陷入"全额偿还"或"逃废债"的二元对立。法律视角下的解决方案在于&#xff1a; 通过法定程序分离三类债务&#xff1a; 无效债务&#xff1a;年利率超LPR四倍部分&#xff08;《民法典》第…

Android Studio 向模拟器手机添加照片、视频、音乐

Android Studio 向模拟器手机添加照片、视频、音乐(其实都是一样的&#xff0c;只是添加到不同的文件夹&#xff09;&#xff0c;例如我们在很多程序中功能例如&#xff1a;选择头像&#xff0c;跳转到手机相册选择头像&#xff0c;此时相册为空&#xff0c;即模拟器没有图片资…

vscode使用“EIDE”和“Cortex-Debug”插件利用st-link插件实现程序烧写以及调试工作

第一步&#xff1a;安装vscode插件“EIDE”EIDE和“Cortex-Debug”。 第二步&#xff1a;配置EIDE 2.1安装“实用工具”&#xff1a; 2.2 EIDE插件配置&#xff1a;根据安装的keil C51 keil MDK IAR的相关路径设置 第三步&#xff1a;配置Cortex-Debug插件 点击settings.jso…

详解开漏输出和推挽输出

开漏输出和推挽输出 以上是 GPIO 配置为输出时的内部示意图&#xff0c;我们要关注的其实就是这两个 MOS 管的开关状态&#xff0c;可以组合出四种状态&#xff1a; 两个 MOS 管都关闭时&#xff0c;输出处于一个浮空状态&#xff0c;此时他对其他点的电阻是无穷大的&#xff…

问界M9五座零重力座椅版实车亮相 智慧出行新体验

5月31日,2025粤港澳大湾区国际汽车博览会正式开幕,问界携全系车型亮相,并举办了“问界M9大五座零重力座椅版交付仪式暨品牌挚友发布会”。赛力斯汽车总裁何利扬向五位车主交付了问界M9 2025款大五座零重力座椅版的钥匙,并宣布演员白敬亭成为问界品牌挚友及问界M8车主。何利…

【Linux】线程互斥

&#x1f4dd;前言&#xff1a; 这篇文章我们来讲讲Linux——线程互斥 &#x1f3ac;个人简介&#xff1a;努力学习ing &#x1f4cb;个人专栏&#xff1a;Linux &#x1f380;CSDN主页 愚润求学 &#x1f304;其他专栏&#xff1a;C学习笔记&#xff0c;C语言入门基础&#xf…

「OC」初识runloop

「OC」初识runloop 简介 iOS中的RunLoop&#xff08;运行循环&#xff09;是事件处理的核心机制&#xff0c;负责管理线程的生命周期、事件调度及资源优化。其核心作用是通过循环处理输入事件、定时器任务和观察者回调&#xff0c;保持线程活跃且高效运行。 runloop的作用 R…

python学习打卡day43

DAY 43 复习日 作业&#xff1a; kaggle找到一个图像数据集&#xff0c;用cnn网络进行训练并且用grad-cam做可视化 浙大疏锦行 数据集使用猫狗数据集&#xff0c;训练集中包含猫图像4000张、狗图像4005张。测试集包含猫图像1012张&#xff0c;狗图像1013张。以下是数据集的下…

【AI学习从零至壹】基于深度学习的⽂本分类任务

基于深度学习的⽂本分类任务 文本分类任务的实现思路⽂本预处理文本分词Jieba分词文本分词器SentencePiece训练步骤⽂本结构化转换 语料转换为训练样本 文本分类任务的实现思路 ⽂本分类就是⼀个将⽂本分配到预定义类别的⼀个过程 整体流程包括&#xff1a; ⽂本语料的获取和…

sourcetree中的mercurial有什么用

1、安装SourceTree的过程中&#xff0c;有一个选项就是mercurial&#xff0c;&#xff0c;一直没搞明白他是干什么用的&#xff0c;直到今天 2、ai登场 3、总结 此软件无用&#xff0c;不需要安装

【Linux】linux基础指令

目录 管理用户相关useraddpaaswduserdelLinux中的用户文件结构 ls-aLinux目录中的.和..是什么&#xff1f; -l-d-FLinux指令使用多个选项 pwdcd绝对路径与相对路径 touchmkdir-p rmdir-p rm-r-i-f mancpmvecho输出重定向和追加重定向 cat-b-n-s moreless-N-i headtail管道文件搭…

Linux中shell介绍

一、脚本实践 脚本示例1 -- 直接编辑并创建一个文件 vim bak.sh-- 写入下面这句话 # 获取ip地址信息 ifconfig ens33 | grep -w inet | awk {print $2} | xargs echo "IP: "-- 运行bak文件 bash bak.sh或者-- 添加可执行权限 chmod ax bak.sh./bak.sh或者source ba…

【智能制造】精读57页智慧工厂MES 项目解决方案【附全文阅读】

本文概述了智慧工厂MES项目解决方案在工业4.0背景下的整体框架与应用。智慧工厂以企业管理运营中心为核心&#xff0c;融合战略绩效、集团管控、决策分析及大数据分析平台&#xff0c;实现C2M&#xff08;Consumer to Manufacturer&#xff09;个性化订单处理。通过信息化系统平…

Stable Diffusion 技术原理解析与代码实践

1. 引言 Stable Diffusion 是由 Stability AI 开发的开源文本到图像生成模型,自 2022 年发布以来在创意产业和研究领域引起了广泛关注。它基于潜在扩散模型架构,能够根据文本描述生成高质量的图像内容,为艺术创作、设计和内容生成提供了强大工具。 2. 技术原理详解 2.1 扩…