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

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

问题描述

Hive 使用 IF(condition, expression, expression),所以当我想做 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?

寻找类似标准的东西

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

推荐答案

您可以使用 Hive Conditional CASE WHEN 函数处理 if-else 方案.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天全站免登陆