基于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
nullfor 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-xxxxxxxxxxxxpattern.
Step 2: Compile and Package
- Compile the class using Hive and Hadoop dependencies (you can use Maven or a javac command with the correct classpath).
- 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
- Add the JAR to Hive:
ADD JAR /path/to/your/base64-luuid-udf.jar; - Create a Temporary Function:
ReplaceCREATE TEMPORARY FUNCTION base64_to_luuid AS 'com.your.package.Base64ToLUUID';com.your.packagewith the actual package name of your UDF class. - 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
相关产品推荐
相关产品推荐

