自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

R語言利劍之NoSQL系列:Redis

數(shù)據(jù)庫 Redis
R利劍NoSQL系列文章,主要介紹通過R語言連接使用NoSQL數(shù)據(jù)庫。涉及的NoSQL產(chǎn)品,包括Redis, MongoDB, HBase, Hive, Cassandra, Neo4j。希望通過我的介紹讓廣大的R語言愛好者,有更多的開發(fā)選擇,做出更多地激動人心的應(yīng)用。

Redis環(huán)境準備 rredis函數(shù)庫 rredis基本使用操作 rredis使用案例

每一章節(jié),都會分為”文字說明部分”和”代碼部分”,保持文字說明與代碼的連貫性。

***章 Redis環(huán)境準備

文字說明部分:

首先環(huán)境準備,這里我選擇了Linux Ubuntu操作系統(tǒng)12.04的64位服務(wù)器版本,大家可以根據(jù)自己的使用習慣選擇順手的Linux。

 

Redis安裝過程跳過。sudo apt-get install redis-server

查看Redis服務(wù)器環(huán)境

使用/etc/init.d/redis-server命令,啟動redis-server, 默認端口:port=6379

在服務(wù)器端,用telnet連接redis-server

用telnet插入數(shù)據(jù),讀取數(shù)據(jù)

R語言環(huán)境2.15.0,WinXP通過遠程連接,訪問Redis server。

代碼部分:

查看操作系統(tǒng) 

  1. ~ 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  

啟動redis 

  1. ~ /etc/init.d/redis-server start      Starting redis-server: redis-server.  

查看系統(tǒng)進程 

  1. ~ 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  

查看啟日志

  1. ~ cat  /var/log/redis/redis-server.log       
  2. [20128] 14 Apr 16:39:43 * Server started, Redis version 2.2.12     
  3. [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.    
  4. [20128] 14 Apr 16:39:43 * The server is now ready to accept connections on port 6379  

telnet連接redis-server

  1. ~ telnet localhost 6379      Trying 127.0.0.1...     Connected to localhost.     Escape character is '^]'.  

插入數(shù)據(jù)

  1. rpush data 1     :1      rpush data 2     :2  

查詢數(shù)據(jù) 

  1. lrange data 0 -1     *2     $1     1     $1     2  

R語言開發(fā)環(huán)境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) 

第二章 rredis函數(shù)庫

rredis提供了100函數(shù),對應(yīng)用redis的操作。雖然函數(shù)也不少,但是用法都是比較簡單的,對R語言支持足夠靈活,代碼也比較簡潔。

下面列出了所有rredis函數(shù)庫,我只挑選一些常用的介紹。

文字說明部分:

建立連接,關(guān)閉連接

  1. redisConnect() , redisClose()  

清空當前/所有數(shù)據(jù)庫數(shù)據(jù) 

  1. redisFlushDB() , redisFlushAll()  

列出所有KEY值,KEY的數(shù)量 

  1. redisKeys(), redisDBSize()  

選擇切換數(shù)據(jù)庫:0是默認數(shù)據(jù)庫 

  1. redisSelect(0)      

插入string對象,批量插入 

  1. redisSet('x',runif(5)), redisMSet(list(x=pi,y=runif(5),z=sqrt(2)))  

讀取string對象,批量讀取 

  1. redisGet('x'), redisMGet(c('x','y','z'))  

刪除對象 

  1. redisDelete('x')  

左邊插入數(shù)組對象,右邊插入數(shù)組對象

  1. redisLPush('a',1), redisRPush('a','A')  

左邊彈出一個數(shù)組對象, 右邊彈出一個數(shù)組對象, 

  1. redisLPop('a'), redisRPop('a')  

從左邊顯示數(shù)組對象列表 

  1. redisLRange('a',0,-1)  

插入set類型對象 

  1. redisSAdd('A',runif(2))  

顯示set對象有幾個元素,列表顯示set對象元素 

  1. redisSCard('A'), redisSMembers('A')  

顯示兩個set對象的差集,交集,并集 

  1. redisSDiff(c('A','B')),redisSInter(c('A','B')),redisSUnion(c('A','B'))  

代碼部分:

共有100個函數(shù)

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 

第三章 rredis基本使用操作

文字說明部分:

首先,要安裝rredis類庫,加載類庫。

redisConnect(host=“192.168.1.101”,port=6379)

