scala – 使用Spark数据帧进行单元测试
发布时间:2020-12-16 18:52:01 所属栏目:安全 来源:网络整理
导读:我正在尝试测试我的程序的一部分,它执行数据帧的转换 我想测试这些数据帧的几种不同变体,它们排除了从文件中读取特定DF的选项 所以我的问题是: 有没有关于如何使用Spark和数据帧执行单元测试的优秀教程,特别是关于数据帧的创建? 如何在没有大量样板的情况
我正在尝试测试我的程序的一部分,它执行数据帧的转换
我想测试这些数据帧的几种不同变体,它们排除了从文件中读取特定DF的选项 所以我的问题是: >有没有关于如何使用Spark和数据帧执行单元测试的优秀教程,特别是关于数据帧的创建? 我之前显然用谷歌搜索过,但找不到任何非常有用的东西.我找到的更有用的链接包括: > Running a basic unit test with a dataframe 如果示例/教程在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,in-valid,) 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编写一个应该很简单. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |