amazonwebcrawler.Mongo.java Source code

Java tutorial

Introduction

Here is the source code for amazonwebcrawler.Mongo.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package amazonwebcrawler;

import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author hwei
 */
//package com.mkyong.core;
public class Mongo {

    private DBCollection myTable;

    public Mongo() {
        //Constructor
        myTable = this.queryDb(null);
    }

    private DBCollection queryDb(String[] args) {
        try {

            /**** Connect to MongoDB ****/
            // Since 2.10.0, uses MongoClient
            MongoClientURI uri = new MongoClientURI(
                    "mongodb://heroku_73s67dx7:2d6hshk9a78f2dlfkachg486t7@ds011399.mlab.com:11399/heroku_73s67dx7");
            MongoClient mongo = new MongoClient(uri);

            /**** Get database ****/
            // if database doesn't exists, MongoDB will create it for you
            DB db = mongo.getDB("heroku_73s67dx7");

            /**** Get collection / table from 'testdb' ****/
            // if collection doesn't exists, MongoDB will create it for you
            DBCollection table = db.getCollection("products");
            return table;
        } catch (MongoException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void addProducts(Set<Map<String, Object>> prodList) {
        for (Map<String, Object> prod : prodList) {
            this.inputData(prod);
            this.searchData((String) prod.get("name"));
        }
    }

    public void inputData(Map<String, Object> product) {
        try {
            /**** Insert ****/
            // create a document to store key and value
            BasicDBObject document = new BasicDBObject();
            document.put("name", (String) product.get("name"));
            document.put("rating", (double) product.get("rating"));
            document.put("url", (String) product.get("url"));
            document.put("price", (String) product.get("price"));
            document.put("ID", (String) product.get("ID"));
            document.put("tag", (List<String>) product.get("tag"));
            document.put("review", (List<String>) product.get("review"));
            document.put("createdDate", new Date());
            removeData((String) product.get("name"));
            myTable.insert(document);
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }

    public void removeData(String name) {
        try {
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("name", name);

            DBCursor cursor = myTable.find(searchQuery);

            while (cursor.hasNext()) {
                myTable.remove(cursor.next());
            }
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }

    public void searchData(String name) {
        try {
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("name", name);

            DBCursor cursor = myTable.find(searchQuery);

            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }

        } catch (MongoException e) {
            e.printStackTrace();
        }
    }

    public void searchAndReplaceData(DBCollection table, String[] args) {
        try {
            BasicDBObject query = new BasicDBObject();
            query.put("name", "mkyong");

            BasicDBObject newDocument = new BasicDBObject();
            newDocument.put("name", "mkyong-updated");

            BasicDBObject updateObj = new BasicDBObject();
            updateObj.put("$set", newDocument);

            table.update(query, updateObj);
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }

}