Last active
July 13, 2016 22:00
-
-
Save Krasnyanskiy/ad2b340180139b490056c383ecf4f2bb to your computer and use it in GitHub Desktop.
-scala: map-reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CharactersCount extends App { | |
val src = fromFile(new File("/Users/akrasnyanskiy/IdeaProjects/spark-sql-example/src/main/resources/words.txt")) | |
var mapped = src.map((_, 1)) | |
val combiner: (((Char, Int), (Char, Int)) => ((Char, Int))) = { | |
case ((a, b), (c, d)) => ('_', b + d) | |
} | |
val wordsAmount = (mapped reduce combiner)._2 | |
println(wordsAmount) | |
} |
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
public class WordCounterImpl implements WordCounter {
@Override
public int count(File file) throws IOException {
List<String> lines = Files.readLines(file, Charset.defaultCharset());
int wordsAmount = 0;
for (String line : lines) {
String[] tokens = line.split(" ");
wordsAmount += tokens.length;
}
return wordsAmount;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Words Count on plain Scala