0
0
Hadoopdata~20 mins

User-defined functions (UDFs) in Hadoop - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UDF Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple UDF in Hive
Consider this Hive UDF written in Java. What will be the output when the UDF is applied to the input string "hadoop"?
Hadoop
public class ReverseStringUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public String evaluate(String input) {
        if (input == null) return null;
        return new StringBuilder(input).reverse().toString();
    }
}

-- Hive query:
SELECT ReverseStringUDF('hadoop');
A'hadoop'
B'poohad'
C'poodah'
Dnull
Attempts:
2 left
💡 Hint
Think about what the StringBuilder.reverse() method does to the input string.
data_output
intermediate
2:00remaining
Result of applying a UDF to a Hive table column
Given a Hive table users with a column username, and a UDF ToUpperCaseUDF that converts strings to uppercase, what is the output of this query?
Hadoop
SELECT username, ToUpperCaseUDF(username) AS username_upper FROM users WHERE username = 'alice';
A[('alice', 'alice')]
B[]
C[('ALICE', 'ALICE')]
D[('alice', 'ALICE')]
Attempts:
2 left
💡 Hint
The UDF changes the case but does not filter rows.
🔧 Debug
advanced
2:00remaining
Identify the error in this UDF code
This Java UDF code is intended to return the length of a string. What error will it cause when compiled or run?
Hadoop
public class StringLengthUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public int evaluate(String input) {
        return input.length();
    }
}
ANullPointerException if input is null
BSyntaxError due to missing return type
CClassNotFoundException at runtime
DNo error, runs correctly
Attempts:
2 left
💡 Hint
What happens if the input string is null and you call length()?
🧠 Conceptual
advanced
2:00remaining
Understanding UDF registration in Hive
Which statement correctly describes how to register a Java UDF in Hive before using it in queries?
AUse <code>ADD JAR 'path_to_jar'; CREATE FUNCTION function_name AS 'class_name'</code>
BUse <code>REGISTER FUNCTION function_name AS 'class_name'</code>
CUse <code>CREATE FUNCTION function_name AS 'class_name' USING JAR 'path_to_jar'</code>
DNo registration needed; just call the UDF class directly in queries
Attempts:
2 left
💡 Hint
Think about how Hive loads external code and links it to a function name.
🚀 Application
expert
2:00remaining
Predict output of a complex UDF with multiple inputs
Given this UDF that concatenates two strings with a dash, what is the output of the Hive query?
Hadoop
public class ConcatDashUDF extends org.apache.hadoop.hive.ql.exec.UDF {
    public String evaluate(String s1, String s2) {
        if (s1 == null) s1 = "";
        if (s2 == null) s2 = "";
        return s1 + "-" + s2;
    }
}

-- Hive query:
SELECT ConcatDashUDF('data', NULL);
A'data-NULL'
B'data-'
C'-NULL'
Dnull
Attempts:
2 left
💡 Hint
Check how the UDF handles null inputs before concatenation.