gpt4 book ai didi

scala - 使用Spark数据框进行单元测试

转载 作者:行者123 更新时间:2023-12-04 11:35:27 25 4
gpt4 key购买 nike

我正在尝试测试程序的一部分,该部分对数据帧执行转换
我想测试这些数据框的几种不同变体,以排除从文件中读取特定DF的选项

所以我的问题是:

  • 是否有关于如何使用Spark和数据帧执行单元测试的良好教程,尤其是关于数据帧创建的教程?
  • 如何在没有大量样板且不从文件读取这些数据的情况下创建这些不同的几行数据帧?
  • 是否有用于检查数据框内特定值的实用程序类?

  • 我以前显然在Google上搜索过,但找不到任何非常有用的东西。我发现的更有用的链接是:
  • Running a basic unit test with a dataframe
  • Custom made assertions with DF

  • 如果示例/教程位于Scala,那就太好了,但我会尽量采用您所能使用的任何语言

    提前致谢

    最佳答案

    link展示了我们如何以编程方式创建具有模式的数据框。您可以将数据保留在单独的特征中,然后将其与测试混合。例如,

    // This example assumes CSV data. But same approach should work for other formats as well.

    trait TestData {
    val data1 = List(
    "this,is,valid,data",
    "this,is,in-valid,data",
    )
    val data2 = ...
    }

    然后使用ScalaTest,我们可以做这样的事情。
    class MyDFTest extends FlatSpec with Matchers {

    "method" should "perform this" in new TestData {
    // You can access your test data here. Use it to create the DataFrame.
    // Your test here.
    }
    }

    要创建DataFrame,您可以使用以下几种util方法。
      def schema(types: Array[String], cols: Array[String]) = {
    val datatypes = types.map {
    case "String" => StringType
    case "Long" => LongType
    case "Double" => DoubleType
    // Add more types here based on your data.
    case _ => StringType
    }
    StructType(cols.indices.map(x => StructField(cols(x), datatypes(x))).toArray)
    }

    def df(data: List[String], types: Array[String], cols: Array[String]) = {
    val rdd = sc.parallelize(data)
    val parser = new CSVParser(',')
    val split = rdd.map(line => parser.parseLine(line))
    val rdd = split.map(arr => Row(arr(0), arr(1), arr(2), arr(3)))
    sqlContext.createDataFrame(rdd, schema(types, cols))
    }

    我不知道任何用于检查DataFrame中特定值的实用程序类。但是我认为使用DataFrame API编写一个应该很简单。

    关于scala - 使用Spark数据框进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36060502/

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