利用R將本地資料檔案讀入redis資料庫中

2021-06-25 17:48:27 字數 3640 閱讀 3697

從本地讀取乙個文字資料檔案,從左到右分別是使用者id,口令,郵箱,在redis建立合適的資料模型,並將這些資料匯入到redis

先將作業素材的資料放在r的預設路徑下

[root@192 ~]# r

r version 3.1.0 (2014-04-10) -- "spring dance"

platform: i686-pc-linux-gnu (32-bit)

r is free software and comes with absolutely no warranty.

you are welcome to redistribute it under certain conditions.

type 'license()' or 'licence()' for distribution details.

natural language support but running in an english locale

r is a collaborative project with many contributors.

type 'contributors()' for more information and

'citation()' on how to cite r or r packages in publications.

type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an html browser inte***ce to help.

type 'q()' to quit r.

> data<-scan(file="第5周作業素材.txt",what=character(),sep=" ")

read 205 items

> data<-data[which(data!='#')]

> data

[1] "wolys"                    "wolysopen111"           

[3] "[email protected]"           "coralshanshan"          

[5] "601601601"                "[email protected]"        

[7] "pengfeihuchao"            "woaidami"               

[9] "[email protected]"         "simulategirl"           

[11] "@#$9608125"               "[email protected]"    

[13] "daisypp"                  "12345678"               

[15] "[email protected]"  "sirenxing424"           

[17] "tfiloveyou"               "[email protected]"   

[19] "raininglxy"               "1901061139"             

[21] "[email protected]"         "leochenlei"             

[23] "leichenlei"               "[email protected]"  

[25] "z370433835"               "lkp145566"              

[27] "[email protected]"         "cxx0409"                 

[29] "12345678"                 "[email protected]"  

# 載入rredis庫

> library(rredis)

#遠端連線redis server

> redisconnect(host="127.0.0.1",port=6379)

> redisflushall()

[1] "ok"

> rediskeys()

null

#迴圈插入資料

> id<-null

> for(i in 1:length(data)) else if(i %% 3 == 2) else {

+     redissadd(paste("users:",id,sep=""),paste("email:",data[i],sep=""))

#列出所有的key

> rediskeys()

[1] "users:xl200811"      "users:candly8509"    "users:bokil"       

[4] "users:yoyomika"      "users:simulategirl"  "users:xiazai200901"

[7] "users:ai3min2"       "users:mengjie007"    "users:darksoul0929"

[10] "users:alex126126"    "users:q1718334567"   "users:cxx0409"     

[13] "users:abc654468252"  "users:ysdz8"         "users:huangdaqiao" 

[16] "users:fxh852"        "users:coralshanshan" "users:licaijun007" 

[19] "users:jiongjiongmao" "users:baoautumn"     "users:llx6888"     

[22] "users:jacksbalu"     "users:sirenxing424"  "users:babylovebooks"

[25] "users:yiling1007"    "users:fennal"        "users:pengfeihuchao"

[28] "users:demonhaodh"    "users:leochenlei"    "users:daisypp"     

[31] "users:wolys"         "users:hudaoyin"      "users:boiny"       

[34] "users:baby19881018"  "users:beibeilong012" "users:xldq_l"      

[37] "users:raininglxy"    "users:pengcfwxh"     "users:xiaochuan2018"

[40] "users:kangte1"       "users:z370433835" 

#通過key查詢value

> redissmembers("users:wolys")

[[1]]

[1] "email:[email protected]"

[[2]]

[1] "id:wolys"

[[3]]

[1] "pw:wolysopen111"

#關閉redis連線

> redisclose()

完成測試案例。

判斷讀入資料檔案結尾 從檔案讀入資料

參考 在使用c c 讀檔案的時候,使用eof 這個函式來判斷檔案是否為空或者是否讀到檔案結尾的時候會有一些特殊情況 先看 include include using namespace std int main e return 0 上述 在vs2012下編譯執行,發現,當檔案結尾沒有空行時,結果正...

利用datafile copy將資料檔案重新命名

思路 o 利用rman對資料檔案做copy o 將資料檔案offline o switch檔名,這一步將更改控制檔案中資料檔案對應的檔名 o recover資料檔案 o 將資料檔案online,恢復生產 注 也可以利用這種思路做database copy,然後做switch database,遷移資...

mysql 載入本地資料檔案

和資料檔案 create table pet name varchar 20 owner varchar 20 species varchar 20 char 1 birth date,death date hadoop master cat datafile.txt fluffy harold c...