使用Jedis连接Redis并操作HyperLogLog
import redis.clients.jedis.Jedis;
public class RedisHyperLogLogExample {
public static void main(String[] args) {
// 连接到本地的Redis服务
Jedis jedis = new Jedis("localhost", 6379);
String hyperLogLogKey = "hll_example";
// 添加元素到HyperLogLog
jedis.pfadd(hyperLogLogKey, "element1");
jedis.pfadd(hyperLogLogKey, "element2");
jedis.pfadd(hyperLogLogKey, "element3");
jedis.pfadd(hyperLogLogKey, "element1"); // 重复的元素
// 获取HyperLogLog的基数估计值
long estimatedCardinality = jedis.pfcount(hyperLogLogKey);
System.out.println("Estimated cardinality: " + estimatedCardinality);
// 清理数据
jedis.del(hyperLogLogKey);
// 关闭Jedis连接
jedis.close();
}
}