Upload
redis-labs
View
306
Download
0
Embed Size (px)
Citation preview
Redis as a JSON document storeItamar Haber
What is JSON (emphasis mine)
ECMA-404 The JSON Data Interchange Standard
JSON (JavaScript Object Notation) is a lightweight
data-interchange format. It is easy for humans to
read and write. It is easy for machines to parse and
generate.
http://json.org/
2
Not a session about the JSON specification...object
{}{ members }
memberspairpair , members
pairstring : value
array[][ elements ]
elementsvaluevalue , elements
3
valuestringnumberobjectarraytruefalsenull
string""" chars "
charscharchar chars
charany-Unicode-Character-except-"-or-\-or-control-character\"\\\/\b\f\n\r\t\u four-hex-digits
numberintint fracint expint frac exp
Intdigitdigit1-9 digits - digit- digit1-9 digits
frac. digits
expe digits
digitsdigitdigit digits
eee+e-EE+E-
...though it does fit nicely in a single slide
There are three kinds of lies: lies, damned lies, and JSON standards.
tl;dr Parsing JSON is a Minefield
Fact #1: everyone uses JSON
Fact #2: many also use Redis
7
JSON Redis
Us
?
Two ways for storing JSON in Redis
Serialized, as a raw String
● Just SET/GET it, client
encodes/decodes
● Great for "forklift" ops
● And with Lua scripting:
○ Sub-element access
○ Atomic updates
8
Deserialized, in a Hash
● Decode->HMSET,
HMGET->Encode
● O(1) access
● Follows the Redis
spirit...
ReJSON = Redis + JSON
ReJSON is
● A custom JSON data type for Redis (v4 Modules API)
● Keys can contain any valid JSON value
○ Scalars, objects or arrays
○ Nested or not
● Data is stored decoded, in binary format
● JSONPath-like syntax for direct access to elements
● Strongly-typed atomic commands
127.0.0.1:6379> JSON.SET scalar . '"Hello JSON!"'
OK
127.0.0.1:6379> JSON.SET object . '{"foo": "bar",
"ans": 42}'
OK
127.0.0.1:6379> JSON.GET object
"{\"foo\":\"bar",\"ans\":42}"
127.0.0.1:6379> JSON.GET object .ans
"42"
ReJSON - basic SET and GET
JSON value -> ReJSON tree data structure
redis> JSON.SET j .
'{
"foo": "bar",
"ans": 42
}'
Type: object
fooans
Type: string"bar"
Type: number42
root
streaming lexer
Performance: 3468 bytes, 3 nesting levels
Throughput Average latencyReJSON Raw JSON & Lua MessagePack & Lua
General JSON.DEL, JSON.GET, JSON.MGET, JSON.SET &
JSON.TYPE
Numbers JSON.NUMINCRBY & JSON.NUMMULTBY
Strings JSON.STRAPPEND & JSON.STRLEN
Objects JSON.OBJKEYS & JSON.OBJLEN
Arrays JSON.ARRAPPEND, JSON.ARRINDEX,
JSON.ARRINSERT, JSON.ARRLEN, JSON.ARRPOP &
JSON.ARRTRIM
Other JSON.RESP
ReJSON commands
http://rejson.io - Thank you!
Itamar HaberChief OSS Education OfficerRedis Labs
@itamarhaber