public void remove(String collection, DBObject query) { DBCollection dbCollection = getCollection(collection); dbCollection.remove(query); Jedis jedis = JedisManager.getJedis(); jedis.zrem("expire", query.toString()); jedis.hdel(collection, query.toString()); JedisManager.returnJedis(jedis); }
public void updateDocument(String collection, DBObject query, DBObject document) { DBCollection dbCollection = getCollection(collection); dbCollection.update(query, document); Jedis jedis = JedisManager.getJedis(); if (jedis.hexists(collection, query.toString())) { jedis.hset(collection, query.toString(), findOneNoCache(collection, query).toString()); jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query); } JedisManager.returnJedis(jedis); }
public void delete(String collection, DBObject query) { DBCollection dbCollection = getCollection(collection); dbCollection.remove(query); Jedis jedis = JedisManager.getJedis(); if (jedis.hexists(collection, query.toString())) { jedis.hdel(collection, query.toString(), findOneNoCache(collection, query).toString()); jedis.zrem("expire", collection + "_" + query); } JedisManager.returnJedis(jedis); }
@Override public String Json(String nombreDB, int clienteId) throws UnknownHostException, JSONException { // TODO Auto-generated method stub MongoClient mongoClient = new MongoClient("localhost", 27017); DB base = mongoClient.getDB(nombreDB); DBCollection collection = base.getCollection("Json"); BasicDBObject query = new BasicDBObject(); query.put("id", clienteId); DBCursor cursor = collection.find(query); if (cursor.size() == 0) { // System.out.println("********************\n"); // System.out.println("No existe el cliente, no se puede ingresar json"); // System.out.println("********************\n"); return "No existe el cliente, no se puede ingresar json"; } // Existe el cliente DBObject objeto = (DBObject) cursor.next(); DBObject json = (DBObject) objeto.get("json"); // DBObject j = JSON.parse(json.toString()); JSONObject aj = new JSONObject(json.toString()); return aj.toString(); }
@Override public void loadPlayer(UUID uuid) { service.submit( () -> { DBCursor cursor = collection.find(new BasicDBObject("uuid", uuid)); GamePlayer player = null; if (cursor.hasNext()) { DBObject next = cursor.next(); String json = next.toString(); player = GSON.fromJson(json, new TypeToken<GamePlayer>() {}.getType()); } if (player == null) { player = new GamePlayer(uuid); } PlayerRegistry.registerPlayer(player); }); }
public DBObject findOne(String collection, DBObject query) { Jedis jedis = JedisManager.getJedis(); Set<String> expiredKeys = jedis.zrangeByScore("expire", 0, System.currentTimeMillis()); if (expiredKeys.isEmpty() == false) { String[] keyArray = new String[expiredKeys.size()]; expiredKeys.toArray(keyArray); jedis.del(keyArray); jedis.zrem("expire", keyArray); } JSONObject jsonObject = null; if (jedis.hexists(collection, query.toString())) { String data = jedis.hget(collection, query.toString()); if (data != null) { try { jsonObject = new JSONObject(data); } catch (JSONException e) { e.printStackTrace(); } } } DBObject dbObject; if (jsonObject == null) { DBCollection dbCollection = getCollection(collection); DBCursor dbCursor = dbCollection.find(query).limit(1); if (dbCursor.hasNext() == false) { return null; } dbObject = dbCursor.next(); dbCursor.close(); jedis.hset(collection, query.toString(), dbObject.toString()); jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query); } else { dbObject = (DBObject) JSON.parse(jsonObject.toString()); jedis.hset(collection, query.toString(), dbObject.toString()); jedis.zadd("expire", System.currentTimeMillis() + 300000, collection + "_" + query); } JedisManager.returnJedis(jedis); return dbObject; }
public static void main(String[] args) { // TODO Auto-generated method stub try { MongoClient mc = new MongoClient("localhost"); DB db = mc.getDB("mydb"); DBCollection dbc = db.getCollection("books"); /*BasicDBObject book= new BasicDBObject(); book.put("name", "자바의 정석"); book.put("pages", 600); dbc.insert(book); book= new BasicDBObject(); book.put("name", "스프링4"); book.put("pages", 250); dbc.insert(book); book= new BasicDBObject(); book.put("name", "JQuery"); book.put("pages", 150); dbc.insert(book); book= new BasicDBObject(); book.put("name", "오라클 프로그램"); book.put("pages", 800); dbc.insert(book); book= new BasicDBObject(); book.put("name", "AngularJS"); book.put("pages", 130); dbc.insert(book); book= new BasicDBObject(); book.put("name", "MongoDB"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "Hadoop"); book.put("pages", 300); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HIVE"); book.put("pages", 350); dbc.insert(book); book= new BasicDBObject(); book.put("name", "PIG"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HTML5"); book.put("pages", 360); dbc.insert(book);*/ String map = "function(){" + "var category;" + "if(this.pages>=300){" + "category='Big Books';" + "}else{" + "category='Small Books';}" + "emit(category,{name:this.name});}"; String reduce = "function(key,values){" + "var sum=0;" + "values.forEach(function(doc){" + "sum+=1;" + "});" + "return {books:sum};}"; MapReduceCommand cmd = new MapReduceCommand(dbc, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = dbc.mapReduce(cmd); for (DBObject o : out.results()) { System.out.println(o.toString()); } } catch (Exception ex) { System.out.println(ex.getMessage()); } }