You need to enable JavaScript to run this app.
导航

如何在 LAS 中使用UDF功能

最近更新时间2023.07.18 20:40:24

首次发布时间2023.07.18 20:40:24

前言

LAS (LakeHouse Analytics Service) 是 Serverless 湖仓一体分析服务 ,提供多模引擎,完全兼容开源 Spark、Presto、Flink 生态,具备弹性计算、海量存储、数据实时更新、统一 SQL、批流合一优势,与大数据开发治理套件对接,支持数据仓库/集市/数据湖分析能力,帮助企业构建云原生实时湖仓分析平台[1]。

在本教程中,我们将向您展示如何使用 LAS 中的 UDF 功能。

关于实验

预计部署时间:20分钟
级别:初级
相关产品:LAS (LakeHouse Analytics Service)
受众: 通用

环境说明
  1. 如果还没有火山引擎账号,点击此链接注册账号。

  2. 如果您还没有开通 LAS,点击此链接进行开通

实验步骤

1. 相关代码如下

  1. 实现功能如下:
package org.example;

import org.apache.hadoop.hive.ql.exec.UDF;

public class GetLength2 extends UDF {
    public GetLength2() {
    }

    public int evaluate(String str) {
        try {
            return str.length();
        } catch (Exception var3) {
            return -1;
        }
    }
}

  1. 引入相关依赖如下:
<?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>udf_test</artifactId>
  <version>1.0-SNAPSHOT</version>

  <!-- 声明并引入,所有子模块都会自动引入该依赖-->
  <dependencies>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>3.1.3</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>

</project>

2. 将代码打成 jar 包

3. 上传 Jar 包

请您导航至 LAS 控制台,点击 数据管理资源包管理,然后点击 创建资源包

alt

然后指定相关的数据库,资源包名称,然后从本地上传到 LAS,最后点击 确定 按钮

alt

3. 创建 UDF

在 LAS 控制台中,找到查询分析,新建查询,运行如下语句:

CREATE FUNCTION rudonx_las_db.rudonx_udf_new_1 AS 'org.example.GetLength2' using jar 'rudonx_las_db.rudonx_new_utf';

经过此步骤,一个 UDF 函数就被创建成功,下面我们可以进行相关查询

4. 进行查询分析

进行分析的 SQL 语句如下:

select rudonx_las_db.rudonx_udf_new_1("ssss")

查询结果如下所示,字符串 "ssss" 共有四个字符。
alt

参考文档

[1] https://www.volcengine.com/docs/6492/72765

[2] https://www.volcengine.com/docs/6492/101913