java查重工具类_Java各种高性能近似成员过滤器(布隆过滤器等查重过滤)

本文介绍了一种在Java中实现的快速近似成员过滤器,包括Bloom过滤器的各种变体以及Cuckoo过滤器等。这些过滤器用于高效地判断元素是否可能存在于集合中,特别适用于内存有限的场景。文中还提供了一个使用示例,演示了如何从大量已知密码创建过滤器,并通过该过滤器查询密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fast Approximate Membership Filters in Java

68747470733a2f2f7472617669732d63692e636f6d2f4661737446696c7465722f6661737466696c7465725f6a6176612e7376673f6272616e63683d6d6173746572

The following filter types are currently implemented:

Bloom filter: the 'standard' algorithm

Blocked Bloom filter: faster than regular Bloom filters, but need a bit more space

Counting Bloom filter: allow removing entries, but need 4 times more space

Succinct counting Bloom filter: about half the space of regular counting Bloom filters; faster lookup but slower add / remove

Succinct counting blocked Bloom filter: same lookup speed as blocked Bloom filter

Cuckoo filter: 8 and 16 bit variants; uses cuckoo hashing to store fingerprints

Cuckoo+ filter: 8 and 16 bit variants, need a bit less space than regular cuckoo filters

Golomb Compressed Set (GCS): needs less space than cuckoo filters, but lookup is slow

Minimal Perfect Hash filter: needs less space than cuckoo filters, but lookup is slow

Xor filter: 8 and 16 bit variants; needs less space than cuckoo filters, with faster lookup

Xor+ filter: 8 and 16 bit variants; compressed xor filter

Reference: Xor Filters: Faster and Smaller Than Bloom and Cuckoo Filters, Journal of Experimental Algorithmics (to appear).

Other Xor Filter Implementations

Password Lookup Tool

Included is a tool to build a filter from a list of known password (hashes), and a tool to do lookups. That way, the password list can be queried locally, without requiring a large file. The filter is only 650 MB, instead of the original file which is 11 GB. At the cost of some false positives (unknown passwords reported as known, with about 1% probability).

Generate the Password Filter File

Download the latest SHA-1 password file that is ordered by hash, for example the file pwned-passwords-sha1-ordered-by-hash-v4.7z (~10 GB) from https://haveibeenpwned.com/passwords with about 550 million passwords.

If you have enough disk space, you can extract the hash file (~25 GB), and convert it as follows:

mvn clean install

cat hash.txt | java -cp target/fastfilter*.jar org.fastfilter.tools.BuildFilterFile filter.bin

Converting takes about 2-3 minutes (depending on hardware). To save disk space, you can extract the file on the fly (Mac OS X using Keka):

/Applications/Keka.app/Contents/Resources/keka7z e -so

pass.7z | java -cp target/fastfilter*.jar org.fastfilter.tools.BuildFilterFile filter.bin

Both will generate a file named filter.bin (~630 MB).

Check Passwords

java -cp target/fastfilter*.jar org.fastfilter.tools.PasswordLookup filter.bin

Enter a password to see if it's in the list. If yes, it will (for sure) either show "Found", or "Found; common", which means it was seen 10 times or more often. Passwords not in the list will show "Not found" with more than 99% probability, and with less than 1% probability "Found" or "Found; common".

Internally, the tool uses a xor+ filter (see above) with 8 bits per fingerprint. Actually, 1024 smaller filters (segments) are made, the segment id being the highest 10 bits of the key. The lowest bit of the key is set to either 0 (regular) or 1 (common), and so two lookups are made per password. Because of that, the false positive rate is twice of what it would be with just one lookup (0.0078 instead of 0.0039). A regular Bloom filter with the same guarantees would be ~760 MB. For each lookup, one filter segment (so, less than 1 MB) are read from the file.

