R meets MongoDB
æè¿ãå²ã¨å¤§ãããªãã¼ã¿ãRã§æ±ãéã«ãã©ã®ãããªã½ãªã¥ã¼ã·ã§ã³ããããè²ã
ã¨èãã¦ãã¾ãã
ããã§ã®ã½ãªã¥ã¼ã·ã§ã³ã¯ä¸¦åè¨ç®äºã
ã¨ãã観ç¹ã§ã¯ãªãã大ããã®ãã¼ã¿ããå¿
è¦ãªãã¼ã¿ããã£ããã¨ç¨æãã¦ã解æãã§ã¼ãºã«ç°¡åã«å
¥ã£ã¦ããããã«ãã¨ãã観ç¹ã§ãã
1ã¤ã¯æåãããbigmemoryã¨ããã½ãªã¥ã¼ã·ã§ã³ããã£ã¦ãããã¯ä»å¾æµ¸éãã¦ãããã ãããªãã¨ã¯æãã¤ã¤ãbigmemoryã§ã¯ãã¼ã¿åãbig.matrixã¨ããç¹æ®ãªãã®ã«ãªã£ã¦ãã¾ãã®ã§ãæ¢åã®Matrixãdata.frameã使ã£ãããã±ã¼ã¸ã®é¢æ°ã使ããªããªã£ã¦ãã¾ã*1ã¨ããç¹ãå¾®å¦ã ãªãã¨æã£ã¦ãã¾ãã
ãããªæã«ãTwitterã®timellineãçºãã¦ããããMongoDBã®R driverãã§ããã¨ãããtweetãè¦ããã¾ããã
MongoDBã¯ããªãæè»ã«è²ã
ã§ãã¾ãããä½ããä»ã¢ãããããã¯ã®ï¼ã¤ã§ãã®ã§ã試ãã£ãããªãã§ãããã
çµè«ããè¨ãã¨ãããªãç°¡åã«ç°å¢ãä½ããä¸ã«ãè²ã ã¨æè»ã«è§£æåºç¤ãæ§ç¯ã§ãããã§ããæãã ãªãã¨æã£ã¦ãã¾ãã
MongoDBã®ã¤ã³ã¹ãã¼ã«
MongoDBã¯ããã¥ã¡ã³ãããã£ãããã¦ãã¾ãã®ã§ãç°å¢ã«åããã¦
Downloads - MongoDB
http://www.mongodb.org/display/DOCS/Quickstart
ãè¦ãªããé²ããã°é常ã«ç°¡åã§ããä»åã®æ¬é¡ã§ã¯ãªãã®ã§å²æ
rmongodbã®ã¤ã³ã¹ãã¼ã«
githubä¸ã«ã³ã¼ããä¸ãã£ã¦ãã¾ãã®ã§ã¨ã£ã¦ãã¾ããã
$ cd /path/to/local/git/repository/ $ git clone https://github.com/gerald-lindsly/rmongodb.git
ããã§ãã³ã·ã§ã³ãä¸ãã£ã¦ãã¾ãã
$ R CMD install rmongodb
ã¨ãã¦ãã¨ã©ã¼ãã§ã¾ããbson.hããªãã¨ãããããªæãã§æããã¾ãã
å¿ãé®ãã¦ããã¥ã¡ã³ããèªã¿ã¾ãããã
README.mdã«ã¤ã³ã¹ãã¼ã«æé ãæ¸ãã¦ããã¾ãã®ã§ãããã«å¾ãã¾ãã
After cloning the repo, drop mongo-c-driver's src directory into rmongodb/rmongodb/src
ãªã®ã§ã
mongo-c-driver/src at master · mongodb/mongo-c-driver · GitHubããmongo-c-driverãè½ã¨ãã¦æ¥ã¾ãã
$ cd /path/to/local/git/repository/ $ git clone https://github.com/mongodb/mongo-c-driver/tree/master/src
次ã«è½ã¨ãã¦æ¥ãã½ã¼ã¹ã³ã¼ãrmongodbã®ãã£ã¬ã¯ããªã«çªã£è¾¼ã¿ã¾ãã
ããã§ä¸ç¹è½ã¨ãç©´ãããã¾ãã
mongo-c-driverã®src"ãã£ã¬ã¯ããª"ãrmongodb/romongodb/srcã«çªã£è¾¼ããã¨æ¸ãã¦ããã¾ãããå®éã¯mongo-c-driverã®srcãã£ã¬ã¯ããª"以ä¸ã®ãã¡ã¤ã«ä¸å¼"ãrmonogodb/rmonogodb/srcã«çªã£è¾¼ã¿ã¾ãã
src以ä¸ã«srcãã£ã¬ã¯ããªã§ããã¨ããããã¼ã¨æã£ã¦ãã¾ãããããã£ã±ãéãã¾ããã
ããã¨ãè±èªã®èª¤èªã§ãããï¼
ã½ã¼ã¹ã³ã¼ããæå®ã®å ´æã«ç½®ããå¾ã
$ R CMD install rmongodb
ããã§ããã¾ãã¤ã³ã¹ãã¼ã«ããã¾ããã
ããããã¼
rmongodbã使ã£ã¦ã¿ã
ã¾ãã¯ããããã«MongoDBãèµ·åãã¾ãã
$ /path/to/monogo-bin/mongod
ãã®ç¶æ ã§Rãèµ·åãã¾ãã
$ R > library(rmongodb)
ãã¼ãrmonogodbãç¡äºã«èªã¿è¾¼ã¾ãã¾ãã
ãã®å¾ã©ãããã°ãããããããªãã®ã§ãããããã«
help(monogo)
ã¨å©ãã¨ãä¾ã表示ããã¾ãã
mongo <- mongo.create() if (mongo.is.connected(mongo)) { buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Joe") mongo.bson.buffer.append(buf, "age", 22L) b <- mongo.bson.from.buffer(buf) mongo.insert(mongo, "test.people", b) }
ããã¯MongoDBã¸ãã¼ã¿ã®æ¿å ¥ãè¡ã£ã¦ãã¾ãã
- mongodbã¸æ¥ç¶ãã¦(mongo.create())
- æ¥ç¶ããã¾ããã£ã¦ããã°(mongo.is.connected(monogo))
- bsonç¨ã®ãããã¡ã¼ãä½æ(monogo.bson.buffer.create())
- bufferã«{"name" : "Joe"}ã追å (mongo.bson.buffer.append(buf, "name", "Joe"))
- åæ§ã«{"age" : 22L}ã追å
- bufferãbsonå½¢å¼ã«å¤æ(mongo.bson.from.buffer(buf))
- db:test, collection:peopleã«ãã¼ã¿ãinsert(mongo.insert(mongo, "test.people", b))
ä¸ã§ã§ããã¬ã³ã¼ãã¨ãã¦ã¯ã{"name" : "Joe", "age" : 22L}ã§ããã
ãã¼ã¿ãåå¾ããã¨ãã¯
mongo <- mongo.create() if (mongo.is.connected(mongo)) { buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Joe") mongo.bson.buffer.append(buf, "age", 22L) b <- mongo.bson.from.buffer(buf) mongo.insert(mongo, "test.people", b) }
mongo.find()ãmongo.find.one()ãå©ç¨ãã¾ãã
åãã
help(mongo.find) help(mongo.find.one)
ã§ä¾ã確èªã§ãã¾ãã
mongo.find.one()ã使ã£ã¦ãå ã»ã©insertãããã¼ã¿ãåå¾ãã¦ã¿ã¾ãã
mongo <- mongo.create() if (mongo.is.connected(mongo)) { buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Joe") query <- mongo.bson.from.buffer(buf) mongo.find.one(mongo, "test.people", query)) }
ã¨ããã¨
> mongo.find.one(mongo, "test.people", query) _id : 7 4e60f5382607c787a9ccc2b6 name : 2 Joe age : 16 22
ãããªæãã§ãã¼ã¿ãåå¾ã§ãã¾ãã
ææ
MongoDBã¯å人çã«æ³¨ç®ã®ãããã¯ã§ãã£ããæ°ã«ãªã£ã¦ããã®ã§ããããªããªã触ãæ©ä¼ãä½ãã¦ãã¾ããã§ããã
Rã¯é常ã«ä¾¿å©ãªè¨èªãªã®ã§ããããã¼ã¿éã大ãããªã£ã¦ããã¨ãã«ãæ¢ã«åºåããã¦ãããã¼ã¿ãã¡ã¤ã«ãèªã¿è¾¼ãã§ä»»æã®ã¬ã³ã¼ããåå¾ã»çµåã»ã»ã»ã¿ãããªãã¨ãããã®ãã¡ãã£ã¨ããã©ããããªãã¨ããæãã§ããã®ã§ãmongodbãæ°è»½ã«ä½¿ããã¨ããã®ã¯é常ã«ããæãã ã¨æãã¾ããã
ã¾ããã¤ã³ã¹ãã¼ã«æé (Cã®ãã©ã¤ããå
¨é¨çªã£è¾¼ã)ããrmongodbã®ããã¥ã¡ã³ããèªãã§ããã¨ãã©ãããCã®APIãRããå©ãããã«ãªã£ã¦ãããããé常ã«å¤ãã®é¢æ°ãæä¾ããã¦ãã¾ãã
MongoDBèªä½ãç°¡åã«æä½ã§ããããã«ãé常ã«æè»ã«ãããããªäºãã§ãããããªæ°ããã¾ããã
ããããæãã§Rããæ°è»½ã«NoSQLãæ±ããã®ã¯ä»å¾é常ã«æçã ãªã¼ã¨æãã¾ãã
MongoDBã使ã£ã¦ããµã¼ãã¹ãå¢ãã¦æ¥ã¦ã¾ãããï¼
追è¨
buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Joe") query <- mongo.bson.from.buffer(buf) mongo.find.one(mongo, "test.people", query))
ããããæ¸ãæ¹ãåé·ã ããªããæ°ææªããªãã¨æã£ã¦ããããªã¹ãã使ã£ã¦ä»¥ä¸ã®ããã«æ¸ããã¿ããã§ãã
mongo.find.one(mongo, "test.people", list(name="Joe"))
ãããªããã£ãããã¦é常ã«ããæãï¼ï¼
*1:[:]ã§matrixã«å¤æã§ãã¾ããããã¯æ¬æ«è»¢åã ã¨æãã¾ããã»ã»ã»