我花了很多时间,我没有办法将以下txt转换为scala.collection.mutable.map.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
首先,我尝试以正常方式读取文件,但是我无法将行添加到地图[string,int],您有一些想法吗?
在 scala 中,您不需要 Mutable Map 即可附加,您可以将映射到 new Collection 包含tuple
类型(如下:(t(0), t(1).toInt)
(,最后将此collection
转换为 map ,也许是:
Source.fromFile(new File("test.txt")).getLines().map(_.split(",")).map(t => (t(0), t(1).toInt)).toMap
您可以简单地使用input text
split
CC_5,然后将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))