scala> // Step 1 scala> val inputList = List( "data=first data || key1=r1v1 || key2=", | "data=second data || key1=r2v1 || key2=r2v2", | "key1=r3v1" | ) inputList: List[String] = List(data=first data || key1=r1v1 || key2=, data=second data || key1=r2v1 || key2=r2v2, key1=r3v1) scala> // Step 2 scala> val splitted = inputList.map{ x => | x.split("\\|\\|") | .map(_.trim) | } splitted: List[Array[String]] = List(Array(data=first data, key1=r1v1, key2=), Array(data=second data, key1=r2v1, key2=r2v2), Array(key1=r3v1)) scala> // Step 3 scala> val filteredList = splitted.map{ x => | val retval = for { element <- x | val keyNval = element.split("=") | if keyNval.size >= 2 | } yield { | val splitted = element.split("=") | // Create Tuple of Key and Value | splitted(0) -> splitted(1) | } | retval | } warning: there were 1 deprecation warning(s); re-run with -deprecation for details filteredList: List[Array[(String, String)]] = List(Array((data,first data), (key1,r1v1)), Array((data,second data), (key1,r2v1), (key2,r2v2)), Array((key1,r3v1))) scala> // Step 4 scala> val dnryList = filteredList.map{ x => | x.toMap | } dnryList: List[scala.collection.immutable.Map[String,String]] = List(Map(data -> first data, key1 -> r1v1), Map(data -> second data, key1 -> r2v1, key2 -> r2v2), Map(key1 -> r3v1)) scala> // Step 5 scala> val filteredDnryList = dnryList.filter{ x => | if (x.getOrElse("data", ()) != ()) | true | else | false | } filteredDnryList: List[scala.collection.immutable.Map[String,String]] = List(Map(data -> first data, key1 -> r1v1), Map(data -> second data, key1 -> r2v1, key2 -> r2v2)) scala> // Step 6 scala> val keyvaluepairs = filteredDnryList.map { x => | val data = x.getOrElse("data", "") | (data, x) | } keyvaluepairs: List[(String, scala.collection.immutable.Map[String,String])] = List((first data,Map(data -> first data, key1 -> r1v1)), (second data,Map(data -> second data, key1 -> r2v1, key2 -> r2v2))) scala> // Step 7 scala> val words = keyvaluepairs.flatMap{ x => | //Underscore (_) is used as a Placeholder indicator | val xsplitted = x._1.split(" ").map(_.trim) | val wordNmeta = for { element <- xsplitted | } yield { | (element, x._2) | } | wordNmeta | } words: List[(String, scala.collection.immutable.Map[String,String])] = List((first,Map(data -> first data, key1 -> r1v1)), (data,Map(data -> first data, key1 -> r1v1)), (second,Map(data -> second data, key1 -> r2v1, key2 -> r2v2)), (data,Map(data -> second data, key1 -> r2v1, key2 -> r2v2)))c200 > a15
Data Conversion using : Map, for/yield etc...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment