如何在Spark2 Scala中使用区分大小写模式

时间:2019-07-16 10:43:59

标签: scala apache-spark pattern-matching

我想在scala中使用大小写匹配来定义String列表。这是我的代码:

Parametre_mod.targetTableBase match {
              case "DWG_LD_ANOMALIE" => 
                val champs = List("CD_ANOMALIE",
                    "CD_FAMILLE",
                    "LIB_ANOMALIE",
                    "DT_MAJ",
                    "CLASSIFICATION",
                    "NB_REJEUX",
                    "TOP_INDIC_CD_ERREUR", "TOP_COORDONNEE_INVALIDE", "TOP_SUPP", "DT_SUPP")

              case "DWG_LD_NOTIFICATION" =>
                val champs = List(
                    "ID_NOTIFICATION", "ID_ENTITE", "ID_ENTITE_GARANTE", "CD_SYS_APPELANT", "CD_TYPE_DESTINATAIRE", "ID_PERSONNE", "EMAIL_DESTINATAIRE", 
                    "NUM_TEL_DESTINATAIRE", "CD_TYPE_EVENEMENT", "CD_TYPE_NOTIFICATION", "CD_TYPE_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE", "REF_EXT_DEST_SOURCE", 
                    "EMAIL_DEST_SOURCE", "NUM_TEL_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE_2", "NB_REJEUX", "TOP_RECYCLAGE", "ID_DOC_STRATEGIE_EDIT", "TOP_SUPP", "DT_SUPP")
            }

            insertion(df, champs, cstProp)

但是Eclipse IDE指示未定义插入函数中的字段冠军。我该如何实现?

2 个答案:

答案 0 :(得分:2)

您在champs表达式内创建的match的范围。而且无法在其外部访问。

您可以如下分配给champs

val champs = Parametre_mod.targetTableBase match {
  case "DWG_LD_ANOMALIE" => List("CD_ANOMALIE",
      "CD_FAMILLE",
      "LIB_ANOMALIE",
      "DT_MAJ",
      "CLASSIFICATION",
      "NB_REJEUX",
      "TOP_INDIC_CD_ERREUR", "TOP_COORDONNEE_INVALIDE", "TOP_SUPP", "DT_SUPP")
  case "DWG_LD_NOTIFICATION" => List(
      "ID_NOTIFICATION", "ID_ENTITE", "ID_ENTITE_GARANTE", "CD_SYS_APPELANT", "CD_TYPE_DESTINATAIRE", "ID_PERSONNE", "EMAIL_DESTINATAIRE",
      "NUM_TEL_DESTINATAIRE", "CD_TYPE_EVENEMENT", "CD_TYPE_NOTIFICATION", "CD_TYPE_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE", "REF_EXT_DEST_SOURCE",
      "EMAIL_DEST_SOURCE", "NUM_TEL_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE_2", "NB_REJEUX", "TOP_RECYCLAGE", "ID_DOC_STRATEGIE_EDIT", "TOP_SUPP", "DT_SUPP")
}

希望这会有所帮助!

答案 1 :(得分:0)

您需要在match块之外定义“冠军”,以便在外部也可以使用-

var champs:List[String] = List.empty[String]

    Parametre_mod.targetTableBase match {
      case "DWG_LD_ANOMALIE" =>
        champs = List("CD_ANOMALIE",
          "CD_FAMILLE",
          "LIB_ANOMALIE",
          "DT_MAJ",
          "CLASSIFICATION",
          "NB_REJEUX",
          "TOP_INDIC_CD_ERREUR", "TOP_COORDONNEE_INVALIDE", "TOP_SUPP", "DT_SUPP")

      case "DWG_LD_NOTIFICATION" =>
        champs = List(
          "ID_NOTIFICATION", "ID_ENTITE", "ID_ENTITE_GARANTE", "CD_SYS_APPELANT", "CD_TYPE_DESTINATAIRE", "ID_PERSONNE", "EMAIL_DESTINATAIRE",
          "NUM_TEL_DESTINATAIRE", "CD_TYPE_EVENEMENT", "CD_TYPE_NOTIFICATION", "CD_TYPE_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE", "REF_EXT_DEST_SOURCE",
          "EMAIL_DEST_SOURCE", "NUM_TEL_DEST_SOURCE", "ID_PERSONNE_DEST_SOURCE_2", "NB_REJEUX", "TOP_RECYCLAGE", "ID_DOC_STRATEGIE_EDIT", "TOP_SUPP", "DT_SUPP")
    }
    insertion(df, champs, cstProp)