然后,通過redisConnect()函數(shù),建立與Redis Server的連接。如果是本地連接redisConnect()不要參數(shù),下面例子使用遠程連接,增加host參數(shù)配置IP地址。redisConnect(host=“192.168.1.101”,port=6379)

redis的基本操作:建議鏈接,切換數(shù)據(jù)庫,列表顯示所有KEY值,清空當前數(shù)據(jù)庫數(shù)據(jù),清空所有數(shù)據(jù)庫數(shù)據(jù),關(guān)閉鏈接,

string類型操作:插入,讀取,刪除,插入并設(shè)置過期時間,批量操作

list類型操作:插入,讀取,彈出

set類型操作:插入,讀取,交集,差集,并集

rredis與redis-cli的交互操作

代碼部分:

redis的基本操作:

  1. #安裝rredis 
  2. install.packages(rredis) 
  3.  
  4. #加載rredis類庫 
  5. library(rredis) 
  6.  
  7. #遠程連接redis server 
  8. redisConnect(host="192.168.1.101",port=6379
  9.  
  10. #列出所有的keys 
  11. redisKeys() 
  12.     [1] "x"    "data" 
  13.  
  14. #顯示有多少個key 
  15. redisDBSize() 
  16.     [1] 2 
  17.  
  18. #切換數(shù)據(jù)庫1 
  19. redisSelect(1) 
  20.     [1] "OK" 
  21. redisKeys() 
  22.     NULL 
  23.  
  24. #切換數(shù)據(jù)庫0 
  25. redisSelect(0) 
  26.     [1] "OK" 
  27. redisKeys() 
  28.     [1] "x"    "data" 
  29.  
  30. #清空當前數(shù)據(jù)庫數(shù)據(jù) 
  31. redisFlushDB() 
  32.     [1] "OK" 
  33.  
  34. #清空所有數(shù)據(jù)庫數(shù)據(jù) 
  35. redisFlushAll() 
  36.     [1] "OK" 
  37.  
  38. #關(guān)閉鏈接 
  39. redisClose() 

string類型操作:

  1. #插入對象 
  2. redisSet('x',runif(5)) 
  3.     1] "OK" 
  4.  
  5. #讀取對象 
  6. redisGet('x') 
  7.     [1] 0.67616159 0.06358643 0.07478021 0.32129140 0.16264615 
  8.  
  9. #設(shè)置數(shù)據(jù)過期時間 
  10. redisExpire('x',1) 
  11. Sys.sleep(1) 
  12. redisGet('x') 
  13.     NULL 
  14.  
  15. #批量插入 
  16. redisMSet(list(x=pi,y=runif(5),z=sqrt(2))) 
  17.     [1] TRUE 
  18.  
  19. #批量讀取 
  20. redisMGet(c('x','y','z')) 
  21.     $x 
  22.     [1] 3.141593 
  23.     $y 
  24.     [1] 0.9249501 0.3444994 0.6477250 0.1681421 0.2646853 
  25.     $z 
  26.     [1] 1.414214 
  27.  
  28. #刪除數(shù)據(jù)     
  29. redisDelete('x') 
  30.     [1] 1 
  31. redisGet('x') 
  32.     NULL 

#p#

list類型操作

  1. #從數(shù)組左邊插入數(shù)據(jù) 
  2. redisLPush('a',1) 
  3. redisLPush('a',2) 
  4. redisLPush('a',3) 
  5.  
  6. #顯示從數(shù)組左邊0-2的數(shù)據(jù) 
  7. redisLRange('a',0,2) 
  8.     [[1]] 
  9.     [1] 3 
  10.     [[2]] 
  11.     [1] 2 
  12.     [[3]] 
  13.     [1] 1 
  14.  
  15. #從數(shù)據(jù)左邊彈出一個數(shù)據(jù) 
  16. redisLPop('a') 
  17.     [1] 3 
  18.  
  19. #顯示從數(shù)組左邊0-(-1)的數(shù)據(jù)    
  20. redisLRange('a',0,-1) 
  21.     [[1]] 
  22.     [1] 2 
  23.  
  24.     [[2]] 
  25.     [1] 1 
  26.  
  27. #從數(shù)組右邊插入數(shù)據(jù) 
  28. redisRPush('a','A') 
  29. redisRPush('a','B') 
  30.  
  31. #顯示從數(shù)組左邊0-(-1)的數(shù)據(jù) 
  32. redisLRange('a',0,-1) 
  33.     [[1]] 
  34.     [1] 2 
  35.     [[2]] 
  36.     [1] 1 
  37.     [[3]] 
  38.     [1] "A" 
  39.     [[4]] 
  40.     [1] "B" 
  41.  
  42. #從數(shù)據(jù)右邊彈出一個數(shù)據(jù) 
  43. redisRPop('a') 

