16 - nosqlcs61/lectures/16 - nosql/16 - nosql.pdf · from isuru suriarachchi's blog /** *...

21
From Fowler

Upload: others

Post on 21-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Fowler

Page 2: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

// customer info collection { "id":42, "fname":"ebenezer", "mi":"j", "lname":"coot", "addresses": [ { "addrType":"billingaddress", "streetaddr":"2103 Xenon Way", "city":"Santa Fe", "St":"NM" }, { "addrType": "shippingaddress", "streetaddr":"42 Wrinkled Way", "city":"Taos", "St":"NM" }] }

// carts { "id":74829312, "customer":42, "itemlist":[ { "UPC":293012429, "price":79.95, "name":"apple 85w power adapter", "shippingspeed":"2nd day" }, { "UPC":829381427, "price":59.95, "name":"apple touchpad mouse" } ], "paymentinfo":[ { "ccard":"1234-5678-9876-5432", "exp":"0115", "ccxact":"111111" } ] }

Page 3: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

By Nathan Hurst

Page 4: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Robinson

Page 5: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Fowler

Page 6: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Robinson

Page 7: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Fowler

Page 8: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Robinson

Page 9: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Wikipedia

Page 10: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Fowler

Page 11: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Robinson

Page 12: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

// customer info collection { "id":42, "fname":"ebenezer", "mi":"j", "lname":"coot", "addresses": [ { "addrType":"billingaddress", "streetaddr":"2103 Xenon Way", "city":"Santa Fe", "St":"NM" }, { "addrType": "shippingaddress", "streetaddr": "42 Wrinkled Way", "city":"Taos", "St":"NM" }] }

// carts { "id":74829312, "customer":42, "itemlist":[ { "UPC":293012429, "price":79.95, "name":"apple 85w power adapter", "shippingspeed":"2nd day" }, { "UPC":829381427, "price":59.95, "name":"apple touchpad mouse" } ], "paymentinfo":[ { "ccard":"1234-5678-9876-5432", "exp":"0115", "ccxact":"111111" } ] }

MONGODB EXAMPLE

Page 13: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)
Page 14: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)
Page 15: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)
Page 16: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)
Page 17: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

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

Page 18: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

From Isuru Suriarachchi's Blog

{ "_id" : ObjectId("519f6c1f44ae9aea2881672a"), "pageId" : "page1", "content" : "your page1 content" }

Format of the documents in our collection:

Page 19: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

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

Page 20: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

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(); } }

Page 21: 16 - NoSQLcs61/Lectures/16 - NoSQL/16 - NoSQL.pdf · From Isuru Suriarachchi's Blog /** * Reads the specified file from classpath */ private static String readFile(String fileName)

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); }}