Created
January 15, 2012 07:25
-
-
Save zaneli/1614851 to your computer and use it in GitHub Desktop.
「ドキュメント指向データベース MongoDB を少しだけ触ってみる(2)」ブログ用
This file contains hidden or 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.zaneli.mongodb; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.commons.codec.binary.Base64; | |
import org.bson.BSON; | |
import org.bson.types.Code; | |
import org.bson.types.CodeWScope; | |
import org.bson.types.Symbol; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.Bytes; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.DBCursor; | |
import com.mongodb.DBObject; | |
import com.mongodb.Mongo; | |
import com.mongodb.MongoException; | |
import com.mongodb.WriteResult; | |
public class MongoDBOperator { | |
private final DB db; | |
public MongoDBOperator(String dbName) throws MongoException, UnknownHostException { | |
this("localhost", 27017, dbName); | |
} | |
public MongoDBOperator(String host, int port, String dbName) throws MongoException, UnknownHostException { | |
Mongo mongo = new Mongo(host, port); | |
this.db = mongo.getDB(dbName); | |
} | |
public List<Map<String, String>> find(String collectionName) { | |
DBCollection col = db.getCollection(collectionName); | |
DBCursor cursor = col.find(); | |
List<Map<String, String>> result = new ArrayList<Map<String, String>>(); | |
while (cursor.hasNext()) { | |
Map<String, String> map = new HashMap<String, String>(); | |
DBObject obj = cursor.next(); | |
createRecordMap(obj, map); | |
result.add(map); | |
} | |
return result; | |
} | |
public Map<String, String> findOne(String collectionName) { | |
DBCollection col = db.getCollection(collectionName); | |
DBObject obj = col.findOne(); | |
Map<String, String> result = new HashMap<String, String>(); | |
createRecordMap(obj, result); | |
return result; | |
} | |
private void createRecordMap(DBObject obj, Map<String, String> map) { | |
for (String key : obj.keySet()) { | |
Object value = obj.get(key); | |
byte type = Bytes.getType(value); | |
switch (type) { | |
case BSON.BINARY: | |
map.put(key, Base64.encodeBase64String((byte[]) value)); | |
break; | |
case BSON.CODE: | |
map.put(key, ((Code) value).getCode()); | |
break; | |
case BSON.CODE_W_SCOPE: | |
map.put(key, ((CodeWScope) value).getCode() + ", " + ((CodeWScope) value).getScope().toString()); | |
break; | |
case BSON.NULL: | |
map.put(key, "null"); | |
break; | |
case BSON.SYMBOL: | |
map.put(key, ((Symbol) value).getSymbol()); | |
break; | |
default: | |
map.put(key, value.toString()); | |
} | |
} | |
} | |
public void insert(String collectionName, @SuppressWarnings("rawtypes") Map record) { | |
DBObject obj = new BasicDBObject(); | |
obj.putAll(record); | |
insert(collectionName, obj); | |
} | |
public void insert(String collectionName, DBObject obj) { | |
DBCollection col = db.getCollection(collectionName); | |
col.insert(obj); | |
} | |
} |
This file contains hidden or 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.zaneli.mongodb; | |
import com.mongodb.BasicDBObject; | |
@SuppressWarnings("serial") | |
public class User extends BasicDBObject { | |
private static final String KEY_NAME = "name"; | |
private static final String KEY_AGE = "age"; | |
public User(String name, int age) { | |
super.put(KEY_NAME, name); | |
super.put(KEY_AGE, age); | |
} | |
public String getName() { | |
return super.getString(KEY_NAME); | |
} | |
public int getAge() { | |
return super.getInt(KEY_AGE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment