-1

나는 불꽃 스칼라 UDF를 작성하고 직면하고있다 :이 오류를 던지고있다- 유형에 대한 스키마는 상관이 지원되지 않습니다

import org.apache.spark.sql.expressions.UserDefinedFunction 
import org.apache.spark.sql.functions.udf 

val aBP = udf((bG: String, pS: String, bP: String, iOne: String, iTwo: String) => { 
    if (bG != "I") {"NA"} 
    else if (pS == "D") 
    {if (iTwo != null) iOne else "NA"} 
    else if (pS == "U") 
    {if (bP != null) bP else "NA"} 
}) 

"java.lang.UnsupportedOperationException가 없음 지원되지 않는 유형의 스키마"를 " java.lang.UnsupportedOperationException가 : 상관

+0

당신은 너무'else'이 필요합니다. 조건이 충족되지 않으면 어떻게해야합니까? – philantrovert

답변

2

this link에 disussed로 UDF가 반환해야 "지원되지 않는 유형에 대한 스키마 :

  • 프리미티브 (INT, 문자열, 부울, ...) 지원되는 다른 종류의
  • 목록, 배열, 다른 지원되는 유형

당신은 당신의 코드에 또 다른 추가 그래서 경우의 다른 지원되는 유형의지도

  • 케이스 클래스의
  • 튜플은 컴파일이 성공합니다.

    val aBP = udf((bG: String, pS: String, bP: String, iOne: String, iTwo: String) => { 
        if (bG != "I") {"NA"} 
        else if (pS == "D") { 
         if (iTwo != null) 
         iOne 
         else "NA" 
        } else if (pS == "U") { 
         if (bP != null) 
         bP 
         else 
         "NA" 
        } else { 
         "" 
        } 
        }) 
    

    또한 패턴 매칭을 사용하여 코드를 재배포 할 수 :

    val aBP = udf [String, String, String, String, String, String] { 
        case (bG: String, _, _, _, _)      if bG != "I" => "NA" 
        case (_, pS: String, _, iOne: String, iTwo: String) if pS == "D" && iTwo.isEmpty => iOne 
        case (_, pS: String, _, _, _)      if pS == "D" => "NA" 
        case (_, pS: String, bP: String, _, _)    if pS == "U" && bP.isEmpty => bP 
        case (_, pS: String, _, _, _)      if pS == "U" => "NA" 
        case _ => "" 
    } 
    
  • 관련 문제