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

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

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

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

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

  1. public Map<String, Object> getFromRedis(String key, Double lat, Double lon, Integer radii, Integer resultCount, boolean hasType, String typekey) {
  2. Map<String, Object> result = new HashMap<>();
  3. ArrayList<Map<String,Object>> geoObj= new ArrayList<>();
  4. boolean gotresult = false;
  5. try(Jedis jedis=new Jedis("localhost",6379);)
  6. {
  7. GeoRadiusParam param = GeoRadiusParam.geoRadiusParam();
  8. param.sortAscending();
  9. param.withDist();
  10. param.count(resultCount);
  11. List<GeoRadiusResponse> response;
  12. response = jedis.georadius(key,lat,lon, radii, GeoUnit.M, param);//redis zset key, lon,lat,radii,..
  13. if(response.size() > 0)
  14. {
  15. for (GeoRadiusResponse geoRadiusResponse : response) {
  16. Map<String, Object> resultObj = new HashMap<>();
  17. Object[] data= {geoRadiusResponse.getMemberByString()};
  18. LOGGER.info("Got Result from Redis Server :: "+data);
  19. gotresult = true;
  20. for(Object o : data)
  21. {
  22. JSONObject jObject = new JSONObject(o.toString());
  23. Iterator<?> keys = jObject.keys();
  24. while( keys.hasNext() ){
  25. String keyObj = (String)keys.next();
  26. String value = jObject.getString(keyObj);
  27. resultObj.put(keyObj, value);
  28. }
  29. LOGGER.info("Fetched Value : "+resultObj);
  30. geoObj.add(resultObj);
  31. }
  32. }
  33. result.put("result", geoObj);
  34. }
  35. else {
  36. LOGGER.info("Unable to find matching result in Redis server");
  37. }
  38. } catch (Exception e) {
  39. LOGGER.error("Redis Fetch result failed");
  40. LOGGER.error("Error : "+e);
  41. }
  42. result.put("status", gotresult);
  43. return result;
  44. }

当我推的时候

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

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

cetgtptt

cetgtptt1#

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

  1. lat = 0
  2. lon = 0
  3. radii = 22000
  4. param = withCoord

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

xwbd5t1u

xwbd5t1u2#

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

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

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

相关问题