我们知道 Hive 不会根据字段验证数据,用户有责任手动检查数据。我知道我们可以执行一些基本检查来验证数据。
我确信我们可以执行更多的检查或验证来验证 Hive 表上的数据。任何建议最欢迎。
不幸的是,您无法为 Hive 中的每一列生成此查询。像这样手动执行此操作或使用 shell 或其他一些工具生成基于描述表的输出:
select count(*) as total_records,
--repeat these for each column
count(case when col1 is null then 1 end) as col1_nulls_cnt,
count(distinct col1) as col1_distinct,
min(col1) as col1_min,
max(col1) as col1_max
from your_table;
Run Code Online (Sandbox Code Playgroud)
可以使用以下方法验证日期cast(col1 as date):
select cast(col1 as date) --returns NULL if the date is in wrong format
Run Code Online (Sandbox Code Playgroud)
您可以像第一个查询一样计算强制转换产生的 NULL:
count(case when cast(col1 as date) is null then 1 end) as col1_wrong_dates_cnt
Run Code Online (Sandbox Code Playgroud)
此外,对于更复杂的检查,您可以加入所需的日期范围,该日期范围可以像这样生成或生成,并检查日期是否加入,如下所示:
select col1,
case when d.dt is not null then 'Ok' else 'Wrong date' end date_check
from your_table t
left join date_range d on t.col1=d.d.dt
Run Code Online (Sandbox Code Playgroud)
也可以使用cast()与此答案中相同的方式检查数字/其他基元类型列: https: //stackoverflow.com/a/38143497/2700344。
关于 Hive 需要记住的一件重要事情是:当您在日期/时间戳列中插入错误格式的字符串时,Hive 会无一例外地将其默默地转换为 NULL。大多数原始类型都会发生这种情况。但是,如果您尝试将 bigint 插入 int 列,Hive 会默默地截断它,生成一些适合 int 大小的不同数字。考虑到所有这些,最好在验证之前在原始数据之上构建包含所有字符串的表。