【问题标题】:How to check for corrupt records in Hive table如何检查 Hive 表中的损坏记录
【发布时间】:2020-11-17 00:56:07
【问题描述】:

我有一个配置单元表,上面的数据每天都会增加。在特定的一天,一些损坏的记录被插入到表中。有没有办法可以将表与 HDFS 上的主文件匹配并从 Hive 中提取损坏的记录

如何识别具有 100 万行的 hive 表中的损坏记录?

【问题讨论】:

  • 1.当您说“数据每天都在增加”时,您是在手动将数据从 HDFS 文件加载到 Hive
  • @BruceWayne - 是的,每天都会将文件添加到 HDFS,然后将数据加载到 Hive 表中。

标签: apache-spark hadoop hive hdfs


【解决方案1】:

使用 join, except1 找出加载到 Hive 表和文件中的损坏记录。

Example:

//read the file
val df=spark.read.<format>("<path>")

//read hive table
val df1=spark.read.table("<db>.<hive_table_name>")

//without using md5 hash
df.exceptAll(df1).show()
df1.exceptAll(df).show()

//create md5 hash by concatenating all column values
val df2=df.withColumn("md_hash",md5(concat_ws(",",df.columns.map(c => col(c)): _*))).select("md_hash")

val df3=df1.withColumn("md_hash",md5(concat_ws(",",df.columns.map(c => col(c)): _*))).select("md_hash")

//get non matching rows from df2 that are not existed in df3
df2.except(df3).show()
df2.exceptAll(df3).show()

//get non matching rows from df3 that are not existed in df2
df3.exceptA(df2).show()
df3.exceptAll(df2).show()

//or using full outer join
df3.join(df2,df3("md_hash") === df2("md_hash"),"full").
filter((df2("md_hash").isNull || df3("md_hash").isNull)).
show(10,false)

【讨论】:

  • 感谢您提供详细信息。你能解释一下“md5 hash by concatenating all column values”的目的吗
  • @SNS,我们正在计算所有列的 md5 哈希,然后很容易找出两个数据帧之间的except(差异),我们可以在不使用的情况下做到这一点md5 也 df.except(df1).show() (或)df1.except(df).show() 可以在没有 md5 哈希的情况下为您提供预期的结果。
  • 谢谢@Shu 的解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2016-11-26
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
相关资源
最近更新 更多