Skip to content

Instantly share code, notes, and snippets.

@bsbodden
Last active October 7, 2021 17:37
Show Gist options
  • Save bsbodden/8afeb138391221f2accf8fad61cc5f60 to your computer and use it in GitHub Desktop.
Save bsbodden/8afeb138391221f2accf8fad61cc5f60 to your computer and use it in GitHub Desktop.
Redis Bloom CLI Demo

Keys / Key Spaces / Expire / Scan / Get/Set

set actor:001 "Cobie Smulders"
exist actor:001
get actor:001
get "actor:001"
get 'actor:001'
type actor:001

set actor:002 "Karen Gillan"
set movie:001 "Guardians of the Galaxy"
get actor:002
get movie:001

set actor:003 "Benedict \"Eggs\" Bumbbleflash"
get actor:003
set actor:003 "Benedict Cumberbatch" nx
get actor:003
set actor:003 "Benedict Cumberbatch" xx
get actor:003

KEYS actor:*
del actor:001
exist actor:001

Sets

SADD actors:oscar-winners "Cate Blanchett" "Ben Kingsley" "Mahershala Ali"
SADD movie:thor-rag:cast "Tessa Thompson" "Taika Waititi" "Cate Blanchett" "Idris Elba"
# REtrieve all OSCAr winners
SMEMBERS actors:oscar-winners
# OSCAR WINNERS IN THOR RAGNAROK
SINTER actors:oscar-winners movie:thor-rag:cast
# OSCAR WINNERS AND THE CAST OF THOR RAGNAROK
SUNION actors:oscar-winners movie:thor-rag:cast
# OSCAR WINNER NOT IN THE CAST OF THOR RAGNAROK
SDIFF actors:oscar-winners movie:thor-rag:cast
# IS "Ben Kingsley" in Thor Ragnarok
SISMEMBER movie:thor-rag:cast "Ben Kingsley"
# IS "Ben Kingsley" an OScan winner
SISMEMBER actors:oscar-winners "Ben Kingsley"
# Give me a random oscar winner
SRANDMEMBER actors:oscar-winners

Hashes

HMSET actors:001 name "Anthony Mackie" yob 1978 pob "New Orleans, LA, US"
HMSET actors:002 name "Danny Ramirez" yob 1992 pob "Chicago, IL, US"
HMSET actors:003 name "Georges St-Pierre" yob 1981 pob "Saint-Isidore, Quebec, CA"
HMGET actors:001 name
HGETALL actors:002
HEXISTS actors:001 character
HMSET actors:001 character "Falcon"
HMGETALL actors:001
HDEL actors:001 character
HSETNX actors:001 character "Captain America"
HMGETALL actors:001
HKEYS actors:001
HVALS actors:001
HSTRLEN actors:003 name

Membership the traditional way with a Set:

SADD unique_visitors_set 10.94.214.120
SISMEMBER unique_visitors_set 10.94.214.120

Bloom

BF.ADD unique_visitors 10.94.214.120
BF.EXISTS unique_visitors 10.94.214.120
BF.EXISTS unique_visitors 10.94.214.121
BF.MADD unique_visitors 10.94.214.100 10.94.214.200 10.94.214.210 10.94.214.212
BF.MEXISTS unique_visitors 10.94.214.200 10.94.214.212
BF.MEXISTS unique_visitors 10.94.214.200 10.94.214.213

Cuckoo

  • Cuckoo: Adding new items to a filter
  • Create an empty cuckoo filter with an initial capacity (of 1000 items)
CF.RESERVE userIds 1000

A new filter is created for you if it does not yet exist

CF.ADD userIds foo

Cuckoo filters can contain the same item multiple times, and consider each insert as separate.

CF.ADD userIds foo

Insert ONLY if it doesn't exist

CF.ADDNX userIds noobmaster69
CF.ADDNX userIds noobmaster69

Cuckoo: Checking whether item exists

CF.EXISTS userIds foo

CF.EXISTS userIds bar

Cuckoo: Deleting item from filter

CF.DEL userIds foo

TOPK

TOPK.RESERVE movies 5

TOPK.ADD movies "the departed"

TOPK.LIST movies

TOPK.ADD movies "The Seven Samurai"
TOPK.ADD movies "Bonnie and Clyde"
TOPK.ADD movies "Reservoir Dogs"
TOPK.ADD movies "Airplane!"
TOPK.ADD movies "Doctor Zhivago"
TOPK.ADD movies "The Deer Hunter"
TOPK.ADD movies "Up"
TOPK.ADD movies "Rocky"
TOPK.ADD movies "Memento"
TOPK.ADD movies "Memento"
TOPK.ADD movies "Inception"
TOPK.ADD movies "The Matrix"

TOPK.INCRBY movies "Airplane!" 2
TOPK.INCRBY movies "Inception" 1
TOPK.INCRBY movies "Rocky" 6
TOPK.INCRBY movies "The Seven Samurai" 3

TOPK.QUERY movies "Seven"
TOPK.QUERY movies "Rocky"

TOPK.COUNT "Inception"
TOPK.COUNT movies "Inception"
TOPK.COUNT movies "Seven"
TOPK.COUNT movies "Rocky"

TOPK.LIST movies

Count Min Sketch

CMS.INITBYPROB hashtags 0.001 0.01

CMS.INCRBY hashtags #foo 1 #bar 2 #baz 3 #quux 1
CMS.INCRBY hashtags #qux 2 #quux 4 #quuz 9 #foo 2
CMS.INCRBY hashtags #corge 1 #grault 1 #garply 1 #waldo 4 #fred 5 #quux

CMS.QUERY hashtags #foo #bar #quux

CMS.INFO hashtags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment