0
0
Hadoopdata~10 mins

User-defined functions (UDFs) in Hadoop - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple UDF that returns the length of a string.

Hadoop
public class StringLengthUDF extends UDF {
    public int evaluate(String input) {
        return input[1];
    }
}
Drag options to blanks, or click blank then click option'
A.length()
B.size()
C.count()
D.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using .length without parentheses, which is a property in arrays but not in String.
Using .size() or .count(), which are not valid String methods.
2fill in blank
medium

Complete the code to register the UDF in Hive using the correct syntax.

Hadoop
CREATE TEMPORARY FUNCTION string_len AS '[1]';
Drag options to blanks, or click blank then click option'
Astring_len
BStringLengthUDF
Ccom.example.StringLengthUDF
Dcom.example.udf.StringLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the class name without package.
Using the function name instead of the class path.
3fill in blank
hard

Fix the error in the UDF evaluate method to handle null input safely.

Hadoop
public Integer evaluate(String input) {
    if (input == null) {
        return [1];
    }
    return input.length();
}
Drag options to blanks, or click blank then click option'
Anull
B0
C-1
Dthrow new IllegalArgumentException()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or -1 which may be misleading as length.
Throwing an exception which breaks query execution.
4fill in blank
hard

Fill both blanks to create a UDF that converts input string to uppercase safely.

Hadoop
public class ToUpperUDF extends UDF {
    public String evaluate(String input) {
        if (input == null) {
            return [1];
        }
        return input[2];
    }
}
Drag options to blanks, or click blank then click option'
Anull
B""
C.toUpperCase()
D.toLowerCase()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning empty string instead of null.
Using .toLowerCase() instead of .toUpperCase().
5fill in blank
hard

Fill all three blanks to create a UDF that returns the first character of a string or null if empty.

Hadoop
public class FirstCharUDF extends UDF {
    public String evaluate(String input) {
        if (input == null || input[1] 0) {
            return [2];
        }
        return input[3];
    }
}
Drag options to blanks, or click blank then click option'
A.length() <=
Bnull
C.substring(0, 1)
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 0 instead of .length() <= 0.
Returning empty string instead of null.
Using substring with wrong indexes.