如何使用hive计算原始数据文件中的所有行?

iqxoj9l9  于 2021-04-02  发布在  Hive
关注(0)|答案(1)|浏览(413)

我读到一些原始输入,看起来像这样。

20   abc   def
21   ghi   jkl
     mno   pqr
23   stu

注意前两行是 "好 "行,后两行是 "坏 "行,因为它们缺少一些数据。
这里是我的hive查询的片段,它将这些原始数据读到一个只读的外部表中。

DROP TABLE IF EXISTS readonly_s3;
CREATE EXTERNAL TABLE readonly_s3 (id string, name string, data string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

我需要得到所有行的计数,包括 "好的 "和 "坏的"。"问题是有些数据丢失了,如果我做SELECT count(id) as total_rows,那就不行,因为不是所有的行都有id
有什么建议可以让我在这个原始数据文件中统计所有的行?

4nkexdtk

4nkexdtk1#

嗯......你可以使用:

select sum(case when col1 is not null and col2 is not null and col3 is not null then 1 else 0 end) as num_good,
       sum(case when col1 is null or col2 is null or col3 is null then 1 else 0 end) as num_bad
from readonly_s3;

相关问题