gpt4 book ai didi

sql - 对 Hive 表执行验证和检查(可能不是重复的)

转载 作者:行者123 更新时间:2023-12-04 21:02:28 24 4
gpt4 key购买 nike

我们知道 Hive 不会根据字段验证数据,用户有责任手动检查数据。
我知道我们可以执行一些基本检查来验证数据。

  • 计算记录数。
  • 每列上的空值数量
  • 每列上唯一/不同值的数量
  • 基于列/数据类型的列级统计数据,如最小值、最大值等
  • 使用 Hive 的内置函数 to_date 和其他函数来检查日期列的验证

  • 我确信我们可以执行更多的检查或验证来验证 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;

    可以使用 cast(col1 as date) 验证日期:
    select cast(col1 as date) --returns NULL if the date is in wrong format

    您可以像在第一个查询中一样计算由 cast 产生的 NULL:
    count(case when cast(col1 as date) is null then 1 end) as col1_wrong_dates_cnt

    此外,对于更复杂的检查,您可以加入所需的日期范围,可以是 generated或生成 like this并检查日期是否已加入,如下所示:
    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

    也可以使用相同的 cast() 检查数字/其他原始类型列就像这个答案: https://stackoverflow.com/a/38143497/2700344 .

    关于 Hive 需要记住的一件重要事情:
    当您在日期/时间戳列中插入错误的格式字符串时,Hive 会毫无异常(exception)地将其转换为 NULL。大多数原始类型都会发生这种情况。但是,如果您尝试将 bigint 插入 int 列,Hive 将默默地截断它,生成一些适合 int 大小的不同数字。考虑到所有这些,最好在验证之前在原始数据之上构建包含所有 STRING 的表。

    关于sql - 对 Hive 表执行验证和检查(可能不是重复的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56259500/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com