从Redis中提取存储集合中的所有唯一分数?

huangapple go评论75阅读模式
英文:

Fetch all unique scores from a stored set with Redis?

问题

添加地理数据使用以下代码:

jedis.geoadd("storegeodata", 51.5074, 0.1278, "London");
// 当成员字符串大多是 JSON 时
jedis.geoadd("storegeodata", 51.5074, 0.1278, "{place: London}");
jedis.geoadd("storegeodata", 51.5074, 0.1278, "{place: London, lat: 51.5074, lon: 0.1278}");

虽然地理哈希作为成员字符串是不同的,但如何检索唯一的基于地理哈希的值以避免冗余:

public Map<String, Object> checkRedisGeo(double lat, double lon, Jedis j) {
    Map<String, Object> result = new HashMap<>();
    try {
        GeoRadiusParam param = GeoRadiusParam.geoRadiusParam();
        param.sortAscending();
        param.withDist();
        param.count(1);
        System.out.println("lat :" + lat + " , lon :" + lon);
        List<GeoRadiusResponse> response = j.georadius("commander", lon, lat, 150, GeoUnit.M, param);
        // System.out.println(response.size() + " size");
        if (response.size() > 0) {
            for (GeoRadiusResponse geoRadiusResponse : response) {
                System.out.println("lat :" + lat + " , lon :" + lon + ",  stringmember :" + geoRadiusResponse.getMemberByString());
                // System.out.println(geoRadiusResponse.getDistance());
                Object[] data = { geoRadiusResponse.getMemberByString() };
                System.out.println(data);
                result.put("result", data);
            }
        } else {
            // sendEvents(streamEvent, null, streamEventChunk);
            System.out.println("E");
        }

    } catch (Exception e) {
        LOGGER.error("checkRedisGeo err : " + e);
    }

    return result;
}

该方法会检索结果,但如何基于地理哈希/分数值进行过滤,以获取所有不同的分数?

以下是示例冗余数据:从Redis中提取存储集合中的所有唯一分数?

英文:

To add geo data following code is used

jedis.geoadd(&quot;storegeodata&quot;, 51.5074, 0.1278, &quot;London&quot;);
//while member string is mostly json 
jedis.geoadd(&quot;storegeodata&quot;, 51.5074, 0.1278, &quot;{place: London}&quot;);
jedis.geoadd(&quot;storegeodata&quot;, 51.5074, 0.1278, &quot;{place: London, lat: 51.5074, lon: 0.1278}&quot;);

While geohash is duplicated as member string is different , how to retrieve unique geohash based value avoiding redundancy

public Map&lt;String, Object&gt; checkRedisGeo(double lat, double lon, Jedis j) {
	Map&lt;String, Object&gt; result = new HashMap&lt;&gt;();
	try
	{
		GeoRadiusParam param = GeoRadiusParam.geoRadiusParam();
		param.sortAscending();
		param.withDist();
		param.count(1);
		System.out.println(&quot;lat :&quot;+lat+&quot; , lon :&quot;+lon);
		List&lt;GeoRadiusResponse&gt; response =  j.georadius(&quot;commander&quot;,
				lon,lat, 150, GeoUnit.M, param);	
		//System.out.println(response.size()+&quot; size&quot;);
	  if(response.size() &gt; 0)
	  {
		  for (GeoRadiusResponse geoRadiusResponse : response) {
				System.out.println(&quot;lat :&quot;+lat+&quot; , lon :&quot;+lon+&quot;,  stringmember :&quot;+geoRadiusResponse.getMemberByString());
				//System.out.println(geoRadiusResponse.getDistance());
				Object[] data= {geoRadiusResponse.getMemberByString()};
				System.out.println(data);
				result.put(&quot;result&quot;, data);
		
			   }
	  }else {
		 // sendEvents(streamEvent, null, streamEventChunk);
		  System.out.println(&quot;E&quot;);
	  }
	  
	} catch (Exception e) {
		LOGGER.error(&quot;checkRedisGeo err : &quot;+e);
	}
	  
  return result;
	
}

Which retrieves result but how to filter out based on geohash / score value ,how to get all distinct score ?

Following is the sample redundant data
从Redis中提取存储集合中的所有唯一分数?

答案1

得分: 1

能够通过比较分数获得重复和唯一分数,因为分数是唯一的,相同分数存储了多个成员,以下是相应的代码:

Double previous_score = 0.0;
int DuplincatesCount = 0;
int UniqueCount = 0;
Set<String> values = jedis.zrange("rediskey", 0, -1); // 获取所有成员
for (String member : values) {
    Double current_score = jedis.zscore("rediskey", member); // 每个成员循环
    if (Double.compare(current_score, previous_score) == 0) { // 将当前分数与前一个分数进行比较
        DuplincatesCount++;
    } else {
        UniqueCount++;
    }
    previous_score = current_score; // 分数映射
}
System.out.println("重复条目 " + DuplincatesCount);
System.out.println("唯一条目 " + UniqueCount);
英文:

Was able to get duplicates and unique scores by comparing the score as the score is unique
and multiple members are stored for same score following was the code for the same

	Double previous_score = 0.0;
	int DuplincatesCount = 0;
	int UniqueCount = 0;
	Set&lt;String&gt; values = jedis.zrange(&quot;rediskey&quot;, 0, -1);// get all the members
	for (String member : values) {
		Double current_score = jedis.zscore(&quot;rediskey&quot;, member);//each member looped
		if (Double.compare(current_score, previous_score) == 0) { //comparing current score with previous
																	// score
			DuplincatesCount++;
		} else {
			UniqueCount++;
		}
		previous_score = current_score;// score mapping
	}
	System.out.println(&quot;Duplincates entry &quot; + DuplincatesCount);
	System.out.println(&quot;Unique entry &quot; + UniqueCount);

huangapple
  • 本文由 发表于 2020年9月30日 21:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定