输入数据为

+----------------------+--------------------------------+
|      movie_name      |             Genres             |
+----------------------+--------------------------------+
| digimon              | Adventure|Animation|Children's |
| Slumber_Party_Massac | Horror                         |
+----------------------+--------------------------------+

我需要像这样的输出
+----------------------+--------------------------------+-----------------+
|      movie_name      |             Genres             | count_of_genres |
+----------------------+--------------------------------+-----------------+
| digimon              | Adventure|Animation|Children's |               3 |
| Slumber_Party_Massac | Horror                         |               1 |
+----------------------+--------------------------------+-----------------+

最佳答案

select  *
       ,size(split(coalesce(Genres,''),'[^|\\s]+'))-1  as count_of_genres

from    mytable

该解决方案涵盖了各种用例,包括-
  • NULL值
  • 空字符串
  • 空 token (例如Adventure||AnimationAdventure| |Animation)
  • 关于hadoop - 如何计算 hive 由 “|”分隔符分隔的每一列中的单词数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43573144/

    10-12 23:02