2017-10-11 45 views
1

我有一個地圖,其中包含另一個地圖的值字段。這是一些記錄的例子;從地圖[字符串,任何]提取值其中任何一個地圖本身

(8702168053422489,Map(sequence -> 5, id -> 8702168053422489, type -> List(AppExperience, Session), time -> 527780267713)) 
(8702170626376335,Map(trackingInfo -> Map(trackId -> 14183197, location -> Browse, listId -> 3393626f-98e3-4973-8d38-6b2fb17454b5_27331247X28X6839X1506087469573, videoId -> 80161702, rank -> 0, row -> 1, imageKey -> boxshot|AD_e01f4a50-7e2b-11e7-a327-12789459b73e|en, requestId -> 662d92c2-6a1c-41a6-8ac4-bf2ae9f1ce68-417037), id -> 8702170626376335, sequence -> 59, time -> 527780275219, type -> List(NavigationLevel, Session), view -> details)) 
(8702168347359313,Map(muting -> false, id -> 8702168347359313, level -> 1, type -> List(Volume))) 
(8702168321522401,Map(utcOffset -> 3600, type -> List(TimeZone), id -> 8702168321522401)) 
(8702171157207449,Map(trackingInfo -> Map(trackId -> 14183197, location -> Browse, listId -> 3393626f-98e3-4973-8d38-6b2fb17454b5_27331247X28X6839X1506087469573, videoId -> 80161356, rank -> 0, row -> 1, imageKey -> boxshot|AD_e01f4a50-7e2b-11e7-a327-12789459b73e|en, requestId -> 662d92c2-6a1c-41a6-8ac4-bf2ae9f1ce68-417037), id -> 8702171157207449, sequence -> 72, startOffset -> 0, time -> 527780278061, type -> List(StartPlay, Action, Session))) 

實際記錄我所感興趣的是包含trackingInfo,記錄2和5

我希望做的是提取那些,然後從那裏提取等一些鍵的那些作爲trackId。像這樣的東西;

val trackingInfo = json("trackingInfo").asInstanceOf[Map[String, Any]] 
val videoId = trackingInfo("videoId").asInstanceOf[Int] 
val id = json("id").asInstanceOf[Long] 
val sequence = json("sequence").asInstanceOf[Int] 
val time = json("time").asInstanceOf[Long] 
val eventType = json.get("type").getOrElse(List("")).asInstanceOf[List[String]] 

爲了提取內圖,我累了; myMap.map {case (k,v: collection.Map[_,_]) => v.toMap case _ => }

這帶來了內部的地圖,但作爲scala.collection.immutable.Iterable[Any]這使我在從中提取值的困惑。

任何幫助表示讚賞

+0

只是一般性評論。使用.asInstanceOf不安全。可能導致問題 – Pavel

回答

1

比方說,你有一個真正的地圖(我把它剪一點點)

val data: Map[ BigInt, Any ] = Map(
    BigInt(8702168053422489L) -> Map("sequence" -> "5", "id" -> BigInt(8702168053422489L)), 
    BigInt(8702170626376335L) -> Map("trackingInfo" -> Map("trackId" -> BigInt(14183197), "location" -> "Browse"), "id" -> BigInt(8702170626376335L)), 
    BigInt(8702168347359313L) -> Map("id" -> BigInt(8702168347359313L)), 
    BigInt(8702168321522401L) -> Map("id" -> BigInt(8702168321522401L)), 
    BigInt(8702171157207449L) -> Map("trackingInfo" -> Map("trackId" -> BigInt(14183197), "location" -> "Browse"), "id" -> BigInt(8702171157207449L)) 
) 

你想其中有一個trackingInfo關鍵

val onlyWithTracking = data.filter((row) => { 

    val recordToFilter = row._2 match { 
     case trackRecord: Map[ String, Any ] => trackRecord 
     case _ => Map("trackId" -> Map()) 
    } 

    recordToFilter.contains("trackingInfo") 
}) 
記錄

然後以某種方式處理這些記錄

onlyWithTracking.foreach((row) => { 

    val record = row._2 match { 
     case trackRecord: Map[ String, Any ] => trackRecord 
     case _ => Map("trackingInfo" -> Map()) 
    } 

    val trackingInfo = record("trackingInfo") match { 
     case trackRow: Map[ String, Any ] => trackRow 
     case _ => Map("trackId" -> "error") 
    } 

    val trackId = trackingInfo("trackId") 

    println(trackId) 
}) 

使用此模式匹配我試圖確保使用諸如trackingInfotrackId之類的密鑰有點安全。你應該實施更嚴格的方法。

+0

感謝您的回覆。不過請記住,原來的Map是'Map [String,Any]'所以當做'val onlyWithTracking = data.filter((row)=> row._2.contains(「trackingInfo」))'我得到錯誤'value contains不是任何成員' – ukbaz

+0

是的。我編輯了答案來解決這個問題。多一點模式匹配。 –

+0

謝謝你,我似乎得到這個錯誤處理位scala.math.BigInt不能轉換爲java.lang.String。 – ukbaz

相關問題