当前位置: 移动技术网 > IT编程>数据库>MongoDB > 看看redis中那些好玩的module (sql on redis, bf/cf on redis)

看看redis中那些好玩的module (sql on redis, bf/cf on redis)

2019年01月20日  | 移动技术网IT编程  | 我要评论

  

  自从redis加入了module功能之后,redis的生态就很有意思了,每个领域的大佬都会以插件的形式给redis扩展一些新的功能,比如本篇说到的redisql,rebloom。

 

一:redisql

  1. 背景

        redis虽然是牛逼,但还是有很多人吐槽redis操作性太弱,比如你想要在redis上实现一个比较复杂的业务逻辑,可能对你来说是一个灾难,有些同学会说用redis的

存储过程lua撒,但是lua不是每个程序员都会的,更何况那些数据分析师,但要是问sql会不会,基本上合格的程序员和分析师在这个上面都是没毛病的,真的要是让sql

落在redis上,那真是如虎添翼,可能最早让sql落到redis上的,应该是spark sql 吧,让redis作为spark的rdd,但这里说到的是另外一个通过module实现的sql on redis。

 

  2. 下载

     源代码可以到 github:https://github.com/redbeardlab/redisql  ,下载地址是:https://github.com/redbeardlab/redisql/releases    

直接下载这个编译好的文件,拿来就用就好了。

 

3. 加载

    这个简单,先把redisql_0.7.1.so 导入到centos中,然后只需使用module load  redisql_0.7.1.so 返回ok即可。

1 [root@localhost redis]# ls
2 00-releasenotes  copying   makefile   readme.md        redis.conf        runtest           src
3 appendonly.aof   deps      manifesto  redis-check-aof  redisql_0.7.1.so  runtest-cluster   tests
4 bugs             dump.rdb  module     redis-check-rdb  redis-server      runtest-sentinel  utils
5 contributing     install   mydata     redis-cli        redis-trib.rb     sentinel.conf

 

[root@localhost redis]# ./redis-cli
127.0.0.1:6379> module load /data/redis/redisql_0.7.1.so
ok

 

4. 简单使用

    既然要让sql落到redis中,那就先得建库建表啦,这里database:datamip, table:customer,然后做了一个简单的查询,如下:

127.0.0.1:6379> redisql.create_db datamip
ok
127.0.0.1:6379> redisql.exec datamip "create table customer(id int, username varchar(10));"
1) done
2) (integer) 0
127.0.0.1:6379> redisql.exec datamip "insert into customer values(1, 'jack');"
1) done
2) (integer) 1
127.0.0.1:6379> redisql.exec datamip "insert into customer values(2, 'mary');"
1) done
2) (integer) 1
127.0.0.1:6379> redisql.exec datamip "select * from customer where id=2"
1) 1) (integer) 2
   2) "mary"
127.0.0.1:6379> 

 

   是不是很爽的感觉,不过作者也是要吃饭的,所以企业版还是要收点压箱底的钱。

 

二: rebloom

1. 背景

  这个module也很有意思,它给redis新增了两种过滤器,一个叫做bloom filter,一个叫做 cuckoo filter, bloomfilter 估计大家都知道,用极小的错误率换取

原有的hashset的1/8 -1/4的空间利用率,具体场景大家看着用吧,cuckoofilter 翻译过来就是布谷鸟过滤性,可能作者家就是养鸟的,不然怎么那么多鸟呢,

大家只要理解cuckoofilter比bloomfilter更省空间,更低的错误率,而且还是支持删除。

具体的大家可以看论文: 。

 

2. 下载

    github地址:https://github.com/redislabsmodules/rebloom   然后找到release模式,下载完之后需要自己make一下。

