Gid*_*eon 7 unit-testing scala apache-spark apache-spark-sql spark-dataframe
我正在尝试测试我的程序的一部分,它执行数据帧的转换我想测试这些数据帧的几个不同变体,这排除了从文件中读取特定DF的选项
所以我的问题是:
我之前显然用谷歌搜索过,但找不到任何非常有用的东西.我找到的更有用的链接包括:
如果示例/教程在Scala中会很棒,但我会采用你所拥有的任何语言
提前致谢
此链接显示了我们如何以编程方式创建具有模式的数据框.您可以将数据保存在单独的特征中,并将其与测试混合.例如,
// 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 = ...
}
Run Code Online (Sandbox Code Playgroud)
然后使用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.
}
}
Run Code Online (Sandbox Code Playgroud)
要创建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))
}
Run Code Online (Sandbox Code Playgroud)
我不知道用于检查DataFrame中的特定值的任何实用程序类.但我认为使用DataFrame API编写一个应该很简单.
| 归档时间: |
|
| 查看次数: |
10347 次 |
| 最近记录: |