scala - 不确定如何避免以下类型错误:找到:任何必需:字符串
问题描述
这是我的数据的样子:
configStr: String =
"
{
"validation": {
"target_feed": "tables.validation",
"data_validations":
[
{"program": "program1",
"test_description": "Checking if column1 are distinct",
"input_column": "column1",
"test": "distinctness",
"query": "select * from table1",
"condition": "None"},
{"program": "program12",
"test_description": "Checking if column2 are distinct",
"input_column": "column2",
"test": "Anomaly",
"query": "select * from table2",
"condition": "None"}
]
}
}"
我需要遍历数据验证并利用每个字段。我打算这样做:
val resultsAsDf = conf("test")
.asInstanceOf[Map[String, Any]]("data_validations")
.asInstanceOf[Seq[Map[String, Any]]]
.map{ dv => someFunc(dv) }
.reduce(_.unionAll(_))
现在,创建someFunc
它将处理逻辑。我建立这样的东西:
def someFunc(testCase: Map[String, Any]): Unit = {
if (testCase("test") == "distinctness") {
val tempDF = spark.sql(testCase("query"))
val verificationResults: VerificationResult = { VerificationSuite()
.onData(tempDF)
.addCheck(
Check(CheckLevel.Error, testCase("program"))
.hasDistinctness(testCase("column"), Check.IsOne))
.run()
}
}
else{
println("Nothing")
}
}
现在,我收到以下错误:
<console>:54: error: type mismatch;
found : Any
required: String
val tempDF = spark.sql(testCase("query"))
^
<console>:60: error: type mismatch;
found : Any
required: String
Check(CheckLevel.Error, testCase("program"))
问题是,我需要映射值是各种类型,这就是我选择Any
. 有什么办法可以解决这个问题还是我做错了什么?
解决方案
顺便说一下几乎正确的SequenceToSequence,混淆了需要更改的类型。testCase
正在返回Any
,当spark.sql(_)
期望 a时String
,问题不在于返回,someFunc
而是返回Map
。
def someFunc(testCase: Map[String, Any]): Unit = {}
val tempDF = spark.sql(testCase("query"))
// Spark 3.1.1 ScalaDoc
def sql(sqlText: String): DataFrame
因此,当您将String
密钥传递到地图时,您会得到一个,Any
这不是预期的:String
spark.sql
testCase: Map[String, Any]
testCase("string") // Result Any
参考文档: SparkSession ScalaDoc
推荐阅读
- d3.js - D3:将工具提示定位在圆环图之外
- .net - .netstandard 2.0 中 AppDomain.CurrentDomain.SetupInformation.PrivateBinPath 的替代方法是什么
- c# - 如何在 c# 的嵌套循环中为特定数据对象添加元素?
- asp.net-mvc - User.Identity.GetUserId() 在我的所有控制器上返回 null
- c# - 如何使 listView 内容水平对齐?
- json - 从 ec2 创建寄存器中提取 volume_id
- c# - 如何在windows xp上将当前进程模拟为SYSTEM?
- sbt - 为 SBT 增量编译设置 codebuild 输出源文件夹
- cypress - 柏。cy.type({enter} 间歇性失败
- linux - 使用剧本的 YAML 语法错误 (Ansible)