我有一张src_grp_map地图。现在我想要得到内部映射,它在内部映射的连接表关键字中有一个模式字符串edw.cdim_country。
下面是我的地图源图
val src_grp_map=Map("edw.dim_cust_extn_odm" ->
Map("src_table" -> "edw.dim_cust_extn_odm", "tgt_attribute_count" -> 3, "join_table" -> "edw.dim_cust,edw.cdim_country,NA"),
"edw.dw_atk_case_general" ->
Map("src_table" -> "edw.dw_atk_case_general", "tgt_attribute_count" -> 2, "join_table" -> "NA"))
现在使用src_grp_map,我希望在join_table键输出中包含edw.cdim_country的内部映射应该如下所示。
Map("src_table" -> "edw.dim_cust_extn_odm", "tgt_attribute_count" -> 3, "join_table" -> "edw.dim_cust,edw.cdim_country,NA")
如果多个内部映射包含模式字符串,那么我需要所有内部映射。
发布于 2020-05-12 19:21:20
这听起来像是一个有趣的问题。然而,我认为有一件事会对你有很大的帮助。目前,src_grp_map的类型为Map[String,MapString,AnyVal]。我认为将其类型设置为MapString,CustomClass会更好。
因此,以下是我首选的解决方案:
case class TableInfo(srcTable: String, tgtAttributeCount: Int, joinTable: IndexedSeq[String])
val info1 = TableInfo("edw.dim_cust_extn_odm", 3, IndexedSeq("edw.dim_cust", "edw.cdim_country", "NA"))
val info2 = TableInfo("edw.dw_atk_case_general", 2, "NA")
val srcGrpMap = Map("edw.dim_cust_extn_odm" -> info1, "edw.dw_atk_case_general" -> info2)
def getTableInfo(joinTableKey: String, inputMap: Map[String, TableInfo]): IndexedSeq[TableInfo] = inputMap.values.filter(_.joinTable.contains(joinTableKey))
只需调用getTableInfo函数。
现在,如果您坚持使用原始的笨重数据格式,这里有一个替代解决方案:
def getTableInfo(joinTableKey: String, inputMap: Map[String, Map[String, AnyVal]]): IndexedSeq[Map[String, AnyVal]] = {
inputMap.values.filter{ x =>
x.get("join_table") match {
case Some(y) =>
y match {
case z: String =>
z.split(",").contains(joinTableKey)
case z => false
}
case None =>
false
}
}.toIndexedSeq
}
发布于 2020-05-12 19:25:06
我已经用下面的代码实现了这一点。
val src_grp_map=Map("edw.dim_cust_extn_odm" ->
Map("src_table" -> "edw.dim_cust_extn_odm", "tgt_attribute_count" -> 3, "join_table" -> "edw.dim_cust,edw.cdim_country,NA"),
"edw.dw_atk_case_general" ->
Map("src_table" -> "edw.dw_atk_case_general", "tgt_attribute_count" -> 2, "join_table" -> "NA"))
val collect_src_table_values=src_grp_map.map(p=>p._2).toList
val z=collect_src_table_values.filter(x=>x("join_table").toString().contains("edw.cdim_country"))
https://stackoverflow.com/questions/61758321
复制