set類型操作

  1. redisSAdd('A',runif(2)) 
  2. redisSAdd('A',55) 
  3.  
  4. #顯示對象有幾個元素 
  5. redisSCard('A') 
  6.     [1] 2 
  7.  
  8. #列表顯示set對象元素 
  9. redisSMembers('A') 
  10.     [[1]] 
  11.     [1] 55 
  12.  
  13.     [[2]] 
  14.     [1] 0.6494041 0.3181108 
  15.  
  16. redisSAdd('B',55) 
  17. redisSAdd('B',rnorm(3)) 
  18.  
  19. #顯示對象有幾個元素 
  20. redisSCard('B') 
  21.     [1] 2 
  22.  
  23. #列表顯示set對象元素     
  24. redisSMembers('B') 
  25.     [[1]] 
  26.     [1] 55 
  27.  
  28.     [[2]] 
  29.     [1] 0.1074787 1.3111006 0.8223434 
  30.  
  31. #差集 
  32. redisSDiff(c('A','B')) 
  33.     [[1]] 
  34.     [1] 0.6494041 0.3181108 
  35.  
  36. #交集 
  37. redisSInter(c('A','B')) 
  38.     [[1]] 
  39.     [1] 55 
  40.  
  41. #并集 
  42. redisSUnion(c('A','B')) 
  43.     [[1]] 
  44.     [1] 55 
  45.  
  46.     [[2]] 
  47.     [1] 0.1074787 1.3111006 0.8223434 
  48.  
  49.     [[3]] 
  50.     [1] 0.6494041 0.3181108 

rredis與redis-cli交互

redis客戶端插入數(shù)據(jù),rredis讀取數(shù)據(jù)

  1. #打開redis客戶端 
  2. ~ redis-cli 
  3. redis 127.0.0.1:6379> set shell "Greetings, R client!" 
  4.     OK 
  5.  
  6. redisGet('shell') 
  7.     [1] "Greetings, R client!" 

rredis插入數(shù)據(jù),redis客戶端讀取數(shù)據(jù)

  1. #插入數(shù)據(jù) 
  2. redisSet('R', 'Greetings, shell client!') 
  3.     [1] "OK" 
  4.  
  5. #讀取數(shù)據(jù)(有亂碼) 
  6. redis 127.0.0.1:6379> get R 
  7.     "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!" 

轉(zhuǎn)型以數(shù)組方式存儲(charToRaw)

  1. redisSet('R', charToRaw('Greetings, shell client!')) 
  2.     [1] TRUE 
  3.  
  4. #正常讀取數(shù)據(jù) 
  5. redis 127.0.0.1:6379> get R 
  6.     "Greetings, shell client!" 

第四章 rredis測試案例

測試案例的需求:

讀入一個數(shù)據(jù)文件,從左到右分別是用戶id,口令,郵箱,在redis里建立合適的數(shù)據(jù)模型,并將這些數(shù)據(jù)導入到redis。

文字說明部分:

首先,定義數(shù)據(jù)模型:

KEY:

users:用戶id

VALUE:

id:用戶id

pw:口令

email:郵箱

R語言讀入數(shù)據(jù)文件。

然后,建立redis連接,以循環(huán)方式插入數(shù)據(jù)。

以users:wolys為KEY,輸出對應(yīng)用的VALVE值。

