在HIVE中编写if/else if/else if/else的最佳方法是什么? [英] What's the best way to write if/else if/else if/else in HIVE?

查看:1207
本文介绍了在HIVE中编写if/else if/else if/else的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hive使用IF(条件,表达式,表达式),因此当我想执行if/else if/else if/else时,我必须这样做:

Hive uses IF(condition, expression, expression), so when I want to do if / else if / else if / else, I have to do:

IF(a, 1, IF(b, 2, IF(c, 3, 4)))

是否有更好的方法来做到这一点,使其更具可读性?

Is there a better way to do this that's more readable?

正在寻找与标准类似的内容

Looking for something similar to the standard

if (a) {
  1
} else if (b) {
  2
} else if (c) {
  3
} else {
  4
}

推荐答案

对于if-else场景,您可以使用 Hive 有条件的CASE WHEN函数. CASE Statement将通过相同的功能为您提供更好的可读性.

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

这篇关于在HIVE中编写if/else if/else if/else的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