What's the best way to write if/else if/else if/else in HIVE?

You can use Hive Conditional CASE WHEN function for if-else scenario. The CASE Statement will provide you better readability with the same functionality.

CASE
  WHEN (condition1) THEN result1
  WHEN (condition2) THEN result2
  WHEN (condition3) THEN result3 
  WHEN (condition4) THEN result4
  ELSE result_default 
END AS attribute_name

You can easily achieve this using CASE WHEN statements.

CASE 
    WHEN a THEN 1
    WHEN b THEN 2
    WHEN c THEN 3
    ELSE 4
END AS attribute_name

For more information, refer official doc at https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-ConditionalFunctions

Tags:

Hql

Hive