在SparkTypes上使用Typeclasses

时间:2018-05-12 08:11:02

标签: scala apache-spark spark-dataframe

我正在尝试在Spark类型上使用scala TypeClass,这是我写的一个小代码片段。

function initializeProgressorPos() {
    document.getElementById("main_navigator_progression").setAttribute("style", ("width: " + (document.getElementsByClassName("main_nav_btn")[2].getBoundingClientRect().x - document.getElementsByClassName("main_nav_btn")[2].getBoundingClientRect().width) + "px"))
}
initializeProgressorPos();
$(window).resize(function() {
    initializeProgressorPos();
});

当我在本地intellij上运行时,会抛出以下错误

trait ThresholdMethods[A] {
    def applyThreshold(): Boolean
}

object ThresholdMethodsInstances {

    def thresholdMatcher[A](v:A)(implicit threshold: ThresholdMethods[A]):Boolean =
        threshold.applyThreshold()

        implicit val exactMatchThresholdStringType = new ThresholdMethods[StringType] {
        def applyThreshold(): Boolean = {
            print("string")
            true
        }
    }

    implicit val exactMatchThresholdNumericType = new ThresholdMethods[IntegerType] {
        def applyThreshold(): Boolean = {
            print("numeric")
            true
        }
    }
}

object Main{
    def main(args: Array[String]): Unit = {
        val spark = SparkSession.builder().appName("ZenDataValidationLib").config("spark.some.config.option", "some-value")
            .master("local").getOrCreate()
        import spark.sqlContext.implicits._
        val df1 = Seq(
            ("2016-04-02", "14", "NULL", 9874, 880, "xyz"), ("2016-04-30", "14", "FR", 9875, 13,"xyz"), ("2017-06-10", "15", "PQR", 9867, 57721,"xyz")
        ).toDF("WEEK", "DIM1", "DIM2","T1","T2","T3")
        import ThresholdMethodsInstances._
        println(df1.schema("T1").dataType)
        ThresholdMethodsInstances.thresholdMatcher(df1.schema("T1").dataType)
    }
}

我也尝试使用String和Int做同样的事情,它工作得非常好。有人可以帮我在SparkTypes上做这个吗?

1 个答案:

答案 0 :(得分:0)

请注意,form.querySelector('input[type="submit"]').click()会返回StructField.dataType,而非特定的子类,而您的代码仅为DataTypeimplicit定义ThresholdMethods IntegerType

由于StringType解析在编译时发生,因此编译时没有足够的信息来确定正确的实例,这会使类型匹配。

如果您(显然您不想要),这将有效:

implicit

但在实践中,你需要更明确的东西,比如模式匹配,而不是隐式参数。

您还可以考虑切换到强类型ThresholdMethodsInstances.thresholdMatcher( df1.schema("T1").dataType.asInstanceOf[IntegerType] ) ,这是一个更好的选择。