Redis支持json数据类型(rejson)
博客专区 > eui988 的博客 > 博客详情
Redis支持json数据类型(rejson)
eui988 发表于11个月前
Redis支持json数据类型(rejson)
  • 发表于 11个月前
  • 阅读 82
  • 收藏 0
  • 点赞 1
  • 评论 0

移动开发云端新模式探索实践 >>>   

Redis支持json数据类型(EN)

Redis支持json数据类型(CH)

ReJSON介绍
ReJSON 是一个Redis Module,它实现了ECMA-404 The JSON Data Interchange Standard作为本地数据类型,它允许从Redis Keys(documents)中存储,更新和获取JSON值

主要特性:
完全支持JSON标准
对于在文档内选择元素类似JSONPath语法
文档作为二进制数据被存储在一个树形结构中,允许快速访问子元素
对所有JSON数据类型按照原子操作进行分类

ReJSON是有Redis Labs开发的,源码下载地址是https://github.com/RedisLabsModules/ReJSON
# centos
yum install gcc gcc-c++ automake autoconf libtool

# ubuntu
sudo apt-get install make libtool gcc autoconf 
#
mkdir -p /usr/local/redis/{bin,etc,db,log,modules}
#
git clone https://github.com/RedisLabsModules/ReJSON
cd  ReJSON
make
cp src/rejson.so /usr/local/redis/modules/
#
wget --no-check-certificate https://github.com/antirez/redis/archive/4.0-rc3.tar.gz
tar -zxvf 4.0-rc3.tar.gz
cd redis-4.0-rc3
make
cd src && \cp -a redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server  /usr/local/redis/bin/
cd ..  && cp redis.conf  /usr/local/redis/etc/6379.conf
sed -i "s#dir ./#dir /usr/local/redis/db/#" /usr/local/redis/etc/6379.conf
sed -i "s/^# bind 127.0.0.1/bind 127.0.0.1/" /usr/local/redis/etc/6379.conf
sed -i "s/daemonize no/daemonize yes/" /usr/local/redis/etc/6379.conf
sed -i 's#logfile ""#logfile "/usr/local/redis/log/6379.log"#' /usr/local/redis/etc/6379.conf
echo -e 'export REDIS_HOME=/usr/local/redis\nexport PATH=$REDIS_HOME/bin:$PATH' >>/etc/profile && source /etc/profile
#加载rejson.so到redis,依赖:Redis V4.0 或更高版本
#通过配置文件加载module(推荐):
sed -i '44a\loadmodule /usr/local/redis/modules/rejson.so' /usr/local/redis/etc/6379.conf
#通过命令行加载module(不推荐):
/usr/local/redis/bin/redis-server --loadmodule /usr/local/redis/modules/rejson.so
#注意,"--loadmodule"比较危险,将来出于安全考虑可能会被禁用或取消


#启动redis进程
/usr/local/redis/bin/redis-server /usr/local/redis/etc/6379.conf
#/usr/local/redis/bin/redis-cli
127.0.0.1:6379> set hao 123
OK
127.0.0.1:6379> get hao
"123"
#/usr/local/redis/bin/redis-cli
127.0.0.1:6379> JSON.SET foo . '"bar"'
OK
127.0.0.1:6379> JSON.GET foo
"\"bar\""
127.0.0.1:6379> JSON.TYPE foo .
string
127.0.0.1:6379> JSON.SET num . 0
OK
127.0.0.1:6379> JSON.NUMINCRBY num . 1
"1"
127.0.0.1:6379> JSON.NUMINCRBY num . 1.5
"2.5"
127.0.0.1:6379> JSON.NUMINCRBY num . -0.75
"1.75"
127.0.0.1:6379> JSON.NUMMULTBY num . 24
"42"
127.0.0.1:6379> JSON.SET amoreinterestingexample . '[ true, { "answer": 42 }, null ]'
OK
127.0.0.1:6379> JSON.GET amoreinterestingexample
"[true,{\"answer\":42},null]"
127.0.0.1:6379> JSON.GET amoreinterestingexample [1].answer
"42"
127.0.0.1:6379>  JSON.DEL amoreinterestingexample [-1]
(integer) 1
127.0.0.1:6379> JSON.GET amoreinterestingexample
"[true,{\"answer\":42}]"
127.0.0.1:6379>  JSON.SET arr . []
OK
127.0.0.1:6379> JSON.ARRAPPEND arr . 0
(integer) 1
127.0.0.1:6379> JSON.GET arr
"[0]"
127.0.0.1:6379> JSON.ARRINSERT arr . 0 -2 -1
(integer) 3
127.0.0.1:6379> JSON.GET arr
"[-2,-1,0]"
127.0.0.1:6379> JSON.ARRTRIM arr . 1 1
(integer) 1
127.0.0.1:6379> JSON.GET arr
"[-1]"
127.0.0.1:6379> JSON.ARRPOP arr
"-1"
127.0.0.1:6379> JSON.ARRPOP arr
(nil)
127.0.0.1:6379> JSON.SET obj . '{"name":"Leonard Cohen","lastSeen":1478476800,"loggedOut": true}'
OK
127.0.0.1:6379> JSON.OBJLEN obj 
(integer) 3
127.0.0.1:6379> JSON.OBJKEYS obj .
1) "name"
2) "lastSeen"
3) "loggedOut"


################
/etc/init.d/redis
#! /bin/bash
#   
# redis - this script starts and stops the redis-server daemon
#   
# chkconfig:    2345 80 90
# description:  Redis is a persistent key-value database
#   
### BEGIN INIT INFO
# Provides:          redis
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    redis-server daemon
# Description:        redis-server daemon
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
   
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/${REDISPORT}.conf"
   
case "$1" in
    start)
        if [ -f $PIDFILE ]; then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo -n "Starting Redis server..."
                $EXEC $CONF
                if [ "$?"="0" ]; then
                    echo " done"
                else
                    echo " failed"
                fi
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]; then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping  Redis server..."
                $REDIS_CLI -p $REDISPORT shutdown
                if [ "$?"="0" ]; then
                    echo " done"
                else
                    echo " failed"
                fi
        fi
        ;;
   restart)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart}" >&2
        exit 1
esac

#centos
chmod +x /etc/init.d/redis
/etc/init.d/redis start
chkconfig add redis

#ubuntu
chmod +x /etc/init.d/redis
/etc/init.d/redis start
update-rc.d redis defaluts
apt-get install sysv-rc-conf -y
sysv-rc-conf修改启动级别
#
tail -f /usr/local/redis/log/6379.log
......
15578:M 08 Jul 16:28:37.503 # <ReJSON> JSON data type for Redis v999.999.999 [encver 0]
15578:M 08 Jul 16:28:37.504 * Module 'ReJSON' loaded from /usr/local/redis/modules/rejson.so
......
对ReJSON的命令提供客户端库支持的一些语言:

Project
(Language)(License)
(Author -- Link)

#
iorejson     
(Node.js)(MIT)
(Evan Huang @evanhuang8 -- https://github.com/evanhuang8)
git:
https://github.com/evanhuang8/iorejson
npm:
https://www.npmjs.com/package/iorejson
#
JReJSON	
(Java)(BSD-2-Clause)
(Redis Labs -- https://redislabs.com/)
git:
https://github.com/RedisLabs/JReJSON/
#
rejson-py	
(Python)(BSD-2-Clause)	
(Redis Labs -- https://redislabs.com/)
git:
https://github.com/RedisLabs/redis-py/
pypi:
https://pypi.python.org/pypi/rejson
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 48
博文 156
码字总数 83777
×
eui988
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: