Created
October 26, 2013 18:49
-
-
Save zynick/7173137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zynick.mongo; | |
import java.net.UnknownHostException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.DBCursor; | |
import com.mongodb.DBObject; | |
import com.mongodb.MongoClient; | |
import java.text.ParseException; | |
/** | |
* Play around with MongoDB | |
* @author zynick | |
*/ | |
public class MongoDBCollectionConversion { | |
static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddHHmm"); | |
public static void main(String[] args) throws UnknownHostException, ParseException { | |
DBCollection spaceCollection = getCollection("tide", "space"); | |
DBCollection spacesCollection = getCollection("tide", "spaces"); | |
DBCursor cur = spaceCollection.find(); | |
while (cur.hasNext()) { | |
// get data from space | |
DBObject obj = cur.next(); | |
// get and format date | |
String dateStr = (String) obj.get("date"); | |
Integer hourInt = (Integer) obj.get("hour"); | |
String hourStr = (hourInt < 10) ? "0" + hourInt : String.valueOf(hourInt); | |
Integer bucketInt = (Integer) obj.get("bucket"); | |
int minute = bucketInt * 5; | |
String minuteStr = (minute < 10) ? "0" + minute : String.valueOf(minute); | |
Date date = format.parse(dateStr + hourStr + minuteStr); | |
@SuppressWarnings("rawtypes") | |
List devices = (List) obj.get("devices"); | |
// insert data into spaces | |
spacesCollection.insert(new BasicDBObject("_id", date) | |
.append("devices", devices) | |
.append("origin", "sprezzaturacoffee")); | |
} | |
System.out.println("gaodim"); | |
} | |
public static DBCollection getCollection(String database, String collection) throws UnknownHostException { | |
MongoClient client = new MongoClient(); | |
DB db = client.getDB(database); | |
DBCollection dbCollection = db.getCollection(collection); | |
return dbCollection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment