如何使用java(jedis)从单个redis密钥获取所有地理记录

jrcvhitl  于 2021-06-10  发布在  Redis
关注(0)|答案(2)|浏览(432)

如何获取与密钥相关联的所有地理记录?我能够将数据推送到redis中,并基于[lon,lat,buffer]使用

jedis.georadius(key, lon,lat, radius, GeoUnit.KM,GeoRadiusParam.geoRadiusParam().withCoord());

有没有办法单独使用密钥获取所有记录?下面是我使用geobuffer从redis中提取记录的代码。

public Map<String, Object> getFromRedis(String key, Double lat, Double lon, Integer radii, Integer resultCount,  boolean hasType, String typekey) {
    Map<String, Object> result = new HashMap<>();
    ArrayList<Map<String,Object>> geoObj= new ArrayList<>();

    boolean gotresult = false;
    try(Jedis jedis=new Jedis("localhost",6379);)
    {

        GeoRadiusParam param = GeoRadiusParam.geoRadiusParam();
        param.sortAscending();
        param.withDist();
        param.count(resultCount);
        List<GeoRadiusResponse> response;
        response =  jedis.georadius(key,lat,lon, radii, GeoUnit.M, param);//redis zset key, lon,lat,radii,..
          if(response.size() > 0)
          {
              for (GeoRadiusResponse geoRadiusResponse : response) {
                    Map<String, Object> resultObj = new HashMap<>();
                    Object[] data= {geoRadiusResponse.getMemberByString()};
                    LOGGER.info("Got Result from Redis Server :: "+data);
                    gotresult = true;
                        for(Object o : data)
                        {
                            JSONObject jObject = new JSONObject(o.toString());
                            Iterator<?> keys = jObject.keys();
                            while( keys.hasNext() ){
                                String keyObj = (String)keys.next();
                                String value = jObject.getString(keyObj); 
                                resultObj.put(keyObj, value);
                            }
                            LOGGER.info("Fetched Value : "+resultObj);
                            geoObj.add(resultObj);
                        }
                   }
                result.put("result", geoObj);
          }
          else {
                LOGGER.info("Unable to find matching result in Redis server");
          }
    } catch (Exception e) {
        LOGGER.error("Redis Fetch result failed");
        LOGGER.error("Error : "+e);
    }
    result.put("status", gotresult);
    return result;
}

当我推的时候

jedis.geoadd(key, lon, lat, String);

正在尝试将所有记录单独从键(apicache)重设为重设

cetgtptt

cetgtptt1#

地理数据以排序集的形式存储到密钥中。所以你可以用 jedis.zrange 用于获取所有点,但重新运行的值是geo格式的。
要获得所有坐标记录,只需给出整个地球的值,如下所示 jedis.georadius .,

lat = 0
lon = 0  
radii = 22000  
param = withCoord

它将返回所有带坐标的记录。但是,您不能使用此命令来订购位置。

xwbd5t1u

xwbd5t1u2#

能够按键获取所有值(存储值为json字符串,Maplat long,因此每次从zrange返回时,不带几何体的返回值也能够获取lat lon)

try(Jedis jedis=new Jedis("localhost",6379);) {
Set<String> values =  jedis.zrange(Key, 0, -1);//key, start , end        
 for (String  recordvalue : values) {
                System.out.println(recordvalue);//HashMap<String, Object> map = new Gson().fromJson(recordvalue .toString(), HashMap.class);
            }
 }
} catch (Exception e) {
        LOGGER.error(e);
}

但正如praga\u t所说,这只会得到值,其思想是Maplat lon并存储为json,这样我们也可以通过键获得latlong

相关问题