首页 > 解决方案 > 不确定如何避免以下类型错误:找到:任何必需:字符串

问题描述

这是我的数据的样子:

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. 有什么办法可以解决这个问题还是我做错了什么?

标签: scalaapache-spark

解决方案


顺便说一下几乎正确的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这不是预期的:Stringspark.sql

testCase: Map[String, Any]

testCase("string") // Result Any

参考文档: SparkSession ScalaDoc


推荐阅读