This topic describes how to use a Java UDF to obtain the value that corresponds to a specific key in a string that has key/value delimiters.

UDF used to obtain the value of a string that has delimiters

UDFKeyValueEx(String str, String split1, String split2, String keyname)
  • Description: used to obtain the value that corresponds to a specific key in a string. Use string delimiters and key/value delimiters to split a string in sequence and then obtain the value that corresponds to a specific key. Different from UDFKeyValue, this UDF is ideal for values that have key/value delimiters.
  • Parameters:
    • str: the target string.
    • split1: the string delimiter, which is a regular expression. The default value is a semicolon (;).
    • split2: the key/value delimiter, which is not a regular expression. The default value is a colon (:).
    • keyname: the key whose value you want to obtain.

UDF example

  1. Function registration
    After UDFKeyValueEx.java passes the test, register it as a function.
    Note
    • MaxCompute Studio provides the one-click publish function for you to run the mvn clean package command, upload a JAR package, and register the UDF in sequence. You can also upload a JAR package by using DataWorks.
    • You can upload resources by running common commands on the client. For more information, see Resources operations.
  2. Examples
    After the UDF is registered, execute one of the following statements:
    • Example 1
      select UDFKeyValueEx('a:b;c:d', ';', ':', 'a') from dual;
      The result is as follows:
      +-----+
      | _c0 |
      +-----+
      | b   |
      +-----+
    • Example 2
      select UDFKeyValueEx('a:b:c;c:d', ';', ':', 'a:b') from dual;
      The result is as follows:
      +-----+
      | _c0 |
      +-----+
      | c   |
      +-----+

UDF code example

// The package name, which can be defined as needed.
package com.aliyun.odps.examples.udf; 
import com.taobao.bi.odps.udf.UDF;

public class UDFKeyValueEx extends UDF {

    public String evaluate(String str, String keyname) {
                // The default value of split1 is a semicolon (;), and that of split2 is a colon (:).
        return evaluate(str, ";", ":", keyname);
    }

    public String evaluate(String str, String split1, String split2, String keyname) {
        try {
            if (str==null || split1==null || split2==null || keyname==null){
                return null;
            }
                        // Use a key/value delimiter.
            String keySplit = keyname+split2;
                        // Traverse the string.
            for (String subStr : str.split(split1)){
                if (subStr.startsWith(keySplit)){
                    if (keySplit.length() < subStr.length()){
                                                // Return the result.
                        return subStr.substring(keySplit.length());
                    }else{
                        return null;
                    }
                }
            }

            // There is no value that corresponds to the key.
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}