Scala: split split is not a member of char

I am trying to write a word counting program in Scala. I use the line "file":

file.map( _.split(" ")).flatMap(word => (word, 1)).reduceByKey( _ + _ )

He says that:

Value separation

not a member of Char

I can’t understand how to solve it!

+3
source share
2 answers

When you call mapin String, it completes WrappedString, which expands AbstractSeq[Char]. Therefore, when you call map, you kind of do it on Seqfrom Charnot a Seqfrom String.

See the link below for the code https://github.com/scala/scala/blob/v2.10.2/src/library/scala/collection/immutable/WrappedString.scala

The code below is broken into spaces and returns the size, word counter.

val file = "Some test data"
file.split("\\s+").size

, .

val file = "Some test data test"
println(file.split("\\s+").toList.groupBy(w => w).mapValues(_.length))
+3

, ! , Spark, RDD, - . ! :

file.flatMap(line = > line.split( ")). map (w = > (w, 1)). reduceByKey (+). saveAsTextFile (" OUT.txt")

.

0

All Articles