You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Java的Hive 1.0 UDF开发:二进制转指定格式十六进制

Hive 1.0 UDF to Convert Base64 Binary to LUUID-style Hex String

I’ve built exactly what you need—a Java UDF for Hive 1.0 that turns Base64-encoded 16-byte binary strings into the hyphenated hex format matching MongoDB’s LUUID. Here’s a step-by-step breakdown of the solution:

Step 1: UDF Implementation

Create a Java class extending Hive’s UDF class. It handles Base64 decoding, validates the byte length, and formats the hex string to your specified pattern.

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class Base64ToLUUID extends UDF {

    public String evaluate(String base64Input) {
        // Handle null or empty input gracefully
        if (base64Input == null || base64Input.trim().isEmpty()) {
            return null;
        }

        byte[] decodedBytes;
        try {
            // Decode the Base64 string to raw bytes
            decodedBytes = Base64.decodeBase64(base64Input);
        } catch (IllegalArgumentException e) {
            // Return null for invalid Base64 instead of failing the entire query
            return null;
        }

        // Ensure we have exactly 16 bytes (standard UUID length)
        if (decodedBytes.length != 16) {
            return null;
        }

        // Convert bytes to lowercase hex string
        String hexString = Hex.encodeHexString(decodedBytes);

        // Format into the 8-4-4-4-12 hyphenated pattern
        return String.format("%s-%s-%s-%s-%s",
                hexString.substring(0, 8),
                hexString.substring(8, 12),
                hexString.substring(12, 16),
                hexString.substring(16, 20),
                hexString.substring(20));
    }
}

Key Notes:

  • Null/Invalid Input Handling: Returns null for empty, null, or invalid Base64 inputs to avoid query failures (adjust to throw exceptions if you need strict validation).
  • Byte Length Check: Ensures the decoded data is exactly 16 bytes, which is required for the UUID-style output format.
  • Hex Formatting: Uses lowercase hex (matching your example) and inserts hyphens at the correct positions to produce the xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx pattern.

Step 2: Compile and Package

  1. Compile the class using Hive and Hadoop dependencies (you can use Maven or a javac command with the correct classpath).
  2. Package the compiled class into a JAR file (e.g., base64-luuid-udf.jar). You don’t need to include Apache Commons Codec in the JAR—Hive already includes it in its default classpath.

Step 3: Deploy and Use in Hive

  1. Add the JAR to Hive:
    ADD JAR /path/to/your/base64-luuid-udf.jar;
    
  2. Create a Temporary Function:
    CREATE TEMPORARY FUNCTION base64_to_luuid AS 'com.your.package.Base64ToLUUID';
    
    Replace com.your.package with the actual package name of your UDF class.
  3. Use the Function in Queries:
    SELECT 
        your_base64_column,
        base64_to_luuid(your_base64_column) AS luuid_format
    FROM your_table;
    

Example Output:

For input AAGOBh3pqkWvtaK8AO2U9w==, the function returns:
00018e06-1de9-aa45-afb5-a2bc00ed94f7

Content of the question is from Stack Exchange, asked by Suraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:20:36