Archive

Archive for the ‘Uncategorized’ Category

Eclipse cannot connect to marketplace

 

download

If you are using windows 8, you might have come up with this issue. After hours of Googling, finally found a solution for this.

  1. Open your eclipse.ini file.
  2. Add following parameter in the bottom and save.

-Djava.net.preferIPv4Stack=true

3.Restart Eclipse.

Advertisements
Categories: Uncategorized

Importing .JSON file into your remote mongoDB database with MongoHQ

mongohq

MongoHQ is the first DBaaS (Database as a Service) of its kind. It provides free hosting space to your non-commercial mongo powered apps. First register at mongoHQ and create your database. Then they will provide the details for the terminal operations as follows.

mongo widmore.mongohq.com:10010/course -u <user> -p<password>

Here course is my db name. Now Lets import the following grades.json file into the course db.

<br />{ "student" : "Joe", "assignment" : "hw1", "grade" : 90 }<br />{ "student" : "Joe", "assignment" : "hw2", "grade" : 80 }<br />{ "student" : "Joe", "assignment" : "hw3", "grade" : 85 }<br />{ "student" : "Joe", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Steve", "assignment" : "hw1", "grade" : 80 }<br />{ "student" : "Steve", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Steve", "assignment" : "hw3", "grade" : 100 }<br />{ "student" : "Steve", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Amanda", "assignment" : "hw1", "grade" : 100 }<br />{ "student" : "Amanda", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Amanda", "assignment" : "hw3", "grade" : 80 }<br />{ "student" : "Amanda", "assignment" : "exam", "grade" : 100 }<br />{ "student" : "Susan", "assignment" : "hw1", "grade" : 100 }<br />{ "student" : "Susan", "assignment" : "hw2", "grade" : 90 }<br />{ "student" : "Susan", "assignment" : "hw3", "grade" : 85 }<br />{ "student" : "Susan", "assignment" : "exam", "grade" : 80 }<br />

Go to the terminal and execute the following command.

mongoimport -h widmore.mongohq.com --port 10010 -d course -c grades -u supun -p ******** --type json --file /Desktop/grades.json

Here are the list of options which have used above.

Option Description
-h This parameter specifies the host (or server) name.
--port This parameter specifies the port on the host that the MongoDB database is listening on. It is --portinstead of -p to prevent confusion with the password parameter.
-d The name of your database.
-c The name of the collection to import the CSV file into.
-u Since MongoHQ requires authentication, the database username that you specified.
-p The database password that you specified.
--type The format of the file to be imported … generally CSV or JSON.
--file The physical location of the file … generally on your computer.
--headerline This simply tells MongoDB to disregard the first line of the CSV file.

Now lets query this using our nodejs application.

app.js

var MongoClient = require('mongodb').MongoClient;MongoClient.connect('mongodb://supun:********@widmore.mongohq.com:10010/course', function(err, db) {<br /> if(err) throw err;//var query = { 'grade' : 100 };</p><p>db.collection('grades').find().sort([["grade",-1]]).toArray(function(err, docs) {<br /> if(err) throw err;</p><p>console.dir(docs);db.close();<br /> });<br />});

Now execute npm install mongodb

then node app.js

Here is the result.

{ _id: ObjectId("521c44526b39dd11b60564d7"), student: "Joe", assignment: "exam", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564da"), student: "Steve", assignment: "hw3", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564db"), student: "Steve", assignment: "exam", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564dc"), student: "Amanda", assignment: "hw1", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564df"), student: "Amanda", assignment: "exam", grade: 100 }\{ _id: ObjectId("521c44526b39dd11b60564e0"), student: "Susan", assignment: "hw1", grade: 100 }{ _id: ObjectId("521c44526b39dd11b60564d4"), student: "Joe", assignment: "hw1", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564d9"), student: "Steve", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564dd"), student: "Amanda", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564e1"), student: "Susan", assignment: "hw2", grade: 90 }{ _id: ObjectId("521c44526b39dd11b60564d6"), student: "Joe", assignment: "hw3", grade: 85 }{ _id: ObjectId("521c44526b39dd11b60564e2"), student: "Susan", assignment: "hw3", grade: 85 }{ _id: ObjectId("521c44526b39dd11b60564d5"), student: "Joe", assignment: "hw2", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564d8"), student: "Steve", assignment: "hw1", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564de"), student: "Amanda", assignment: "hw3", grade: 80 }{ _id: ObjectId("521c44526b39dd11b60564e3"), student: "Susan", assignment: "exam", grade: 80 }
Categories: Uncategorized

Connect MongoDB with Java

 

This post is about how to connect MongoDB server through java. Before that here are some tricks which may be useful to work with MongoDB.

Before running  the MongoDB server, make sure to create following folders in C.

C:datadb.

And after running MongoDB server, if you close the window forcibly (Without pressing Ctrl+C),  you won’t be able to run it again. Here is the solution. Go to C:datadb and delete the file “mongod.lock”. 😀

So, here is how to connect MongoDB server using java.
Download the latest mongoDB java driver from  here. Create a  new java project in eclipse. Add a user library to add mongodb  driver.

Then add following to the code.


package com.technodyne.core;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class App {

 public static void main(String[] args) {

 try {
 // connect to mongoDB, ip and port number
 Mongo mongo = new Mongo("localhost", 27017);

 // get database from MongoDB,
 // if database doesn't exists, mongoDB will create it automatically
 DB db = mongo.getDB("technodyne");

 // Get collection from MongoDB, database named "yourDB"
 // if collection doesn't exists, mongoDB will create it
 // automatically
 DBCollection collection = db.getCollection("MyCollection");

 // create a document to store key and value
 BasicDBObject document = new BasicDBObject();
 document.put("id", 1001);
 document.put("msg", "hello world mongoDB in Java");

 // save it into collection named "MyCollection"
 collection.insert(document);

 // search query
 BasicDBObject searchQuery = new BasicDBObject();
 searchQuery.put("id", 1001);

 // query it
 DBCursor cursor = collection.find(searchQuery);

 // loop over the cursor and display the retrieved result
 while (cursor.hasNext()) {
 System.out.println(cursor.next());
 }

 System.out.println("Done");

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

 }

}

Then run the application. The result is below.

{ “_id” : { “$oid” : “50423e3b28b389ac6e3c3aa7”} , “id” : 1001 , “msg” : “hello world mongoDB in Java”}
Done

Categories: Uncategorized