1
0
mirror of https://github.com/redis/node-redis.git synced 2025-07-31 05:44:24 +03:00

Adds a basic count-min sketch example for RedisBloom. (#1842)

This commit is contained in:
Simon Prickett
2022-01-19 18:30:41 +00:00
committed by GitHub
parent 1f8993a14d
commit 86c239e7e9
2 changed files with 84 additions and 0 deletions

View File

@ -8,6 +8,7 @@ This folder contains example scripts showing how to use Node Redis in different
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch |
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |

View File

@ -0,0 +1,83 @@
// This example demonstrates the use of the Count-Min Sketch
// in the RedisBloom module (https://redisbloom.io/)
import { createClient } from 'redis';
async function countMinSketch() {
const client = createClient();
await client.connect();
// Delete any pre-existing Count-Min Sketch.
await client.del('mycms');
// Initialize a Count-Min Sketch with error rate and probability:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsinitbyprob
try {
await client.cms.initByProb('mycms', 0.001, 0.01);
console.log('Reserved Top K.');
} catch (e) {
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'
];
// Store actual counts for comparison with CMS.
let actualCounts = {};
// Randomly emit a team member and count them with the CMS.
for (let n = 0; n < 1000; n++) {
const teamMember = teamMembers[Math.floor(Math.random() * teamMembers.length)];
await client.cms.incrBy('mycms', {
item: teamMember,
incrementBy: 1
});
actualCounts[teamMember] = actualCounts[teamMember] ? actualCounts[teamMember] + 1 : 1;
console.log(`Incremented score for ${teamMember}.`);
}
// Get count estimate for some team members:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsquery
const [ alexCount, rachelCount ] = await client.cms.query('mycms', [
'simon',
'lance'
]);
console.log(`Count estimate for alex: ${alexCount} (actual ${actualCounts.alex}).`);
console.log(`Count estimate for rachel: ${rachelCount} (actual ${actualCounts.rachel}).`);
// Get overall information about the Count-Min Sketch:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsinfo
const info = await client.cms.info('mycms');
console.log('Count-Min Sketch info:');
// info looks like this:
// {
// width: 2000,
// depth: 7,
// count: 1000
// }
console.log(info);
await client.quit();
}
countMinSketch();