From e4601b6960bc3a8b84b3804408e89f4a15677a4d Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Sat, 15 Jan 2022 16:44:53 +0000 Subject: [PATCH] Adds topk example for RedisBloom (#1837) * Adds topk example. * Update topk.js Co-authored-by: Leibale Eidelman --- examples/README.md | 1 + examples/topk.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/topk.js diff --git a/examples/README.md b/examples/README.md index a9c0869901..5151edfe1a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -14,6 +14,7 @@ This folder contains example scripts showing how to use Node Redis in different | `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality | | `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command | | `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command | +| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. | ## Contributing diff --git a/examples/topk.js b/examples/topk.js new file mode 100644 index 0000000000..1412972434 --- /dev/null +++ b/examples/topk.js @@ -0,0 +1,94 @@ +// This example demonstrates the use of the Top K +// in the RedisBloom module (https://redisbloom.io/) + +import { createClient } from 'redis'; + +async function topK() { + const client = createClient(); + + await client.connect(); + + // Delete any pre-existing Top K. + await client.del('mytopk'); + + // Reserve a Top K to track the 10 most common items. + // https://oss.redis.com/redisbloom/TopK_Commands/#topkreserve + try { + await client.topK.reserve('mytopk', 10); + console.log('Reserved Top K.'); + } catch (e) { + if (e.message.endsWith('key already exists')) { + console.log('Top K already reserved.'); + } else { + console.log('Error, maybe RedisBloom is not installed?:'); + console.log(e); + } + } + + const teamMembers = [ + 'leibale', + 'simon', + 'guy', + 'suze', + 'brian', + 'steve', + 'kyleb', + 'kyleo', + 'josefin', + 'alex', + 'nava', + 'lance', + 'rachel', + 'kaitlyn' + ]; + + // Add random counts for random team members with TOPK.INCRBY + for (let n = 0; n < 1000; n++) { + const teamMember = teamMembers[Math.floor(Math.random() * teamMembers.length)]; + const points = Math.floor(Math.random() * 1000) + 1; + await client.topK.incrBy('mytopk', { + item: teamMember, + incrementBy: points + }); + console.log(`Added ${points} points for ${teamMember}.`); + } + + // List out the top 10 with TOPK.LIST + const top10 = await client.topK.list('mytopk'); + console.log('The top 10:'); + // top10 looks like this: + // [ + // 'guy', 'nava', + // 'kaitlyn', 'brian', + // 'simon', 'suze', + // 'lance', 'alex', + // 'steve', 'kyleo' + // ] + console.log(top10); + + // Check if a few team members are in the top 10 with TOPK.QUERY: + const [ steve, suze, leibale, frederick ] = await client.topK.query('mytopk', [ + 'steve', + 'suze', + 'leibale', + 'frederick' + ]); + + console.log(`steve ${steve === 1 ? 'is': 'is not'} in the top 10.`); + console.log(`suze ${suze === 1 ? 'is': 'is not'} in the top 10.`); + console.log(`leibale ${leibale === 1 ? 'is': 'is not'} in the top 10.`); + console.log(`frederick ${frederick === 1 ? 'is': 'is not'} in the top 10.`); + + // Get count estimate for some team members: + const [ simonCount, lanceCount ] = await client.topK.count('mytopk', [ + 'simon', + 'lance' + ]); + + console.log(`Count estimate for simon: ${simonCount}.`); + console.log(`Count estimate for lance: ${lanceCount}.`); + + await client.quit(); +} + +topK();