Download presentation
Presentation is loading. Please wait.
1
Sadalage & Fowler (Amazon)
Graphdatabases.com (free!)
2
From Fowler
3
By Nathan Hurst
4
From Robinson
5
From Fowler
6
From Robinson
7
From Fowler
8
From Robinson
9
From Wikipedia
10
From Fowler
11
From Robinson
16
function map() { // get content of the current document var cnt = this.content; // split the content into an array of words using a regular expression var words = cnt.match(/\w+/g); // if there are no words, return if (words == null) { return; } // for each word, output {word, count} pair for (var i = 0; i < words.length; i++) { emit({ word:words[i] }, { count:1 }); From Isuru Suriarachchi's Blog
17
Format of the documents in our collection:
{ "_id" : ObjectId("519f6c1f44ae9aea a"), "pageId" : "page1", "content" : "your page1 content" } From Isuru Suriarachchi's Blog
18
function reduce(key, counts) {
var cnt = 0; // loop through call count values for (var i = 0; i < counts.length; i++) { // add current count to total cnt = cnt + counts[i].count; } // return total count return { count:cnt }; From Isuru Suriarachchi's Blog
19
From Isuru Suriarachchi's Blog
public class WordCount { public static void main(String[] args) { try { // create a MongoClient by connecting to the // MongoDB instance in localhost MongoClient mongoClient = new MongoClient("localhost", 27017); // access the db named "sample" DB db = mongoClient.getDB("sample"); // access the input collection DBCollection collection = db.getCollection("book"); // read Map file String map = readFile("wc_map.js"); // read Reduce file String reduce = readFile("wc_reduce.js"); // execute MapReduce on the input collection and // direct the result to "wordcounts" collection collection.mapReduce(map, reduce, "wordcounts", MapReduceCommand.OutputType.REPLACE, null); } catch (Exception e) { e.printStackTrace(); } From Isuru Suriarachchi's Blog
20
From Isuru Suriarachchi's Blog
/** * Reads the specified file from classpath */ private static String readFile(String fileName) throws IOException { // get the input stream InputStream fileStream = WordCount.class.getResourceAsStream("/" + fileName); // create a buffer with some default size byte[] buffer = new byte[8192]; // read the stream into the buffer int size = fileStream.read(buffer); // create a string for the needed size and return return new String(buffer, 0, size); } From Isuru Suriarachchi's Blog
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.