使用mongodb存取lbs数据
1,在mongodb中创建lbs_db数据库,collection名称lbs_info,要使用lbs查询功能,需要对二维数据列建立索引
db.lbs_info.ensureIndex( { locs : "2d" } );
2,Servlet源码如下:
package com.ciaos.lbs; import java.io.IOException; import java.net.UnknownHostException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; public class LBSServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private Mongo mongo = null; final private String MongoDbIp = "127.0.0.1"; final private Integer MongoDbPort = 27017; final private String MongoDbName = "lbs_db"; @Override public void init() throws ServletException { // TODO Auto-generated method stub try { mongo=new Mongo(MongoDbIp, MongoDbPort); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } super.init(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uid = req.getParameter("uid"); String lon = req.getParameter("lon"); String lat = req.getParameter("lat"); if(uid == null || lon == null || lat == null){ resp.sendError(400,"bad request"); return; } int skip = 0, limit = 10; try{ skip = Integer.parseInt(req.getParameter("skip")); limit = Integer.parseInt(req.getParameter("limit")); } catch(Exception ex){ skip = 0; limit = 10; } try{ DB db=mongo.getDB(MongoDbName); DBCollection col = db.getCollection("lbs_info"); BasicDBList dbl = new BasicDBList(); dbl.add(Double.parseDouble(lon)); dbl.add(Double.parseDouble(lat)); BasicDBObject query = new BasicDBObject("locs", new BasicDBObject("$near", dbl)); BasicDBObject filters = new BasicDBObject("_id", false); DBCursor cursor = col.find(query, filters).skip(skip).limit(limit); resp.getWriter().println(cursor.toArray().toString()); resp.getWriter().flush(); }catch(Exception ex){ resp.sendError(500,"internal server error"); return; } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uid = req.getParameter("uid"); String lon = req.getParameter("lon"); String lat = req.getParameter("lat"); if(uid == null || lon == null || lat == null){ resp.sendError(400,"bad request"); return; } try{ DB db=mongo.getDB(MongoDbName); DBCollection col = db.getCollection("lbs_info"); BasicDBList dbl = new BasicDBList(); dbl.add(Double.parseDouble(lon)); dbl.add(Double.parseDouble(lat)); BasicDBObject query = new BasicDBObject("uid", uid); DBObject setValue=new BasicDBObject(); setValue.put("utime", System.currentTimeMillis()); setValue.put("locs", dbl); DBObject upsertValue=new BasicDBObject("$set",setValue); col.update(query, upsertValue, true, true); resp.getWriter().println("ok"); resp.getWriter().flush(); }catch(Exception ex){ resp.sendError(500,"internal server error"); return; } } }
3,使用示例
1,插入或更新数据 curl -XPOST -v "http://127.0.0.1:8080/lbs?uid=1001&lon=1&lat=1" curl -XPOST -v "http://127.0.0.1:8080/lbs?uid=1002&lon=2&lat=2" curl -XPOST -v "http://127.0.0.1:8080/lbs?uid=1003&lon=3&lat=3" curl -XPOST -v "http://127.0.0.1:8080/lbs?uid=1004&lon=4&lat=4" curl -XPOST -v "http://127.0.0.1:8080/lbs?uid=1005&lon=5&lat=5" 2,查询距离最近的数据 curl -XGET -v "http://127.0.0.1:8080/lbs?uid=1006&lon=3&lat=3 [{ "_id" : { "$oid" : "532786efb84743574afdd26f"} , "locs" : [ 3.0 , 3.0] , "uid" : "1003" , "utime" : 1395476404876}, { "_id" : { "$oid" : "532786e8b84743574afdd26e"} , "locs" : [ 2.0 , 2.0] , "uid" : "1002" , "utime" : 1395476397434}, { "_id" : { "$oid" : "532786f8b84743574afdd270"} , "locs" : [ 4.0 , 4.0] , "uid" : "1004" , "utime" : 1395476413299}, { "_id" : { "$oid" : "5327868db84743574afdd26d"} , "locs" : [ 1.0 , 1.0] , "uid" : "1001" , "utime" : 1395476436048}, { "_id" : { "$oid" : "5327885db84743574afdd271"} , "locs" : [ 6.0 , 6.0] , "uid" : "1006" , "utime" : 1395476769272}] 3,mongodb命令行查询 rs0:PRIMARY> db.lbs_info.find(); { "_id" : ObjectId("5327868db84743574afdd26d"), "locs" : [ 1, 1 ], "uid" : "1001", "utime" : NumberLong("1395476436048") } { "_id" : ObjectId("532786e8b84743574afdd26e"), "locs" : [ 2, 2 ], "uid" : "1002", "utime" : NumberLong("1395476397434") } { "_id" : ObjectId("532786efb84743574afdd26f"), "locs" : [ 3, 3 ], "uid" : "1003", "utime" : NumberLong("1395476404876") } { "_id" : ObjectId("532786f8b84743574afdd270"), "locs" : [ 4, 4 ], "uid" : "1004", "utime" : NumberLong("1395476413299") } { "_id" : ObjectId("5327885db84743574afdd271"), "locs" : [ 6, 6 ], "uid" : "1006", "utime" : NumberLong("1395476769272") } rs0:PRIMARY> db.lbs_info.find( { locs : {$near:[0,0]}, uid: {$gt:"1001"}}).limit(3) { "_id" : ObjectId("532786e8b84743574afdd26e"), "locs" : [ 2, 2 ], "uid" : "1002", "utime" : NumberLong("1395476397434") } { "_id" : ObjectId("532786efb84743574afdd26f"), "locs" : [ 3, 3 ], "uid" : "1003", "utime" : NumberLong("1395476404876") } { "_id" : ObjectId("532786f8b84743574afdd270"), "locs" : [ 4, 4 ], "uid" : "1004", "utime" : NumberLong("1395476413299") }