Redis扩展功能


目录

  1. Redis的扩展功能
  2. RedisJSON

参考/来源

Redis的扩展功能

在官网的文档中,在Redis Stack部分,展示了一些Redis的扩展功能,目的是为了更好的供开发者使用。

原文:

Redis Stack
Extend Redis with modern data models and processing engines.
Redis Stack is an extension of Redis that adds modern data models and processing engines to provide a complete developer experience.

In addition to all of the features of OSS Redis, Redis stack supports:

  • Queryable JSON documents
  • Full-text search
  • Time series data (ingestion & querying)
  • Graph data models with the Cypher query language
  • Probabilistic data structures

译文:

Redis 技术栈
使用现代数据模型和处理引擎扩展Redis。
Redis Stack是Redis的扩展,它添加了现代数据模型和处理引擎,以提供完整的开发人员体验。

除了OSS Redis的所有功能外,Redis stack还支持:

  • 可查询的JSON文档
  • 全文搜索
  • 时间序列数据(摄取和查询)
  • 使用Cypher查询语言的图形数据模型
  • 概率数据结构

  • RedisInsight

    Visualize and optimize Redis data

  • RediSearch

    Queries, secondary indexing, and full-text search for Redis

  • RedisJSON

    JSON support for Redis

  • RedisGraph

    A Graph database built on Redis

  • RedisTimeSeries

    Ingest and query time series data with Redis

  • RedisBloom

    Bloom filters and other probabilistic data structures for Redis

    Redis自己实现的布隆过滤器

这里以RedisJSON进行简单介绍

RedisJSON

RedisJSON 是一个 Redis 模块,允许从 Redis 键(文档)存储、更新和获取 JSON 值。

主要特点:

  • 完全支持 JSON 标准
  • 用于在文档中选择元素的 类似 JSONPath 的语法
  • 文档以树状结构存储为二进制数据,允许快速访问子元素
  • 所有 JSON 值类型的类型化原子操作

命令格式:JSON.{cmd} key JsonPath value

127.0.0.1:6380> JSON.SET test . '{"key": "value"}'
OK
127.0.0.1:6380> JSON.SET test ".n" "999"   // 添加数字元素到test
OK
127.0.0.1:6380> JSON.SET test .array '[1, 2, 3]'  // 添加列表元素到test
OK
127.0.0.1:6380> JSON.GET test
"{\"key\":\"value\",\"n\":999,\"array\":[1,2,3]}"
127.0.0.1:6380> JSON.GET test .array[0]     // 按照JSONPath获取对应位置的值
"[1]"

127.0.0.1:6380> JSON.SET test .array[0] 99     //  修改array 列表中第一个元素值为99
OK
127.0.0.1:6380> JSON.GET test
"{\"key\":\"value\",\"n\":999,\"array\":[99,2,3]}"

对于新的 Redis 键, path 必须是根,即. 。修改JSON数据使用path指定, 对应数据的位置。

对于标准的JSON数据对象,字符串, 数字, 布尔, 列表和字典也定义了不同的命令:

  1. 数字

    JSON数据有对应数字的操作JSON.NUMINCRBY, 另外还增加了乘法JSON.NUMMULTBY和乘方JSON.NUMPOWBY的操作

    127.0.0.1:6380> JSON.NUMINCRBY test .n 1    // 路径n对应数据 +1
    "1000"
    127.0.0.1:6380> JSON.NUMINCRBY test .n -990  // 路径n对应数据 -990
    "10"
    127.0.0.1:6380> JSON.NUMMULTBY test .n 2    // 路径n对应数据 *2
    "20"
    127.0.0.1:6380> JSON.NUMPOWBY test .n 2     // 路径n对应数据 ^2
    "400"
    127.0.0.1:6380> JSON.GET test
    "{\"key\":\"value\",\"n\":400,\"array\":[99,2,3]}"
  2. 字符

    对应str类型的数据, JSON支持 JSON.STRAPPENDJSON.STRLEN 与对应原生redis的appendstrlen命令

    127.0.0.1:6380> set a "a"
    OK
    127.0.0.1:6380> append a "bc"
    (integer) 3
    127.0.0.1:6380> strlen a
    (integer) 3
    127.0.0.1:6380> JSON.STRAPPEND test .key '"alo"'
    (integer) 8
    127.0.0.1:6380> JSON.get test
    "{\"key\":\"valuealo\",\"n\":400,\"array\":[99,2,3]}"
    127.0.0.1:6380> JSON.STRLEN test .key
    (integer) 8
  3. 数组

    使用JSON.ARRAPPENDJSON.ARRINSERT添加元素

    127.0.0.1:6380> JSON.ARRAPPEND test .array 4    // 列表后面添加数据
    (integer) 4
    127.0.0.1:6380> JSON.GET test .array
    "[99,2,3,4]"
    
    127.0.0.1:6380> JSON.ARRINSERT test .array 0 '"new"'    // 插入到对应坐标位置
    (integer) 5
    127.0.0.1:6380> JSON.GET test .array
    "[\"new\",99,2,3,4]"

    JSON.ARRINDEX查询数据坐标, JSON.ARRLEN获取数据长度

    127.0.0.1:6380> JSON.GET test .array
    "[\"new\",99,2,3,4]"
    127.0.0.1:6380> JSON.ARRINDEX test .array 4     // 查找数据对应的坐标, 存在多个时返回第一个坐标
    (integer) 3
    127.0.0.1:6380> JSON.ARRINDEX test .array 99
    (integer) 1
    
    127.0.0.1:6380> JSON.ARRLEN test .array    // 获取数据长度
    (integer) 4

    使用JSON.ARRPOP弹出数据

    127.0.0.1:6380> JSON.ARRPOP test .array 1    // 弹出对应位置的数据
    "99"
    127.0.0.1:6380> JSON.GET test .array
    "[\"new\",2,3,4]"
    127.0.0.1:6380> JSON.ARRPOP test .array 6    // index 大于长度时, 弹出最后一个数据
    "4"

    使用JSON.ARRTRIM裁剪数据

    127.0.0.1:6380> JSON.SET test .array "[\"new\",2,3,4,5,6]"
    OK
    127.0.0.1:6380> JSON.GET test .array
    "[\"new\",2,3,4,5,6]"
    127.0.0.1:6380> JSON.ARRTRIM test .array 1 4    // 修建数组, 只保留指定范围内数据
    (integer) 4
    127.0.0.1:6380> JSON.GET test .array
    "[2,3,4,5]"
  4. 对象

    JSON.OBJKEYSJSON.OBJLEN

    127.0.0.1:6380> JSON.GET test
    "{\"key\":\"valuealo\",\"n\":400,\"array\":[2,3,4,5]}"
    127.0.0.1:6380> JSON.OBJKEYS test
    1) "key"
    2) "n"
    3) "array"
    127.0.0.1:6380> JSON.OBJLEN test
    (integer) 3

文章作者: 小小千千
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小小千千 !
评论
  目录