设为首页 收藏本站
查看: 1628|回复: 0

[经验分享] 使用mongodb存取lbs数据

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-4-2 09:09:44 | 显示全部楼层 |阅读模式
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") }
复制代码


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-16659-1-1.html 上篇帖子: MongoDB数据库简介及安装 下篇帖子: Mongodb与Redis应用指标对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表