代碼部分

  1. #讀入數(shù)據(jù) 
  2. data<-scan(file="data5.txt",what=character(),sep=" "
  3. data<-data[which(data!='#')] 
  4.  
  5. > data 
  6.  
  7.      [1] "wolys"                   "wolysopen111"            "wolys@21cn.com"          
  8.      [4] "coralshanshan"           "601601601"               "zss1984@126.com"         
  9.      [7] "pengfeihuchao"           "woaidami"                "294522652@qq.com"        
  10.     [10] "simulategirl"            "@#$9608125"              "simulateboy@163.com"     
  11.     [13] "daisypp"                 "12345678"                "zhoushigang_123@163.com" 
  12.     [16] "sirenxing424"            "tfiloveyou"              "sirenxing424@126.com"    
  13.     [19] "raininglxy"              "1901061139"              "lixinyu23@qq.com"        
  14.     [22] "leochenlei"              "leichenlei"              "chenlei1201@gmail.com"   
  15.     [25] "z370433835"              "lkp145566"               "370433835@qq.com"        
  16.     [28] "cxx0409"                 "12345678"                "cxx0409@126.com"         
  17.     [31] "xldq_l"                  "061222ll"                "viv093@sina.com"   
  18.  
  19. #連接redis連接 
  20. redisConnect(host="192.168.1.101",port=6379
  21. redisFlushAll() 
  22. redisKeys() 
  23.  
  24. #循環(huán)插入數(shù)據(jù) 
  25. id<-NULL 
  26. for(i in 1:length(data)){ 
  27.   if(i %% 3 == 1) { 
  28.     id<-data[i] 
  29.     redisSAdd(paste("users:",id,sep=""),paste("id:",id,sep="")) 
  30.   } else if(i %% 3 == 2) { 
  31.     redisSAdd(paste("users:",id,sep=""),paste("pw:",data[i],sep="")) 
  32.   } else { 
  33.     redisSAdd(paste("users:",id,sep=""),paste("email:",data[i],sep="")) 
  34.   } 
  35.  
  36. #列出所有的KEY 
  37. redisKeys() 
  38.  
  39.      [1] "users:cxx0409"       "users:sirenxing424"  "users:simulategirl"  "users:xldq_l"        
  40.      [5] "users:coralshanshan" "users:raininglxy"    "users:pengfeihuchao" "users:leochenlei"    
  41.      [9] "users:daisypp"       "users:wolys"         "users:z370433835"    
  42.  
  43. #通過KEY查詢VALUE 
  44. redisSMembers("users:wolys") 
  45.  
  46.     [[1]] 
  47.     [1] "pw:wolysopen111" 
  48.  
  49.     [[2]] 
  50.     [1] "email:wolys@21cn.com" 
  51.  
  52.     [[3]] 
  53.     [1] "id:wolys" 
  54.  
  55. #關(guān)閉redis連接 
  56. redisClose() 

完成測試案例。

數(shù)據(jù)文件:data5.txt

  1. wolys # wolysopen111 # wolys@21cn.com 
  2. coralshanshan # 601601601 # zss1984@126.com 
  3. pengfeihuchao # woaidami # 294522652@qq.com 
  4. simulategirl # @#$9608125 # simulateboy@163.com 
  5. daisypp # 12345678 # zhoushigang_123@163.com 
  6. sirenxing424 # tfiloveyou # sirenxing424@126.com 
  7. raininglxy # 1901061139 # lixinyu23@qq.com 
  8. leochenlei # leichenlei # chenlei1201@gmail.com 
  9. z370433835 # lkp145566 # 370433835@qq.com 
  10. cxx0409 # 12345678 # cxx0409@126.com 
  11. xldq_l # 061222ll # viv093@sina.com 

原文鏈接:http://cos.name/2013/04/nosql-r-redis/ 

【責任編輯:彭凡 TEL:(010)68476606】

責任編輯:彭凡 來源: 統(tǒng)計之都
相關(guān)推薦

2014-07-31 09:13:54

R語言MongoDB

2019-03-20 15:59:11

NoSQLRedis數(shù)據(jù)庫

2013-04-10 10:31:21

R語言

2011-07-06 16:36:40

Redis

2017-10-17 11:58:54

R語言UpSetR可視化

2013-05-31 10:15:29

R語言

2021-05-18 07:15:37

Python

2013-05-16 09:37:14

R語言

2010-08-31 09:11:58

2013-05-24 10:01:40

R語言

2011-07-13 09:58:15

HBase

2011-05-16 10:29:44

HandlerSockNoSQL

2012-08-29 16:41:44

信息安全RSA華為

2017-11-17 18:40:54

華為

2010-03-11 14:37:47

Visual StudScrum

2011-08-01 08:56:06

CouchDBSQLiteNoSQL

2018-03-12 22:13:46

GO語言編程軟件

2023-11-29 16:20:21

2021-12-22 07:31:18

RedisNoSQL數(shù)據(jù)庫

2013-05-10 14:37:37

點贊
收藏

51CTO技術(shù)棧公眾號