1/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
CS-695 NoSQL DatabaseRedis (part 1 of 2)
Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge
19 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 201519 Nov. 2015
2/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
Table of contents I
1 Miscellanea
2 DB comparisons
3 Assgn. #7
4 Historical origins
5 Data model
6 CRUDy stuff
7 Other operations
8 Conclusion
9 References
3/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
Corrections and additions since last lecture.
Assignment #07 is available
4/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
How different DBs compare to a RDBMS
We have some terms to compare now[2]
RDBMS K/V Columnar Doc. Graphic Redis
DB. instance cluster cluster instance — —database — namespace — — idtable bucket table collection — —row key-value row document node id key-valuerowid key — id — keycol. — col. fam. — — keyschema — — database —join — — DBRef — INT— — — — rel. id —
5/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
Words of explanation.
The full text is available at:http://www.cs.odu.edu/~ccartled/Teaching/2015-Fall/NoSQL/Assignments/07/
In general terms:
1 Design the database,particularially the definition of a“key”
2 Parse data
3 Populate the database
4 Query the database using the keys
5 Manipulate the results (may beminimal based on how keys aredefined)
6 Plot data
6/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
It is young
Redis means REmote Dictionary Server[4]
Some characterize it as amore of a data store than adata base
Based on key/value pairs,value can be:
Lists,Sets,Sorted strings,Hash tables,Other data types.
Commands for each datatype
Supports publish/subscribe,master/slave replication,persistence, scripting [1]
7/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
What is in the backend?
Not to clear.
In memory data storage
Data is persisted at regularintervals
Slaves get copies frommaster
8/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
Pre-CRUDy stuff
There is a server and a CLI.
Databases exist if the server is running.
Default port:6379
There are default databasesnumbered 0 through 15(inclusive)
At CLI, use the command:select 1 to select databasenumber 1
You can also telnet to theserver and issue commandsdirectly.
9/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
CRUDy stuff
Creating data[3]
Redis is a key value data store, so . . .
Create a key1
Associate a value with the key
Multiple creation and sets can be combined as one operation
Format: SET key value valueExample: SET 7wks http://www.sevenweeks.org/Example: MSET gog http://www.google.com yahhttp://www.yahoo.com
1Many of the following examples use a colon (:) as a separator. The colondoes not have any significance.
10/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
CRUDy stuff
Reporting data
Redis is a key value data store, so . . .
Values are reported based on the key used to search thedatabase
Wild cards can be used to search for keys
Multiple key value searches can be batched2
Format: GET key valueFormat: MGET key value key valueFormat: KEYS patternExample: GET 7wksExample: KEYS *
2Not recommended for production systems. Consider SCAN or sets.
11/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
CRUDy stuff
Updating data
Redis is a key value data store, so . . .
Updating a key/value pair is simply using the old key with anew value, or
Appending a new string to the current value
Format: SET key value valueFormat: MSET key value value key value valueFormat: APPEND key value valueExample: SET 7wks http://www.sevenweeks.org/Example: MSET gog http://www.google.com yahhttp://www.yahoo.comExample: APPEND mykey “Hello”
12/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
CRUDy stuff
Deleting data
Redis is a key value data store, so . . .
Deleting a key/value pair is simply deleting the key
Format: DEL key valueFormat: MDEL key valueExample: DEL 7wksExample: MDEL gog yah fiddle
13/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
A hash can contain multiple values
Hash values are faster than simple keys (for some operations). Ahash can have one or more values. The hash can be used to returnthe things it points at:
Single key/value pairs,
Multiple key/value pairs.
Format: HSET hash value key value valueFormat: HMSET hash value key value value key value value . . .Example: HSET myhash field1 “Hello”Example: HMSET user:eric name “Eric Redmond” passwords3cret
14/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
Retrieving data associated with a hash.
There are different ways of getting the a hashed value:
Get all the values associated with a hash,
Get all the keys associated with a hash,
Get a single key/value pair associated with a hash.
Format: HVALS hash valueFormat: HKEYS hash valueFormat: HGET hash value key valueExample: HVALS user:ericExample: HKEYS user:ericExample: HGET user:eric password
15/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
Redis does lists.
Lists are ordered values associated with one key value. Lists canact as queues or stacks. Redis supports “pushes,” “pops,” and“copy” operations.
Create a list,
Add a value to the list (beginning or end),
Remove a value (head or tail).
(Only “stack” operations shown.)Format: RPUSH list value value value . . .Format: RPOP list valueExample: RPUSH mylist “hello” “world”Example: RPOP mylist
16/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
Redis does sets.
“Sets are unordered collections with no duplicatevalues and are an excellent choice for performing complexoperations between two or more key values, such asunions or intersections.”
Redmond and Wilson [2]
Format: SADD set value value value . . .Format: SMEMBERS set valueFormat: SINT set value1 set value2Example: SADD news nytimes.com pragprog.comExample: SADD tech pragprog.com apple.comExample: SMEMBERS newsExample: SINTER news tech
17/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
Built in publish-subscribe support
Consumers subscribes to a “channel.” A producer publishes to a“channel.” All updates to a “channel” are sent to all subscribers.Format: SUBSCRIBE channel [channel . . . ]Format: PUBLISH channel “message”Example: SUBSCRIBE commentsExample: PUBLISH comments “Check out this shortcoded site!7wks”
18/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
Redis “knows” math.
Most of the time Redis doesn’t care about the “value” in akey-value pair. Except when you want to do math.
You can increment/decrement a value,
By an integer, or a float.
Format: INCR key valueFormat: INCRBY key value valueExample: INCR stats:page:aboutExample: INCRBY ratings:video:12333 5Lots of commands dealing with math, values, and time3.
3http://redis.io/commands
19/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
A collection of operations that aren’t strictly CRUDy related
There is a logic to naming Redis operators.
Many times you can take the base operator and prepend the typeof key to get the name of the operator for that type of key. Forexample:
GET — get a single value based on a single key
MGET — get multiple values based on multiple keys
HMGET — get multiple values based on a given hash value
LPOP — get and remove the first element in a list
RPOP — get and remove the last element in a list
SPOP — get some number of elements from a set
Lots of commands (over 175) dealing with math, values, sets,persistence, and time4.
4http://redis.io/commands
20/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
What have we covered?
Reviewed assignment #07Covered some of the RedisCRUDy stuffNo class 26 Nov. 2015Next class 3 Dec. 2015Assignment #07 due beforenext class
Next time: continued Redis exploration
21/21
Miscellanea DB comparisons Assgn. #7 Historical origins Data model CRUDy stuff Other operations Conclusion References
References I
[1] Josiah L Carlson, Redis in action, Manning Publications Co., 2013.
[2] Eric Redmond and Jim R Wilson, Seven databases in seven weeks,Pragmatic Bookshelf, 2012.
[3] Redis Staff, Commands, http://redis.io/commands, 2015.
[4] Wikipedia Staff, Redis, 2015.