在scala中获取类型不匹配异常

时间:2017-08-02 05:39:09

标签: scala apache-spark

您好我正在尝试使用火花scala的UDAF。我收到以下异常。

 Description Resource Path Location Type type mismatch;  found   : scala.collection.immutable.IndexedSeq[Any]  required: String SumCalc.scala   /filter line 63 Scala Problem

这是我的代码。

override def evaluate(buffer: Row): Any = {
val in_array = buffer.getAs[WrappedArray[String]](0);
var finalArray = Array.empty[Array[String]]
import scala.util.control.Breaks._
breakable {
  for (outerArray <- in_array) {
          val currentTimeStamp = outerArray(1).toLong 
    var sum = 0.0
    var count = 0
    var check = false
    var list = outerArray
    for (array <- in_array) {
      val toCheckTimeStamp = array(1).toLong
      if (((currentTimeStamp - 10L) <= toCheckTimeStamp) && (currentTimeStamp >= toCheckTimeStamp)) {
        sum += array(5).toDouble 
        count += 1
      }
      if ((currentTimeStamp - 10L) > toCheckTimeStamp) {
        check = true
        break
      }
    }
    if (sum != 0.0 && check) list = list :+ (sum).toString // getting error on this line.
        else list = list :+ list(5).toDouble.toString 
    finalArray ++= Array(list)
  }
  finalArray
}
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

evaluate UDAF函数中存在一些错误。

  1. list变量是string,但您将其视为array

  2. finalArray初始化为Array.empty[Array[String]],但稍后您将Array(list)添加到finalArray

  3. 您没有从finalArray方法返回evaluate内部for循环

  4. 所以正确的方法应该如下

      override def evaluate(buffer: Row): Any = {
        val in_array = buffer.getAs[WrappedArray[String]](0);
        var finalArray = Array.empty[String]
        import scala.util.control.Breaks._
        breakable {
          for (outerArray <- in_array) {
            val currentTimeStamp = outerArray(1).toLong // timestamp values
            var sum = 0.0
            var count = 0
            var check = false
            var list = outerArray
            for (array <- in_array) {
              val toCheckTimeStamp = array(1).toLong
              if (((currentTimeStamp - 10L) <= toCheckTimeStamp) && (currentTimeStamp >= toCheckTimeStamp)) {
                sum += array(5).toDouble // RSSI weightage values
                count += 1
              }
              if ((currentTimeStamp - 10L) > toCheckTimeStamp) {
                check = true
                break
              }
            }
    
            if (sum != 0.0 && check) list = list + (sum).toString // calculate sum for the 10 secs difference
            else list = list + (sum).toString // If 10 secs difference is not there take rssi weightage value
    
            finalArray ++= Array(list)
          }
        }
        finalArray // Final results for this function
      }
    

    希望答案有用