Brigitte
Brigitte

Reputation: 25

Scala: value split is not a member of char

I am trying to write word count program in Scala. I'm using a string "file" :

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

It is keep saying that:

value split is not a member of Char

Can't figure out how to solve it!

Upvotes: 2

Views: 13430

Answers (2)

Brigitte
Brigitte

Reputation: 25

I found out that the code is perfect! Just because I was running it on Spark, the answer was kept in lazy RDD file that I needed to collect it somehow. Therefore, I saved it to a text file and problem solved! Here is the code:

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

Thanks.

Upvotes: 0

Dave Swartz
Dave Swartz

Reputation: 910

When you call map on a String it is wrapped with WrappedString which extends AbstractSeq[Char]. Therefore, when you call map it is as if you are doing so on a Seq of Char not a Seq of 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 splits by whitespace and returns the size, a word counter.

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

To get a count of the number of times each word in the string appears.

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

Upvotes: 4

Related Questions