-1

我有一个 Dataframe 1, Df1, Dataframe 2 , Df2 - 相同的架构

我有来自 Df1 - Dfw1 的第 1 行,来自 Df2 - Dfw2 的第 1 行

我需要比较两者以获得差异 b/n Dfw1 和 Dfw2 并将差异作为集合(地图或其他东西)

4

1 回答 1

1

一个简单的解决方案是将 Row 对象转换为 Map,然后比较 2 个 Map 的值。

像 Scala 中的东西:

val m1 = Dfw1.getValuesMap[AnyVal](Dfw1.schema.fieldNames)
val m2 = Dfw2.getValuesMap[AnyVal](Dfw2.schema.fieldNames)

val differences = for {
field <- m1.keySet
if (!m1.get(field).equals(m2.get(field)))
} yield (field, m1(field), m2(field))

(field, value of Dfw1, value of Dfw1)如果它们不同,则返回元组的 Seq 。

您还可以在 Row 对象上使用模式匹配进行比较:

Dfw1 match {
  case(id: String, desc: String, ....) => // assuming you have the schema
  // compare each value with Dfw2 and return differences
}
于 2019-12-12T20:37:18.017 回答