成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

R語言利劍之NoSQL系列:Redis

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

Redis環境準備 rredis函數庫 rredis基本使用操作 rredis使用案例

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

***章 Redis環境準備

文字說明部分:

首先環境準備,這里我選擇了Linux Ubuntu操作系統12.04的64位服務器版本,大家可以根據自己的使用習慣選擇順手的Linux。

 

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

查看Redis服務器環境

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

在服務器端,用telnet連接redis-server

用telnet插入數據,讀取數據

R語言環境2.15.0,WinXP通過遠程連接,訪問Redis server。

代碼部分:

查看操作系統 

  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.  

查看系統進程 

  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 '^]'.  

插入數據

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

查詢數據 

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

R語言開發環境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函數庫

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

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

文字說明部分:

建立連接,關閉連接

  1. redisConnect() , redisClose()  

清空當前/所有數據庫數據 

  1. redisFlushDB() , redisFlushAll()  

列出所有KEY值,KEY的數量 

  1. redisKeys(), redisDBSize()  

選擇切換數據庫:0是默認數據庫 

  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')  

左邊插入數組對象,右邊插入數組對象

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

左邊彈出一個數組對象, 右邊彈出一個數組對象, 

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

從左邊顯示數組對象列表 

  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個函數

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()函數,建立與Redis Server的連接。如果是本地連接redisConnect()不要參數,下面例子使用遠程連接,增加host參數配置IP地址。redisConnect(host=“192.168.1.101”,port=6379)

redis的基本操作:建議鏈接,切換數據庫,列表顯示所有KEY值,清空當前數據庫數據,清空所有數據庫數據,關閉鏈接,

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

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. #切換數據庫1 
  19. redisSelect(1) 
  20.     [1] "OK" 
  21. redisKeys() 
  22.     NULL 
  23.  
  24. #切換數據庫0 
  25. redisSelect(0) 
  26.     [1] "OK" 
  27. redisKeys() 
  28.     [1] "x"    "data" 
  29.  
  30. #清空當前數據庫數據 
  31. redisFlushDB() 
  32.     [1] "OK" 
  33.  
  34. #清空所有數據庫數據 
  35. redisFlushAll() 
  36.     [1] "OK" 
  37.  
  38. #關閉鏈接 
  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. #設置數據過期時間 
  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. #刪除數據     
  29. redisDelete('x') 
  30.     [1] 1 
  31. redisGet('x') 
  32.     NULL 

#p#

list類型操作

  1. #從數組左邊插入數據 
  2. redisLPush('a',1) 
  3. redisLPush('a',2) 
  4. redisLPush('a',3) 
  5.  
  6. #顯示從數組左邊0-2的數據 
  7. redisLRange('a',0,2) 
  8.     [[1]] 
  9.     [1] 3 
  10.     [[2]] 
  11.     [1] 2 
  12.     [[3]] 
  13.     [1] 1 
  14.  
  15. #從數據左邊彈出一個數據 
  16. redisLPop('a') 
  17.     [1] 3 
  18.  
  19. #顯示從數組左邊0-(-1)的數據    
  20. redisLRange('a',0,-1) 
  21.     [[1]] 
  22.     [1] 2 
  23.  
  24.     [[2]] 
  25.     [1] 1 
  26.  
  27. #從數組右邊插入數據 
  28. redisRPush('a','A') 
  29. redisRPush('a','B') 
  30.  
  31. #顯示從數組左邊0-(-1)的數據 
  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. #從數據右邊彈出一個數據 
  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客戶端插入數據,rredis讀取數據

  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插入數據,redis客戶端讀取數據

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

轉型以數組方式存儲(charToRaw)

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

第四章 rredis測試案例

測試案例的需求:

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

文字說明部分:

首先,定義數據模型:

KEY:

users:用戶id

VALUE:

id:用戶id

pw:口令

email:郵箱

R語言讀入數據文件。

然后,建立redis連接,以循環方式插入數據。

以users:wolys為KEY,輸出對應用的VALVE值。

代碼部分

  1. #讀入數據 
  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. #循環插入數據 
  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. #關閉redis連接 
  56. redisClose() 

完成測試案例。

數據文件: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】

責任編輯:彭凡 來源: 統計之都
相關推薦

2014-07-31 09:13:54

R語言MongoDB

2019-03-20 15:59:11

NoSQLRedis數據庫

2013-04-10 10:31:21

R語言

2011-07-06 16:36:40

Redis

2013-05-31 10:15:29

R語言

2017-10-17 11:58:54

R語言UpSetR可視化

2021-05-18 07:15:37

Python

2013-05-16 09:37:14

R語言

2010-08-31 09:11:58

2010-03-11 14:37:47

Visual StudScrum

2012-08-29 16:41:44

信息安全RSA華為

2017-11-17 18:40:54

華為

2013-05-24 10:01:40

R語言

2011-05-16 10:29:44

HandlerSockNoSQL

2011-07-13 09:58:15

HBase

2011-08-01 08:56:06

CouchDBSQLiteNoSQL

2018-03-12 22:13:46

GO語言編程軟件

2014-07-11 10:09:30

2011-05-30 09:27:35

NoSQL評測

2013-12-23 17:29:43

NewSQLNoSQL
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: www.五月婷婷.com | 久久国产婷婷国产香蕉 | 精品国产精品三级精品av网址 | 日韩黄| 中文字幕成人免费视频 | 久久综合伊人一区二区三 | 成人精品一区二区三区中文字幕 | 中文字幕视频在线 | 国产三级精品三级在线观看四季网 | www..com18午夜观看 | 中文字幕av在线一二三区 | 一区二区三区四区毛片 | 天天草夜夜骑 | 日韩国产免费观看 | 波多野结衣先锋影音 | 亚洲天堂一区 | 欧美福利在线 | 伊人网影院 | 亚洲成人精选 | 色婷婷婷婷色 | 精品一区在线 | 国产一区二区在线免费播放 | 第一区在线观看免费国语入口 | 免费h在线 | 麻豆va| 欧美一区二区网站 | 日韩成人在线播放 | 亚洲视频欧美视频 | 成人在线免费网站 | 国外成人免费视频 | 二区av| 日韩第1页| 亚洲精品久久久一区二区三区 | 日批免费看 | 人人操日日干 | 国产精品久久毛片av大全日韩 | 欧美成人手机在线 | 久久99精品久久久久久青青日本 | 午夜免费观看网站 | 欧美福利| 日本精品在线观看 |