List of usage examples for com.mongodb DefaultDBCallback DefaultDBCallback
public DefaultDBCallback(final DBCollection collection)
From source file:com.edgytech.umongo.DocumentDeserializer.java
License:Apache License
public DBObject readObject() throws IOException { if (first) {// ww w .j a v a 2s .c o m if (format != Format.BSON) { if (is == null) { FileReader fr = new FileReader(file); br = new BufferedReader(fr); } else { br = new BufferedReader(new InputStreamReader(is)); } if (format == Format.CSV) { fields = br.readLine(); if (fields != null) { filter = fields.split(delimiter); // field names are never quoted for (int i = 0; i < filter.length; ++i) { filter[i] = filter[i].trim(); } } } } else { if (is == null) { is = new FileInputStream(file); } callback = new DefaultDBCallback(null); decoder = new BasicBSONDecoder(); } if (format == Format.JSON_ARRAY) { String line = br.readLine(); BasicDBList list = (BasicDBList) JSON.parse(line); iterator = list.iterator(); } first = false; } if (format == Format.JSON_ARRAY) { if (iterator == null || !iterator.hasNext()) { return null; } return (DBObject) iterator.next(); } DBObject obj = null; if (format != Format.BSON) { String line = br.readLine(); if (line == null) { return null; } if (format == Format.JSON_SINGLE_DOC) { // keep reading all lines String line2 = null; while ((line2 = br.readLine()) != null) { line += line2; } } if (format == Format.CSV) { List<String> values = splitByCommasNotInQuotes(line); if (template == null) { obj = new BasicDBObject(); // set each field defined for (int i = 0; i < filter.length; ++i) { String val = values.get(i); // string values are always quoted obj.put(filter[i], JSON.parse(val)); } } else { obj = (BasicDBObject) template.copy(); fillInTemplate(obj, values); } } else { obj = (DBObject) JSON.parse(line); } } else { // BSON is binary callback.reset(); try { decoder.decode(is, callback); } catch (IOException e) { // most likely EOF return null; } obj = (DBObject) callback.get(); // // read length // byte[] buf = new byte[4096]; // int n = fis.read(buf, 0, 4); // if (n <= 0) { // return null; // } // int len = Bits.readInt(buf); // // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // baos.write(buf, 0, 4); // int toread = len; // while (toread > 0) { // n = fis.read(buf, 0, Math.min(toread, buf.length)); // if (n <= 0) { // break; // } // baos.write(buf, 0, n); // toread -= n; // } // if (baos.size() != len) // throw new IOException("Lenght of read object " + baos.size() + " does not match expected size " + len); // obj = new BasicDBObject((BasicBSONObject) BSON.decode(baos.toByteArray())); } return obj; }
From source file:com.ikanow.infinit.e.data_model.store.SizeReportingBasicBSONDecoder.java
License:Open Source License
@Override public DBCallback getDBCallback(DBCollection collection) { return new DefaultDBCallback(collection); }
From source file:me.yyam.mongodbutils.MongoDbOperater.java
public void insert(String dbName, String colName, String json) { DB db = mongoClient.getDB(dbName);// w w w . j ava 2 s . c o m DBCollection coll = db.getCollection(colName); BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll)); BasicDBObject record = new BasicDBObject(obj); coll.insert(record); }
From source file:me.yyam.mongodbutils.MongoDbOperater.java
public void update(String dbName, String colName, String findJson, String json) { DB db = mongoClient.getDB(dbName);//from ww w .ja v a 2 s . c o m DBCollection coll = db.getCollection(colName); BasicBSONObject locateObj = (BasicBSONObject) JSON.parse(findJson, new DefaultDBCallback(coll)); BasicDBObject locate = new BasicDBObject(locateObj); BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll)); BasicDBObject record = new BasicDBObject(obj); coll.update(locate, record, false, true); }
From source file:me.yyam.mongodbutils.MongoDbOperater.java
public static void main(String[] args) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); DBCollection coll = db.getCollection("rztest"); String sql = "select * from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123"; MongoDbOperater ope = new MongoDbOperater(); ope.setMongoClient(mongoClient);/*from w ww . j a v a 2 s . c om*/ QueryInfo info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); sql = "select * from algoflow_instance_log where functionName='f1' and reuseResult=false and returnCode=0 and action='LEAVE' order by timestamp desc limit 100"; info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); String updateSql = "update table1 set a=1, b='sdaf', c='2012-11-23 17:45:32' where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3')"; QueryInfo uinfo = ope.sql2QueryInfo("abc", updateSql); System.out.println(uinfo.debugStr()); String json = "{\"a\":123,\"b\":345}"; BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll)); BasicDBObject dbObj = new BasicDBObject(obj); System.out.println(dbObj); }
From source file:org.shenjitang.mongodbutils.MongoDbOperater.java
public static void main(String[] args) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); DBCollection coll = db.getCollection("rztest"); String sql = "select * from table1 where name='aaa' and age between 20 and 30 and haha = 'ss'"; //String sql = "select * from table1 where name='aaa' and age >= 20 and age <= 30"; MongoDbOperater ope = new MongoDbOperater(); ope.setMongoClient(mongoClient);/* w w w .j ava 2 s. co m*/ QueryInfo info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); sql = "select * from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123"; info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); sql = "select * from algoflow_instance_log where functionName='f1' and reuseResult=false and returnCode=0 and action='LEAVE' order by timestamp desc limit 100"; info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); String updateSql = "update table1 set a=1, b='sdaf', c='2012-11-23 17:45:32' where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3')"; QueryInfo uinfo = ope.sql2QueryInfo("abc", updateSql); System.out.println(uinfo.debugStr()); String json = "{\"a\":123,\"b\":345}"; BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll)); BasicDBObject dbObj = new BasicDBObject(obj); System.out.println(dbObj); }