文档

UDF示例:获取URL中指定位置的字符

更新时间:

本文为您介绍如何分别通过Java UDF和Python UDF实现获取URL中指定位置的字符。

命令说明

本示例将注册一个名称为UDF_GET_URL_CHAR的自定义函数,下面对命令格式和入参进行说明。

string UDF_GET_URL_CHAR(string <url>, bigint <n>)
  • 函数功能:该函数接收一个URL字符串和一个整数参数n,先使用/分割URL后获得离.htm最近的子字符串,再使用特殊字符“-”分割子字符串,最后从右到左返回指定位置n的字符。

  • 参数说明:

    • url:url字符串,STRING类型,必填。

    • n:指定的位置,BIGINT类型,必填。

开发和使用步骤

1. 代码开发

Java UDF 代码示例

package com.aliyun; //package名称,可以根据您的情况定义
import com.aliyun.odps.udf.UDF;

public class GetUrlChar extends UDF {
    public String evaluate(String url, Long n)  {
        if (n == 0) {
            return "";
        }
        try {
            // 获取url字符串中第一次出现.htm时.的索引位置,数值类型为int。
            int index = url.indexOf(".htm");
            if (index < 0)  {
                return "";
            }
            // 从0开始,其中不包括index位置的字符。
            String a = url.substring(0, index);
            // 返回指定字符/在此字符串中最后一次出现处的索引。
            index = a.lastIndexOf("/");
            // 取得的字符串长度为:a.length() - (index  +  1)
            String b = a.substring(index  +  1);
            // 用"-"拆分字符串b,获取字符串数组,如果需要使用其他分割符,只需修改b.split中指定的字符
            String[] c = b.split("-");
            // 此处c.length>=n时,才有返回值。
            if (c.length  <  n)  {
                return  "";
            }
            // 返回字符串数组中指定下标的字符。
            return c[c.length - n.intValue()];
        } catch (Exception e)  {
            return  "Internal error";
        }
    }
}

使用Java语言编写UDF代码必须继承UDF类,本例中evaluate方法定义了string类型和bigint类型的入参和string类型的返回值,输入参数和返回值的数据类型将作为SQL语句中UDF的函数签名Signature,其他代码规范和要求请参考:UDF开发规范与通用流程(Java)

Python3 UDF 代码示例

from odps.udf import annotate


@annotate("string,bigint->string")
class GetUrlChar(object):

    def evaluate(self, url, n):
        if n == 0:
            return ""
        try:
            index = url.find(".htm")
            if index < 0:
                return ""
            a = url[:index]
            index = a.rfind("/")
            b = a[index + 1:]
            c = b.split("-")
            if len(c) < n:
                return ""
            return c[-n]
        except Exception:
            return "Internal error"

MaxCompute默认使用Python 2,可以在Session级别使用命令set odps.sql.python.version=cp37开启Python 3。更多python3 UDF规范请参考:UDF开发规范与通用流程(Python3)

Python2 UDF 代码示例

#coding:utf-8
from odps.udf import annotate


@annotate("string,bigint->string")
class GetUrlChar(object):

    def evaluate(self, url, n):
        if n == 0:
            return ""
        try:
            index = url.find(".htm")
            if index < 0:
                return ""
            a = url[:index]
            index = a.rfind("/")
            b = a[index + 1:]
            c = b.split("-")
            if len(c) < n:
                return ""
            return c[-n]
        except Exception:
            return "Internal error"

当Python 2代码中出现中文字符时,运行程序会报错,必须在代码头部增加编码声明。固定声明格式为#coding:utf-8# -*- coding: utf-8 -*-,二者等效。更多python2 UDF规范请参考:UDF开发规范与通用流程(Python2)

2. 上传资源和注册函数

完成UDF代码开发和调试之后,将资源上传至MaxCompute并注册函数,本示例注册函数名:UDF_GET_URL_CHAR。Java UDF上传资源与注册函数详情步骤请参见:打包、上传及注册,Python UDF请参见:上传及注册

3. 使用示例

成功注册UDF后,执行以下命令:

  • 示例一

    set odps.sql.python.version=cp37; -- python3 UDF需要使用该命令开启python3
    SELECT UDF_GET_URL_CHAR("http://www.taobao.com", 1);

    执行结果如下:

    +-----+
    | _c0 |
    +-----+
    |     |
    +-----+
    说明

    这里的返回结果不为NULL。

  • 示例二

    select UDF_GET_URL_CHAR("http://www.taobao.com/a.htm", 1);

    运行结果如下:

    +-----+
    | _c0 |
    +-----+
    | a   |
    +-----+
  • 示例三

    select UDF_GET_URL_CHAR("http://www.taobao.com/a-b-c-d.htm", 3);

    运行结果如下:

    +-----+
    | _c0 |
    +-----+
    | b   |
    +-----+

  • 本页导读 (1)
文档反馈