<think>嗯,用户之前问了关于Redis锁的数据一致性问题中如何设置延长时间,现在他们想进一步了解Redisson的其他功能,比如分布式锁、布隆过滤器、队列、集合和并发工具类。我需要详细介绍一下这些功能,但首先要理清楚每个部分的结构,确保回答清晰有条理。 首先,用户可能已经对Redisson的分布式锁有一定了解,所以需要先简要回顾,再扩展其他功能。用户提到的分布式布隆、队列、集合和并发工具类,这些都是Redisson提供的分布式数据结构,每个部分需要分开讲解,说明用途、实现原理和使用场景。 我需要考虑用户可能的背景,可能是开发人员或者系统架构师,他们可能在设计分布式系统时需要用到这些工具。用户的需求可能是寻找解决分布式环境中常见问题的方案,比如数据一致性、去重、任务分发、并发控制等。 在介绍每个功能时,要结合具体的使用场景,比如分布式锁用于资源同步,布隆过滤器用于高效去重,队列用于任务分发,集合用于跨服务的数据共享,并发工具类如CountDownLatch用于协调多节点任务。同时,需要给出简单的代码示例,帮助用户理解如何使用这些功能,但要注意示例的简洁性和语言的通用性,可能使用Java或伪代码。 还要注意强调Redisson的优势,比如基于Redis的高性能,与Spring等框架的集成能力,以及丰富的文档支持。此外,可能需要比较Redisson与其他实现方式的优缺点,比如手动实现分布式锁的复杂性,而Redisson提供了封装好的解决方案,减少开发负担。 最后,总结部分需要将这些功能串联起来,说明它们如何共同支持分布式系统的构建,帮助用户形成整体认识。确保回答结构清晰,每个部分有小标题,关键点用粗体或列表突出,便于阅读和理解。</think>以下是 Redisson 的**核心扩展功能**及其在分布式场景下的应用详解: --- ### **1. 分布式锁(RLock)** - **功能**:提供可重入锁、公平锁、联锁(MultiLock)等,支持自动续期(看门狗机制)。 - **核心特性**: - **自动续期**:默认每隔 10 秒检查锁状态并延长 TTL(避免业务未完成锁过期)。 - **可重入性**:同一线程可多次获取锁,避免死锁。 - **联锁**:同时锁定多个资源(原子性保障)。 - **使用场景**:分布式资源竞争控制(如库存扣减、秒杀系统)。 - **示例**: ```java RLock lock = redisson.getLock("orderLock"); try { lock.lock(); // 加锁(默认 30 秒 TTL,自动续期) // 执行业务逻辑... } finally { lock.unlock(); // 释放锁 } ``` -- ### **2. 分布式布隆过滤器(RBloomFilter)** - **功能**:基于 Redis 的布隆过滤器,用于高效判断元素是否存在(空间效率高,允许误判)。 - **核心特性**: - **预初始化**:需预先设置预期元素数量和误判率。 - **分布式共享**:多个服务共享同一过滤器状态。 - **使用场景**:防止缓存穿透(如校验非法请求 ID)、去重(如大规模数据清洗)。 - **示例**: ```java RBloomFilter<String> bloomFilter = redisson.getBloomFilter("userFilter"); bloomFilter.tryInit(1000000L, 0.03); // 预期 100 万数据,3% 误判率 bloomFilter.add("user123"); // 添加元素 boolean exists = bloomFilter.contains("user456"); // 判断是否存在 ``` --- ### **3. 分布式队列(RQueue、RDelayedQueue)** #### **(1) 普通队列(RQueue)** - **功能**:基于 Redis 的 FIFO 队列,支持多节点生产消费。 - **使用场景**:异步任务分发、削峰填谷。 - **示例**: ```java RQueue<String> queue = redisson.getQueue("taskQueue"); queue.add("task1"); // 生产者推送任务 String task = queue.poll(); // 消费者拉取任务 ``` #### **(2) 延迟队列(RDelayedQueue)** - **功能**:支持延迟投递消息(消息暂存,到期后进入目标队列)。 - **使用场景**:订单超时关闭、定时提醒。 - **示例**: ```java RQueue<String> destQueue = redisson.getQueue("delayedDestQueue"); RDelayedQueue<String> delayedQueue = redisson.getDelayedQueue(destQueue); delayedQueue.offer("order123", 30, TimeUnit.MINUTES); // 30 分钟后进入 destQueue ``` --- ### **4. 分布式集合(RSet、RScoredSortedSet)** #### **(1) 无序集合(RSet)** - **功能**:跨节点的 Set 结构,支持交并差运算。 - **使用场景**:标签管理、共同好友计算。 - **示例**: ```java RSet<String> tags = redisson.getSet("productTags"); tags.add("electronics"); Set<String> intersection = tags.intersection("anotherSet"); // 求交集 ``` #### **(2) 有序集合(RScoredSortedSet)** - **功能**:按分值排序的集合,支持范围查询。 - **使用场景**:排行榜、优先级任务调度。 - **示例**: ```java RScoredSortedSet<String> leaderboard = redisson.getScoredSortedSet("gameRank"); leaderboard.add(1000, "player1"); // 添加玩家及分数 Collection<String> top10 = leaderboard.valueRangeReversed(0, 9); // 获取 Top 10 ``` --- ### **5. 分布式并发工具类** #### **(1) 信号量(RSemaphore)** - **功能**:限制同时访问资源的线程数。 - **使用场景**:限流(如 API 并发控制)。 - **示例**: ```java RSemaphore semaphore = redisson.getSemaphore("apiLimit"); semaphore.trySetPermits(100); // 设置最大并发数 100 semaphore.acquire(); // 获取信号量(阻塞直到成功) // 执行业务... semaphore.release(); // 释放信号量 ``` #### **(2) 闭锁(RCountDownLatch)** - **功能**:等待多个分布式节点完成操作。 - **使用场景**:多服务协同启动、批量任务同步。 - **示例**: ```java RCountDownLatch latch = redisson.getCountDownLatch("initLatch"); latch.trySetCount(3); // 等待 3 个节点 // 节点完成任务后调用 countDown() latch.await(); // 阻塞直到计数归零 ``` #### **(3) 原子长整型(RAtomicLong)** - **功能**:分布式环境下的原子计数器。 - **使用场景**:全局唯一 ID 生成、统计计数。 - **示例**: ```java RAtomicLong counter = redisson.getAtomicLong("requestCount"); long id = counter.incrementAndGet(); // 原子自增 ``` --- ### **6. 高级功能** - **分布式映射(RMap)**:支持本地缓存、LRU 驱逐策略的分布式 Map。 - **分布式 Topic(RTopic)**:发布-订阅模式,支持跨服务消息广播。 - **分布式 HyperLogLog(RHyperLogLog)**:近似统计独立访客数(UV)。 --- ### **总结:Redisson 的核心价值** - **简化分布式开发**:将 Redis 的数据结构抽象为 Java 对象,屏蔽底层复杂性。 - **高可用保障**:支持 Redis 集群、哨兵模式,自动处理故障转移。 - **性能优化**:内置连接池、Lua 脚本原子化操作,减少网络开销。 - **无缝集成**:与 Spring、Spring Boot 等框架深度整合。 通过 Redisson,开发者可以像操作本地数据结构一样使用分布式组件,显著降低分布式系统开发门槛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值