[root@localhost module]# ls
v1.1.0.tar.gz
[root@localhost module]# tar -xzvf v1.1.0.tar.gz
rebloom-1.1.0/
rebloom-1.1.0/.circleci/
rebloom-1.1.0/.circleci/config.yml
rebloom-1.1.0/.clang-format
rebloom-1.1.0/.gitignore
rebloom-1.1.0/dockerfile
rebloom-1.1.0/license
rebloom-1.1.0/makefile
rebloom-1.1.0/readme.md
rebloom-1.1.0/contrib/
rebloom-1.1.0/contrib/murmurhash2.c
rebloom-1.1.0/contrib/bloom.c
rebloom-1.1.0/contrib/bloom.h
rebloom-1.1.0/contrib/murmurhash2.h
rebloom-1.1.0/docs/
rebloom-1.1.0/docs/bloom_commands.md
rebloom-1.1.0/docs/cname
rebloom-1.1.0/docs/cuckoo_commands.md
rebloom-1.1.0/docs/java_client.md
rebloom-1.1.0/docs/quick_start.md
rebloom-1.1.0/docs/_config.yml
rebloom-1.1.0/docs/index.md
rebloom-1.1.0/mkdocs.yml
rebloom-1.1.0/ramp.yml
rebloom-1.1.0/src/
rebloom-1.1.0/src/cf.c
rebloom-1.1.0/src/cf.h
rebloom-1.1.0/src/cuckoo.c
rebloom-1.1.0/src/cuckoo.h
rebloom-1.1.0/src/print_version.c
rebloom-1.1.0/src/rebloom.c
rebloom-1.1.0/src/redismodule.h
rebloom-1.1.0/src/sb.c
rebloom-1.1.0/src/sb.h
rebloom-1.1.0/src/version.h
rebloom-1.1.0/tests/
rebloom-1.1.0/tests/makefile
rebloom-1.1.0/tests/cuckoo.py
rebloom-1.1.0/tests/pytests.py
rebloom-1.1.0/tests/test-basic.c
rebloom-1.1.0/tests/test-cuckoo.c
rebloom-1.1.0/tests/test-perf.c
rebloom-1.1.0/tests/test.h
[root@localhost module]# ls
rebloom-1.1.0  v1.1.0.tar.gz
[root@localhost module]# cd rebloom-1.1.0
[root@localhost rebloom-1.1.0]# ls
contrib  dockerfile  docs  license  makefile  mkdocs.yml  ramp.yml  readme.md  src  tests
[root@localhost rebloom-1.1.0]# make
cc  -wall -wno-unused-function -g -ggdb -o2 -fpic -std=gnu99 -d_gnu_source -i/data/redis/module/rebloom-1.1.0 -i/data/redis/module/rebloom-1.1.0/contrib  -c -o /data/redis/module/rebloom-1.1.0/src/rebloom.o /data/redis/module/rebloom-1.1.0/src/rebloom.c
cc  -wall -wno-unused-function -g -ggdb -o2 -fpic -std=gnu99 -d_gnu_source -i/data/redis/module/rebloom-1.1.0 -i/data/redis/module/rebloom-1.1.0/contrib  -c -o /data/redis/module/rebloom-1.1.0/contrib/murmurhash2.o /data/redis/module/rebloom-1.1.0/contrib/murmurhash2.c
cc  -wall -wno-unused-function -g -ggdb -o2 -fpic -std=gnu99 -d_gnu_source -i/data/redis/module/rebloom-1.1.0 -i/data/redis/module/rebloom-1.1.0/contrib  -c -o /data/redis/module/rebloom-1.1.0/src/sb.o /data/redis/module/rebloom-1.1.0/src/sb.c
cc  -wall -wno-unused-function -g -ggdb -o2 -fpic -std=gnu99 -d_gnu_source -i/data/redis/module/rebloom-1.1.0 -i/data/redis/module/rebloom-1.1.0/contrib  -c -o /data/redis/module/rebloom-1.1.0/src/cf.o /data/redis/module/rebloom-1.1.0/src/cf.c
in file included from /data/redis/module/rebloom-1.1.0/src/cf.c:6:0:
/data/redis/module/rebloom-1.1.0/src/cuckoo.c: in function ‘cuckoofilter_count’:
/data/redis/module/rebloom-1.1.0/src/cuckoo.c:157:9: warning: passing argument 1 of ‘filtercount’ from incompatible pointer type [enabled by default]
         ret += filtercount(filter->filters[ii], &params);
         ^
/data/redis/module/rebloom-1.1.0/src/cuckoo.c:139:15: note: expected ‘const uint8_t (*)[2]’ but argument is of type ‘uint8_t (*)[2]’
 static size_t filtercount(const cuckoobucket *filter, const lookupparams *params) {
               ^
ld /data/redis/module/rebloom-1.1.0/src/rebloom.o /data/redis/module/rebloom-1.1.0/contrib/murmurhash2.o /data/redis/module/rebloom-1.1.0/src/sb.o /data/redis/module/rebloom-1.1.0/src/cf.o -o /data/redis/module/rebloom-1.1.0/rebloom.so -shared -bsymbolic -bsymbolic-functions -lm -lc
[root@localhost rebloom-1.1.0]# ls
contrib  dockerfile  docs  license  makefile  mkdocs.yml  ramp.yml  readme.md  rebloom.so  src  tests

 

最后标红的 rebloom.so 就是你最终要找的加载文件。

[root@localhost redis]# ./redis-cli
127.0.0.1:6379> module load /data/redis/module/rebloom-1.1.0/rebloom.so
ok

 

3.  简单使用

 

《1》 bloomfilter 的简单使用,比如塞入1,2,3,4 。 判断3,5是否在其中,如下:

127.0.0.1:6379> bf.add myfilter 1
(integer) 1
127.0.0.1:6379> bf.add myfilter 2
(integer) 1
127.0.0.1:6379> bf.add myfilter 3
(integer) 1
127.0.0.1:6379> bf.add myfilter 4
(integer) 1
127.0.0.1:6379> bf.exists myfilter 3
(integer) 1
127.0.0.1:6379> bf.exists myfilter 5
(integer) 0
127.0.0.1:6379>

 

《2》 在github的quickstart中并没有找到cuckoofilter的使用方式,没关系撒,找找源文件就好啦。

   

比如下面的源码就是告诉你怎么去使用。

 

接下来就可以简单的add,delete,exists 啦。

127.0.0.1:6379> cf.add myfilter2 1
(integer) 1
127.0.0.1:6379> cf.add myfilter2 2
(integer) 1
127.0.0.1:6379> cf.add myfilter2 3
(integer) 1
127.0.0.1:6379> cf.add myfilter2 4
(integer) 1
127.0.0.1:6379> cf.del myfilter 2
(error) not found
127.0.0.1:6379> cf.del myfilter2 2
(integer) 1
127.0.0.1:6379> cf.exists myfilter2 1
(integer) 1

 

   好了,这就是本篇给大家介绍的module,还是蛮有意思的。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网