我花了很多时间,我没有办法将以下的txt转换为scala.collection.mutable.Map [String,Int]
"Becky Smith",12
"John Smith",42
"John",27
"Jones",36
"Matt Jones",48
"Matthew",21
"Rebecca",3
"Sarah Jones",18
"Sarah",33
"Smith",30
-73ef638e:15c66949809:-7ffc,45
-73ef638e:15c66949809:-7ffd,34
-73ef638e:15c66949809:-7ffe,9
-73ef638e:15c66949809:-7fff,39
http://somewhere/JohnSmith/,40
http://somewhere/MattJones/,46
http://somewhere/RebeccaSmith/,10
http://somewhere/SarahJones/,16
http://www.w3.org/2001/vcard-rdf/3.0#FN,47
http://www.w3.org/2001/vcard-rdf/3.0#Family,35
http://www.w3.org/2001/vcard-rdf/3.0#Given,32
http://www.w3.org/2001/vcard-rdf/3.0#N,44
首先我尝试以正常方式读取文件,但我无法将这些行添加到Map [String,Int],你有什么想法吗?
答案 0 :(得分:3)
在 Scala 中,您不需要附加可变地图,您可以map添加新集合包含tuple
类型(如下所示:(t(0), t(1).toInt)
),最后toMap将此collection
转换为地图,可能如下:
Source.fromFile(new File("test.txt")).getLines().map(_.split(",")).map(t => (t(0), t(1).toInt)).toMap
答案 1 :(得分:0)
您可以split
使用input text
,
,然后将splitted text
转换为mutable hashmap
。结果将是hashmaps
的集合,即rdd of hashmaps
代码如下
val data = sc.textFile("path to your text file")
val rddMaps = data.map(line => line.split(",")).map(array => collection.mutable.HashMap[String, Int](array(0)->array(1).toInt))