NoSQL series: Redis (1)
Redis environment preparation rredis function library rredis basic Use Cases
Each chapter is divided into "text description" and "code section" to ensure consistency between the text description and the code.
Chapter 1 prepare the Redis Environment
Text description:
First, prepare the environment. Here I chose the 64-bit server version of Linux Ubuntu operating system 12.04. You can choose a proper Linux version based on your usage habits.
Redis installation is skipped. Sudo apt-get install redis-server
View the Redis server environment
Run the/etc/init. d/redis-server command to start redis-server. The default port is port = 6379.
On the server side, use telnet to connect to redis-server
Use telnet to insert data and read data
In the R language environment 2.15.0, WinXP remotely connects to the Redis server.
Code Section:
View Operating System
- ~ uname -a Linux AY121111030241cda8003 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux ~ cat /etc/issue Ubuntu 12.04.1 LTS \n \l
Start redis
- ~ /etc/init.d/redis-server start Starting redis-server: redis-server.
View System Processes
- ~ ps -aux|grep redis redis 20128 0.0 0.0 10676 1428 ? Ss 16:39 0:00 /usr/bin/redis-server /etc/redis/redis.conf
View startup logs
- ~ cat /var/log/redis/redis-server.log
- [20128] 14 Apr 16:39:43 * Server started, Redis version 2.2.12
- [20128] 14 Apr 16:39:43 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
- [20128] 14 Apr 16:39:43 * The server is now ready to accept connections on port 6379
Telnet to redis-server
- ~ telnet localhost 6379 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
Insert data
- rpush data 1 :1 rpush data 2 :2
Query data
- lrange data 0 -1 *2 $1 1 $1 2
R Language Development Environment 2.15.0, WinXP
~ R R version 2.15.0 (2012-03-30) Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit)
Chapter 2 rredis function library
Rredis provides the 100 function to operate on redis applications. Although there are many functions, the usage is relatively simple, the R language is flexible enough, and the code is concise.
The following lists all the rredis function libraries. I only select some common introduction.
Text description:
Establish a connection and close the connection
- redisConnect() , redisClose()
Clear current/All Database Data
- redisFlushDB() , redisFlushAll()
List all KEY values and number of keys
- redisKeys(), redisDBSize()
Select to switch Database: 0 is the default database
- redisSelect(0)
Insert string objects in batches
- redisSet('x',runif(5)), redisMSet(list(x=pi,y=runif(5),z=sqrt(2)))
Reads string objects in batches
- redisGet('x'), redisMGet(c('x','y','z'))
Delete object
- redisDelete('x')
Insert array object on the left and array object on the right
- redisLPush('a',1), redisRPush('a','A')
An array object is displayed on the left, and an array object is displayed on the right,
- redisLPop('a'), redisRPop('a')
List of array objects on the left
- redisLRange('a',0,-1)
Insert a set object
- redisSAdd('A',runif(2))
Displays the set object with several elements. The List displays the set object elements.
- redisSCard('A'), redisSMembers('A')
Display the difference set, intersection, and union of two set objects
- redisSDiff(c('A','B')),redisSInter(c('A','B')),redisSUnion(c('A','B'))
Code Section:
A total of 100 functions
redisAuth redisBgRewriteAOF redisBgSave redisBLPop redisBRPop redisBRPopLPush redisClose redisCmd redisConnect redisDBSize redisDecr redisDecrBy redisDelete redisDiscard redisEval redisExec redisExists redisExpire redisExpireAt redisFlushAll redisFlushDB redisGet redisGetContext redisGetResponse redisGetSet redisHDel redisHExists redisHFields redisHGet redisHGetAll redisHIncrBy redisHKeys redisHLen redisHMGet redisHMSet redisHSet redisHVals redisIncr redisIncrBy redisInfo redisKeys redisLIndex redisLLen redisLPop redisLPush redisLRange redisLRem redisLSet redisLTrim redisMGet redisMonitorChannels redisMove redisMSet redisMulti redisPublish redisRandomKey redisRename redisRPop redisRPopLPush redisRPush redisSAdd redisSave redisSCard redisSDiff redisSDiffStore redisSelect redisSet redisSetBlocking redisSetContext redisShutdown redisSInter redisSInterStore redisSIsMember redisSlaveOf redisSMembers redisSMove redisSort redisSPop redisSRandMember redisSRem redisSubscribe redisSUnion redisSUnionStore redisTTL redisType redisUnsubscribe redisUnwatch redisWatch redisZAdd redisZCard redisZIncrBy redisZInterStore redisZRange redisZRangeByScore redisZRank redisZRem redisZRemRangeByRank redisZRemRangeByScore redisZScore redisZUnionStore
Chapter 3 basic operations of rredis
Text description:
First, install the rredis class library and load the class library.
RedisConnect (host = "192.168.1.101", port = 6379)
Then, use the redisConnect () function to establish a connection with the Redis Server. If the local connection redisConnect () is not a parameter, the following example uses remote connection and adds the host parameter to configure the IP address. RedisConnect (host = "192.168.1.101", port = 6379)
Basic operations of redis: it is recommended to link to switch the database. The List displays all KEY values, clears the current database data, clears all database data, and closes the link,
String-type operations: insert, read, delete, insert and set expiration time, batch operations
List Operation: insert, read, and pop-up
Set Operations: insert, read, intersection, difference set, and Union
Interaction between rredis and redis-cli
Code Section:
Basic operations of redis:
- # Installing rredis
- Install. packages (rredis)
-
- # Loading the rredis class library
- Library (rredis)
-
- # Remote connection to redis server
- RedisConnect (host = "192.168.1.101", port = 6379)
-
- # Listing all keys
- RedisKeys ()
- [1] "x" "data"
-
- # How many keys are displayed
- RedisDBSize ()
- [1] 2
-
- # Switching database 1
- RedisSelect (1)
- [1] "OK"
- RedisKeys ()
- NULL
-
- # Switching database 0
- RedisSelect (0)
- [1] "OK"
- RedisKeys ()
- [1] "x" "data"
-
- # Clearing data in the current database
- RedisFlushDB ()
- [1] "OK"
-
- # Clear all database data
- RedisFlushAll ()
- [1] "OK"
-
- # Close the link
- RedisClose ()
String type operation:
- # Insert object
- RedisSet ('x', runif (5 ))
- 1] "OK"
-
- # Reading objects
- RedisGet ('x ')
- [1] 0.67616159 0.06358643 0.07478021 0.32129140 0.16264615
-
- # Set the data expiration time
- RedisExpire ('x', 1)
- Sys. sleep (1)
- RedisGet ('x ')
- NULL
-
- # Batch insert
- RedisMSet (list (x = pi, y = runif (5), z = sqrt (2 )))
- [1] TRUE
-
- # Batch read
- RedisMGet (c ('x', 'y', 'z '))
- $ X
- [1] 3.141593
- $ Y
- [1] 0.9249501 0.3444994 0.6477250 0.1681421 0.2646853
- $ Z
- [1] 1.414214
-
- # Deleting data
- RedisDelete ('x ')
- [1] 1
- RedisGet ('x ')
- NULL