基於mongodb的地理檢索實現

使用mongoDB不是很多,記得以前做“家長助手”的時候,使用過一點。只是在去年做“派單系統”的時候,又再一次使用mongoDB。

在這裏先簡單介紹一下派單系統,派單系統在雲足療(O2O,上門足療)裏一個專門負責訂單派送,提高訂單完成效率的一個系統。主要是當一個來了之後,會根據訂單的服務項目、服務時間和服務地點,快速找到最合適最優秀的技師,返回給用戶。由於上門足療特殊行業的要求,不能給訂單指定技師直接下單。而是將篩選的一些優秀的技師返回給用戶,讓用戶自己去選擇指派給哪位技師。

項目背景說完了之後,就是技術方案選擇的問題。一開始,是想基於HBase一個分佈式的、面向列的開源數據庫去存儲技師上報經緯度。然後通過storm流式計算技師的位置。後來,感覺HBase過重而且還不便於維護。同時,又考慮到小公司的都會面對的一個問題——成本問題。就放棄了這一種方案。然後,就想選擇一個非關係型數據庫去存住數據,這時候就想到了Mongodb。好了,這是數據層。分佈式架構我們使用的阿里的dubbo。選擇它的原因就不多說了。首先是,市面上使用廣泛吧。以後,會具體講述其特點。分佈式的管控使用的是zookeeper,跨系統中間件使用的是ActiveMq。這個還是要簡單說一下爲什麼要選擇這個,而沒有選擇市面上用的比較多的RocketMq。最重要的一個原因時以前的工作中使用過,而對MQ的應用場景是師傅端經緯度上報,消息的可靠性又不是很高,同時降低了學習成本。說到這裏,感覺說的太多了,今天主要說的是mongodb的應用。

接下來就寫一個基於mongodb的地理檢索的實現吧!

Controller層

//查詢附近
@ResponseBody
@RequestMapping(value = "/geoNearN", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
public String testQueryMongoTopN() {
    CarPointNearQuery personQuery = new CarPointNearQuery();
    Random random = new Random();
    double[] arr = MongoUtil.getRandomLocation();
    //最多查100條記錄
    personQuery.setCount(100);
    //隨機1km米到10km
    int distance = random.nextInt(10);
    personQuery.setDistance(distance);
    personQuery.setLongitude(arr[0]);
    personQuery.setLatitude(arr[1]);
    return JSON.toJSONString(mongoLbsService.geoNearCarPoint(personQuery));
}

Service層

public CarPointNearResult geoNearCarPoint(CarPointNearQuery carPointNearQuery) {
    CarPointNearResult carPointNearResult = new CarPointNearResult();
    if(carPointNearQuery != null && carPointNearQuery.getLongitude() != 0.0D && carPointNearQuery.getLatitude() != 0.0D) {
        Point point = new Point(carPointNearQuery.getLongitude(), carPointNearQuery.getLatitude());
        NearQuery near = NearQuery.near(point, Metrics.KILOMETERS);
        Query query = new Query();
        //數量
        query.limit(carPointNearQuery.getCount() == 0?100:carPointNearQuery.getCount());
        near.query(query);
        //距離
        near.maxDistance(new Distance(carPointNearQuery.getDistance() == 0.0D?1.0D:carPointNearQuery.getDistance(), Metrics.KILOMETERS));
        near.spherical(true);
        //調用DAO層,獲取數據
        GeoResults geoResults = this.carPointDao.geoNear(near, CarPoint.class, "carPoint");

        carPointNearQuery.setCount(geoResults.getContent().size());
        carPointNearQuery.setDistance(near.getMaxDistance().getValue());
        carPointNearResult.setCarPointNearQuery(carPointNearQuery);
        List geoResultsContent = geoResults.getContent();
        ArrayList resultsList = new ArrayList();
        Iterator i$ = geoResultsContent.iterator();
        while(i$.hasNext()) {
            GeoResult geoResult = (GeoResult)i$.next();
            CarPointResult carPointResult = new CarPointResult();
            carPointResult.setDistance(geoResult.getDistance().getValue());
            carPointResult.setCarPoint((CarPoint)geoResult.getContent());
            resultsList.add(carPointResult);
        }

        carPointNearResult.setCarPointList(resultsList);
        return carPointNearResult;
    } else {
        logger.error("geoNear 參數異常");
        carPointNearResult.setErrorCode(ErrorCode.PARAM_ERROR);
        return null;
    }
}

DAO層

public GeoResults<T> geoNear(NearQuery near, Class<T> clazz, String collectionName) {
    //直接使用mongoTemplate就可以了
    GeoResults geoResults = this.mongoTemplate.geoNear(near, clazz, collectionName);
    return geoResults;
}

以上就是使用mongodbTemplate實現地理檢索的功能,關於mongodb地理檢索的方式有很多種,具體可以參考mongodb中文社區,裏面都有具體的介紹。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章