我是 Scala 的新手,在编写单元测试时遇到了问题。
我试图在 Scala 中比较和检查两个 Spark DataFrame 的相等性以进行单元测试,并意识到没有简单的方法来检查两个 Spark DataFrame 的相等性。
C++ 等效代码将是(假设 DataFrames 在 C++ 中表示为双数组):
int expected[10][2];
int result[10][2];
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 2; col++) {
if (expected[row][col] != result[row][col]) return false;
}
}
实际测试将涉及基于 DataFrame 列的数据类型的相等性测试(使用浮点数的精度容差测试等)。
似乎没有一种简单的方法可以使用 Scala 和其他用于检查两个 DataFrame 相等性的解决方案(例如
df1.except(df2)
)来迭代地循环遍历 DataFrame 中的所有元素。在我的情况下不起作用,因为我需要能够为测试相等性提供支持,并容忍 float 和 double 。当然,我可以尝试事先对所有元素进行舍入,然后比较结果,但我想看看是否有任何其他解决方案可以让我遍历 DataFrame 以检查相等性。
最佳答案
import org.scalatest.{BeforeAndAfterAll, FeatureSpec, Matchers}
outDf.collect() should contain theSameElementsAs (dfComparable.collect())
# or ( obs order matters ! )
// outDf.except(dfComparable).toDF().count should be(0)
outDf.except(dfComparable).count should be(0)
关于scala - 检查 Scala 中两个 Spark DataFrame 的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40497639/