Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The correct method to get the length of a string in Java is .length() with parentheses.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the class name without package.
Using the function name instead of the class path.
✗ Incorrect
The full class path including package is needed to register the UDF in Hive.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or -1 which may be misleading as length.
Throwing an exception which breaks query execution.
✗ Incorrect
Returning null for null input is a common safe practice in UDFs to avoid errors.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning empty string instead of null.
Using .toLowerCase() instead of .toUpperCase().
✗ Incorrect
Return null if input is null, and use .toUpperCase() to convert string to uppercase.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 0 instead of .length() <= 0.
Returning empty string instead of null.
Using substring with wrong indexes.
✗ Incorrect
Check if length is 0 or less, return null, else return substring of first character.