Hive_ several functions to handle NULL NVL, COALESCE, NULLIF

Official documentation:

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

Reference documents:

https://blog.csdn.net/qq_34105362/article/details/80402806

 

       Hive provides a lot of functions, you can list all functions on the command line show functions, you will find that these function names are very similar to mysql, most of them are the same, you can check the function usage by describe function functionName.

 

 

Hive commonly used null value processing functions are NVL (0.11 +), COALESCE, NULLIF (2.3.0 +).

The main function of the null value processing function: 

NVL (0.11), COALESCE complements null data

nullif (a, b) is mainly to determine whether a and b are the same, return null if they are the same, otherwise return a

 

Let ’s take a look at the introduction of these functions.

 

1.COALESCE

T

COALESCE(T v1, T v2, ...)

Returns the first v that is not NULL, or NULL if all v's are NULL.

hive> describe function coalesce;
OK
coalesce(a1, a2, ...) - Returns the first non-null argument
Time taken: 0.028 seconds, Fetched: 1 row(s)

The function of the Coalese function is that the NVL function is a bit similar, and its advantage is that there are more options.
The format is as follows:
Coalesce (expr1, expr2, expr3… .. exprn)
means that you can specify placeholders for multiple expressions. All expressions must be of the same type, or can be implicitly converted to the same type.

 

In actual work, I generally use this method to give a default value to NULL data.

 

 

2. NVL

T nvl(T value, T default_value) Returns default value if value is null else returns value (as of HIve 0.11).
hive> describe function nvl;
OK
nvl(value,default_value) - Returns default value if value is null else returns value
Time taken: 0.019 seconds, Fetched: 1 row(s)

NVL (expr1, expr2):
1. Null value conversion function;
2. Similar to mysql-nullif (expr1, expr2), sqlserver-ifnull (expr1, expr2).

Remarks:
1. If expr1 is NULL, the return value is expr2, otherwise it returns expr1.
2. Applicable to numeric, character and date types, but the data types of expr1 and expr2 must be the same type.
 

3.NULLIF 

T nullif( a, b )

Returns NULL if a=b; otherwise returns a (as of Hive 2.3.0).

Shorthand for: CASE WHEN a = b then NULL else a

 Since I am using a lower version of Hive which does not support this function temporarily, I will not make a demonstration of the Hive console.

The function of NULLIF (exp1, expr2) function is to return NULL if exp1 and exp2 are equal, otherwise it returns the first value.

 

Published 519 original articles · praised 1146 · 2.83 million views

Guess you like

Origin blog.csdn.net/u010003835/article/details/102468899