Skip to content

Instantly share code, notes, and snippets.

@Krasnyanskiy
Last active July 13, 2016 22:00
Show Gist options
  • Save Krasnyanskiy/ad2b340180139b490056c383ecf4f2bb to your computer and use it in GitHub Desktop.
Save Krasnyanskiy/ad2b340180139b490056c383ecf4f2bb to your computer and use it in GitHub Desktop.
-scala: map-reduce
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)
}
@Krasnyanskiy
Copy link
Author

Krasnyanskiy commented Jul 13, 2016

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