In [1]:
var conf = new SparkConf().setAppName("JavaScript word count").setMaster("local[*]"); 
var sparkContext = new SparkContext(conf);

In [23]:
var file = "../dream.txt";

var rdd = sparkContext.textFile(file).cache();

In [15]:
var rdd2 = rdd.flatMap(function(sentence) {
    return sentence.split(" ");
});

In [16]:
var rdd3 = rdd2.filter(function(word) {
    return word.trim().length > 0;
});

In [17]:
var rdd4 = rdd3.mapToPair(function(word) {
    return new Tuple(word, 1);
});

In [18]:
var rdd5 = rdd4.reduceByKey(function(a, b) {
    return a + b;
});

In [19]:
var rdd6 = rdd5.mapToPair(function(tuple) {
    return new Tuple(tuple[1]+0.0, tuple[0]);
})

In [22]:
var rdd7 = rdd6.sortByKey(false);
JSON.stringify(rdd7.take(10))


Out[22]:
[[34,"of"],[30,"the"],[19,"be"],[19,"to"],[19,"and"],[15,"will"],[12,"from"],[12,"I"],[11,"freedom"],[10,"that"]]

In [ ]: