Divided into 4 chapters.
Redis Environment Preparation Rredis function library Rredis Basic use Action Rredis use case
Each chapter is divided into "Text Description section" and "code section" to maintain the coherence between text description and code.
Chapter One REDIS environment preparationText Description section:
First environment preparation, here I chose the Linux Ubuntu operating system 12.04 64-bit server version, you can use their own habits to choose the right Linux.
The Redis installation process has been skipped.
sudo apt-get install Redis-server
View the REDIS server environment
Using the/etc/init.d/redis-server command, start Redis-server, default port: port=6379
On the server side, connect Redis-server with Telnet
Use Telnet to insert data and read data.
The R locale 2.15.0,WINXP access to Redis server over a remote connection.
Code section:
View operating System
~ uname-a Linux ay121111030241cda8003 3.2.0-29-generic #46-ubuntu SMP Fri Jul 17:03:23 UTC + x86_64 x86_64 x86_6 4 gnu/linux~ cat/etc/issue Ubuntu 12.04.1 LTS \ \l
Start Redis
~/etc/init.d/redis-server start starting redis-server:redis-server.
viewing 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 Boot Log
~ Cat/var/log/redis/redis-server.log [20128] Apr 16:39:43 * Server started, Redis version 2.2.12 [20128] 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.overcom Mit_memory=1 ' for the take effect. [20128] APR 16:39:43 * The server is now a ready-to-accept connections on port 6379
Telnet connection Redis-server
~ telnet localhost 6379 Trying 127.0.0.1 ... Connected to localhost. Escape character is ' ^] '.
Inserting data
Rpush Data 1:1 Rpush data 2:2
Querying data
Lrange Data 0-1 * $1 $2
R Language Development Environment 2.15.0,WINXP
~ RR version 2.15.0 (2012-03-30) Copyright (C) The R Foundation for statistical COMPUTINGISBN 3-900051-07-0PLATFORM:I 386-pc-mingw32/i386 (32-bit)
Chapter II Rredis function library
Rredis provides 100 functions for applying REDIS operations. Although there are many functions, but the usage is relatively simple, the R language support is flexible enough, the code is relatively concise.
All of the Rredis libraries are listed below, and I'll just pick a few common introductions.
Text Description section:
Make a connection, close the connection
Redisconnect (), Redisclose ()
Empties current/All database data
Redisflushdb (), Redisflushall ()
List all key values, number of keys
Rediskeys (), Redisdbsize ()
Select Switch Database: 0 is the default database
Redisselect (0)
Insert a String object, BULK Insert
Redisset (' x ', runif (5)), Redismset (List (x=pi,y=runif (5), Z=SQRT (2)))
Read a String object, bulk Read
Redisget (' x '), Redismget (C (' x ', ' y ', ' z '))
Delete Object
Redisdelete (' x ')
Insert Array object to the left, insert array object to the right
Redislpush (' A ', 1), Redisrpush (' A ', ' a ')
An array object pops up on the left and an array object pops to the right.
Redislpop (' A '), Redisrpop (' a ')
Display list of array objects from the left
Redislrange (' A ', 0,-1)
Inserting a set type Object
Redissadd (' A ', runif (2))
Show set object has several elements, list shows set object element
Redisscard (' A '), Redissmembers (' a ')
Displays the difference set, intersection, and set of two set objects
Redissdiff (C (' A ', ' B ')), Redissinter (C (' A ', ' B ')), Redissunion (C (' A ', ' B '))
Code section:
A total of 100 functions
Redisauthredisbgrewriteaofredisbgsaveredisblpopredisbrpopredisbrpoplpushrediscloserediscmdredisconnectredisdbsizeredisdec Rredisdecrbyredisdeleteredisdiscardredisevalredisexecredisexistsredisexpireredisexpireatredisflushallredisflushdbredisget Redisgetcontextredisgetresponseredisgetsetredishdelredishexistsredishfieldsredishgetredishgetallredishincrbyredishkeysred Ishlenredishmgetredishmsetredishsetredishvalsredisincrredisincrbyredisinforediskeysredislindexredisllenredislpopredislpus Hredislrangeredislremredislsetredisltrimredismgetredismonitorchannelsredismoveredismsetredismultiredispublishredisrandomk Eyredisrenameredisrpopredisrpoplpushredisrpushredissaddredissaveredisscardredissdiffredissdiffstoreredisselectredissetred Issetblockingredissetcontextredisshutdownredissinterredissinterstoreredissismemberredisslaveofredissmembersredissmoveredi Ssortredisspopredissrandmemberredissremredissubscriberedissunionredissunionstoreredisttlredistyperedisunsubscriberedisunw AtchrediswatchrediszaddrediszcarDrediszincrbyrediszinterstorerediszrangerediszrangebyscorerediszrankrediszremrediszremrangebyrankrediszremrangebyscorered Iszscorerediszunionstore
Chapter III Rredis Basic use of OperationsText Description section:
First, to install the Rredis class Library, load the class library.
Redisconnect (host= "192.168.1.101", port=6379)
Then, through the Redisconnect () function, establish a connection to the Redis server. If the local connection redisconnect () does not have parameters, the following example uses a remote connection to increase the host parameter configuration IP address. Redisconnect (host= "192.168.1.101", port=6379)
Basic Redis operations: Suggest links, switch databases, list displays all key values, empties current database data, empties all database data, closes links,
String type operation: Insert, read, delete, insert and set expiration time, bulk operation
List type operation: Insert, read, eject
Set type operation: Insert, read, intersect, difference set, and set
Rredis and REDIS-CLI Interactive operation
Code section:Basic operations for Redis:
#安装rredisinstall. Packages (Rredis) #加载rredis类库library (Rredis) #远程连接redis serverredisconnect (host= "192.168.1.101", port=6379) #列出所有的keysredisKeys () [1] "x" "Data" #显示有多少个keyredisDBSize () [1] * Switch database 1redisSelect (1) [1] "OK" redis Keys () null# switch database 0redisSelect (0) [1] "OK" Rediskeys () [1] "x" "Data" #清空当前数据库数据redisFlushDB () [1] "OK" #清空所有数据 Library data Redisflushall () [1] "OK" #关闭链接redisClose ()
String type operation:
#插入对象redisSet (' x ', runif (5)) 1] "OK" #读取对象redisGet (' x ') [1] 0.67616159 0.06358643 0.07478021 0.32129140 0.16264615# Set number According to Expiration Time Redisexpire (' x ', 1) sys.sleep (1) redisget (' x ') null# BULK Insert Redismset (List (x=pi,y=runif (5), Z=SQRT (2))) [1] true# Bulk 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# Delete data redisdelete (' x ') [1] 1redisGet (' x ') NULL
List Type Operation
#从数组左边插入数据redisLPush (' A ', 1) redislpush (' A ', 2) redislpush (' A ', ' 3 ') #显示从数组左边0-2 data redislrange (' A ', 0,2) [[1]] [1] 3 [[2]] [1] 2 [[3]] [1] # from the left side of the data popup a data redislpop (' a ') [1] 3# display from the left of the array 0-(-1) Data Redislrange (' A ', 0,-1) [[1]] [1] 2 [[2]] [1] # from the right of the array to insert data Redisrpush (' A ', ' a ') redisrpush (' A ', ' B ') #显示从数组左边0-(-1) Data Redislrange (' A ', 0,-1) [[1]] [1] 2 [[2]] [1] 1 [[3]] [1] "a" [[4]] [1] "B" #从数据右边弹出一个数据redisRPop (' a ')
set type Operation
Redissadd (' A ', runif (2)) Redissadd (' A ', ') #显示对象有几个元素redisSCard (' a ') [1] 2# List display Set object element redissmembers (' A ') [[1]] [1] 55 [[2]] [1] 0.6494041 0.3181108redissadd (' B ', redisSAdd) (' B ', rnorm (3)) The #显示对象有几个元素redisSCard (' B ') [1] 2# list shows the Set object element redissmembers (' B ') [[1]] [1] 55 [[2]] [1] 0.1074787 1.3111006 0.8223434# difference Set Redissdiff (C (' A ', ' B ')) [ [1]] [1] 0.6494041 0.3181108# intersection Redissinter (C (' A ', ' B ')) [[1 ]] [1] 55# redissunion (C (' A ', ' B ')) [[1]] [1] 55 [[2]] [1] 0.1074787 1.3111006 0.8223434 [[3]] [1] 0.6494041 0.3181108
Rredis interacting with redis-cli
Redis Client inserts data, Rredis reads data
#打开redis客户端 ~ Redis-cliredis 127.0.0.1:6379> Set Shell "Greetings, R client!" Okredisget (' Shell ') [1] "Greetings, R client!"
Rredis Insert data, Redis client reads data
#插入数据redisSet (' R ', ' Greetings, Shell client! ') [1] "OK" #读取数据 (garbled) Redis 127.0.0.1:6379> get R "x\\x00\x00\x00\x02\x00\x02\x0f\x00\x00\x02\x03\x00\x00\x00\x00\ X10\x00\x00\x00\x01\x00\x04\x00\\x00\x00\x00\x18greetings, Shell client! "
Transformation is stored in array mode (CHARTORAW)
Redisset (' R ', Chartoraw (' Greetings, Shell client! ')) [1] true# normal read data Redis 127.0.0.1:6379> get R "Greetings, Shell client!"
Fourth Chapter Rredis test Cases
Requirements for test cases:
Read in a data file, from left to right are user IDs, passwords, mailboxes, build the appropriate data model in Redis, and import this data into Redis.
Text Description section:
R language to read into the data file.
Then, establish a Redis connection to insert the data in a circular fashion.
With Users:wolys as key, the valve value for the application is output.
Code Section
#读入数据data <-scan (file= "Data5.txt", What=character (), sep= " ") Data<-data[which (data!= ' # ')]> data [1] "Wolys" "wolysopen111" "[email protected]" [4] "Coralshanshan" "601601601" "[email protected]" [7 ] "Pengfeihuchao" "Woaidami" "[email protected]" [10] "Simulategirl" "@#$9608125" "[email protected]" [13] "DAISYPP" " 12345678 " " [ Email protected] " [16] " sirenxing424 " "Tfiloveyou" "[email protected]" [19] " Raininglxy " " 1901061139 " "[email protected]" [22] "Leochenlei" "Leichenlei" "[email protected]" [25] "z370433835" "lkp145566" "[email protected]" [28] "cxx0409" "12345678" "[email protected]" [31] "xldq_l" "061222ll" "[email protected]" #连接redis连接redisConnect (host= " 192.168.1.101 ", port=6379) Redisflushall () Rediskeys () #循环插入数据id <-nullfor (i in 1:length (data)) { if (i %% 3 == 1) { id<-data[i] Redissadd (Paste ("Users:", id,sep= ""), Paste ("ID:", id,sep= "")) } else if (i %% 3 &NBSP;==&NBSP;2) { redissadd (Paste ("Users:", id,sep= ""), Paste ("PW:", data[i],sep= "") ) } else { redissadd (Paste ("Users:", id,sep= ""), Paste ("email:", data [i],sep= "]) }} #列出所有的KEYredisKeys () [1] " users:cxx0409 " " users:sirenxing424 " " Users:simulategirl " " users:xldq_l " [5] "Users:coralshanshan" "Users:raininglxy" "Users:pengfeihuchao" "Users:leochenlei" [9] "USERS:DAISYPP" "Users:wolys" "users:z370433835" #通过KEY查询VALUEredisSMembers ("Users:wolys") [[1]] [1] "pw:wolysopen111" [[2]] [1] "email:[email protected" [[3]] [1] "Id:wolys" #关闭redis连接redisClose ()
Complete the test case.
Data file: Data5.txt
Wolys # wolysopen111 # [Email Protected]coralshanshan # 601601601 # [Email Protected]pengfeihuchao # woaidami # [Email Pro Tected]simulategirl # @#$9608125 # [Email protected]daisypp # 12345678 # [Email protected]sirenxing424 # tfiloveyou # [EMA] Il protected]raininglxy # 1901061139 # [Email protected]leochenlei # leichenlei # [Email protected]z370433835 # lkp145566 # [Email protected]cxx0409 # 12345678 # [Email protected]xldq_l # 061222ll # [email protected]
Combined use of R language and Redis