Java tutorial
/* * 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 parser; import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import java.util.Arrays; import java.util.HashMap; import java.util.Set; /** * * @author madhatter */ public class mongodbContoller { final String PageDataCollectionName = "PageDataCollection"; final String PageDataDBName = "PageData"; MongoClient MongoDbClient; public mongodbContoller() { try { MongoDbClient = new MongoClient("localhost", 27017); System.out.println("MongoClient is ready.Debug string: \n " + MongoDbClient.debugString()); } catch (Exception e) { e.printStackTrace(); MongoDbClient = null; } } public void add_page_data(pageData PageData) { try { DB PageDataDB = MongoDbClient.getDB(PageDataDBName); if (!PageDataDB.collectionExists(PageDataCollectionName)) { DBObject DBOptions = BasicDBObjectBuilder.start().get(); PageDataDB.createCollection(PageDataCollectionName, DBOptions); } BasicDBObject PageDataDocument = new BasicDBObject(); //adds page url PageDataDocument.append("Web_Url", PageData.getPageUrl()); //adds page description if (PageData.getPageDescription() != null) { PageDataDocument.append("Page_Description", PageData.getPageDescription()); } //adds keyword frequencies if (PageData.get_keyword_frequencies_map() != null) { PageDataDocument.append("Keywords", new BasicDBObject(PageData.get_keyword_frequencies_map())); } //adds term frequencies if (PageData.get_term_frequencies_map() != null) { PageDataDocument.append("Terms", new BasicDBObject(PageData.get_term_frequencies_map())); } System.out.println("Inserting; \n " + PageDataDocument.toString()); System.out.println("Inserted " + PageDataDB.getCollection(PageDataCollectionName) .insert(PageDataDocument, WriteConcern.FSYNCED).getN() + " Documents"); System.out.println("Last error:\n" + PageDataDB.getLastError()); } catch (Exception e) { e.printStackTrace(); } } }