Thursday, March 20, 2014

Eclipse - Specified VM install not found: type Standard VM, name XXX

Solution 1

- suppose the path of workspace of eclipse is 'D:\eclipse\workspace'
- then, go to  'D:\eclipse\workspace\.metadata\.plugins\org.eclipse.debug.core\.launches'
- find 'projectName build.xml.launch' file and delete it

Solution 2

- Right Click on build.xml
- Go to "Run As" >> "External Tools Configurations..."
- It shall open new window
- Go to JRE tab
- Select proper JRE if missing

MongoDB 2.4 - Java (Fail over)

import java.net.UnknownHostException;
import java.util.Arrays;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException,
InterruptedException {
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(
"localhost", 27017), new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DBCollection test = client.getDB("database")
.getCollection("collection");

test.drop();

for (int i = 0; i < Integer.MAX_VALUE; i++) {
for (int retries = 0; retries < 3; retries++) {
try {
test.insert(new BasicDBObject("_id", i));
System.out.println("Inserted document: " + i);
break;
} catch (MongoException.DuplicateKey e) {
System.out.println("Document already inserted " + i);
} catch (MongoException e) {
System.out.println(e.getMessage());
System.out.println("Retrying");
Thread.sleep(5000);
}
}

Thread.sleep(500);
}
}
}

MongoDB 2.4 - Java (connecting to a replica set)

import java.net.UnknownHostException;
import java.util.Arrays;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException,
InterruptedException {
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(
"localhost", 27017), new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DBCollection test = client.getDB("database")
.getCollection("collection");

test.drop();

for (int i = 0; i < Integer.MAX_VALUE; i++) {
test.insert(new BasicDBObject("_id", i));
System.out.println("Inserted document: " + i);
Thread.sleep(500);
}
}
}

Wednesday, March 19, 2014

Ubuntu 12.04 - link /var/lib/postgresql to another partition's directory

- sudo service postgresql stop
- sudo cp -rf /var/lib/postgresql /anotherPartition/postgresql
- sudo chwon -R postgres:postgres /anotherPartition/postgresql
- sudo service postgresql start

Thursday, March 13, 2014

Mongo 2.4.9 - MapReduce

1. Map

function map_closest() {
    var pitt = [-80.064879, 40.612044];
    var phil = [-74.978052, 40.089738];

    function distance(a, b) {
        var dx = a[0] - b[0];
        var dy = a[1] - b[1];
        return Math.sqrt(dx * dx + dy * dy);
    }

    if (distance(this.loc, pitt) < distance(this.loc, phil)) {
        emit("pitt", 1);
    } else {
        emit("phil", 1);
    }
}

2. Reduce

2.1. Array.sum()

function reduce_closest(city, counts) {  
  return Array.sum(counts); 
}

2.2. for()

function reduce_closest(city, counts) {
 var total = 0;
 var length = counts.length;

 for (var i = 0; i < length; i++) {
  total += counts[i];
 }

 return total;
}

2.3. forEach()

function reduce_closest(city, counts) {
 var total = 0;

 counts.forEach(function(count) {
  total += count;
 });

 return total;
}

3. mapReduce

db.zips.mapReduce(
  map_closest, 
  reduce_closest, 
  { 
    query: { state: 'PA' }, 
    out: { inline: 1 } 
  }
)

Mongo 2.4.9 - $elemMatch

- db.policies.find( { status : { $ne : "expired" }, coverages : { $elemMatch : { type : "liability", rates : { $elemMatch : { rate : { $gte : 100 }, current : true } } } } } ).pretty()

{
        "_id" : "1024850AB",
        "status" : "draft",
        "insured_item" : {
                "make" : "Cessna",
                "model" : "Skylane",
                "year" : 1982,
                "serial" : "AB1783A"
        },
        "insured_parties" : [
                ObjectId("5097f7351d9a5941f5111d61")
        ],
        "coverages" : [
                {
                        "type" : "liability",
                        "limit" : 1000000,
                        "rates" : [
                                {
                                        "rate" : 200,
                                        "staff_id" : ObjectId("5097f7351d9a5999f5111d69"),
                                        "date" : ISODate("2012-11-05T17:29:54.561Z"),
                                        "current" : true
                                }
                        ]
                },
                {
                        "type" : "property",
                        "deductible" : 5000,
                        "limit" : 100000,
                        "rates" : [
                                {
                                        "rate" : 300,
                                        "staff_id" : ObjectId("5097f7351d9a5999f5111d69"),
                                        "date" : ISODate("2012-11-05T17:29:56.561Z"),
                                        "current" : true
                                }
                        ]
                }
        ],
        "underwriting" : {
                "staff_id" : ObjectId("5097f84cf8dd729bc7273068"),
                "action" : "approved",
                "date" : ISODate("2012-11-05T17:33:00.693Z")
        }
}

Mongo 2.4.9 - Command

- mongoimport -d database -c collection --drop example.json

- mongo example.js
- mongo --shell example.js
- mongo --shell database example.js