I have following Graph Text file, It is big file I want to convert into adjacency List in scala. First few lines of the text file given below:
src, dst,dist
A,C,5.0
A,B,8.0
B,C,6.0
B,D,7.0
B,A,8.0
C,A,5.0
C,B,6.0
D,B,7.0
D,E,8.0
E,D,8.0
I want to convert it into following format of adjucency list.
"A"->List((8.0,"B"),(5.0,"C"))
Please guide me what is good way convert it into adjacency list in scala.
Since there's been no followup:
val zs = """A,C,5.0
A,B,8.0
B,C,6.0
B,D,7.0
B,A,8.0
C,A,5.0
C,B,6.0
D,B,7.0
D,E,8.0
E,D,8.0"""
Parse the string into a Seq of Seq...
val as = zs.split('\n').toSeq.map(_.split(',').toSeq)
Make it into key, value. Value is a a single-element Seq because it makes reduceByKey
easy to use
val bs = as.map{case Seq(k,v,d) => (k, Seq((v,d)))}.reduceByKey(_ ++ _)
// Map(E -> List((D,8.0)),
// A -> List((C,5.0), (B,8.0)),
// B -> List((C,6.0), (D,7.0), (A ,8.0)),
// C -> List((A,5.0), (B,6.0)),
// D -> List((B,7.0), (E,8.0)))
Where a (slightly simplistic) reduceByKey
is
implicit class SparkOps[K, V]( xs:Traversable[(K,V)]) {
def reduceByKey(f:(V,V)=>V)=
xs.groupBy(_._1).mapValues{x=>x.unzip._2.reduce(f)}
}
("simplistic" because it always returns a Map rather than the same Traversable as the input).