ã¯ããã«
ãNoSQLã¨ãããã¼ã¯ã¼ãã注ç®ãéãã¦ããæ°å¹´ãã¡ã¾ããNoSQLã¨ãããã®ãç°¡åã«ããããããã¨ãNoSQLã¨ã¯Not Only SQLã®ç¥èªã§ããããã¼ã¿ãã¼ã¹ã®å½¢å¼ã¨ãã¦ããã¾ã§å¤§å¢ãå ãã¦ä¸»æµã§ãã£ãRDBMSï¼Relational DataBase Management Systemï¼ã«å¯¾æããæ¦å¿µã¨ãã¦ç»å ´ãã¾ãããè¿å¹´ã®ãã¼ã¿å¢ãWebã¢ããªã±ã¼ã·ã§ã³ã«ãããã¹ã±ã¼ã©ããªãã£ã®å¿ è¦æ§ãããå¾æ¥ã®RDBMSã§ã¯æ§é çã«é©ããªããã¼ã¿æ§é ã®ã·ã¹ãã ãRDBMSãæ±ããåé¡ã解決ãããã®ã¨ãã¦æ°å¤ãã®ãã¼ã¿ãã¼ã¹ãç»å ´ãã¦ãã¾ããç¾å¨ãNoSQLã®å¤§ããªåé¡ã¨ãã¦ã¯3種é¡ã«ããããã¦ãã¾ãã
ãã¼ã»ããªã¥ã¼å
ããã¼ã¨å¤ï¼ããªã¥ã¼ï¼ã¨ããæ¯è¼çåç´ãªæ§é ã®ãã¼ã¿ãã¼ã¹ã代表çãªãã®ã¯RiakãRedisãMemcachedãªã©ã
ããã¥ã¡ã³ãå¿å
ã1件ã®ãã¼ã¿ãããã¥ã¡ã³ãã¨å¼ã³ããã¼ã¿æ§é ãèªç±ã§ã¹ãã¼ãã¬ã¹ãªæ§é ã®ãã¼ã¿ãã¼ã¹ã代表çãªãã®ã¯MongoDBãCouchDBã
åå¿å
ããã¼ã«å¯¾ãã¦ä»£è¡¨çãªãã®ã¯HBaseãCassandraã
ãæ¬é£è¼ã§ã¯ãããNoSQLã¨å¼ã°ãããã¼ã¿ãã¼ã¹ã®ä¸ãããMongoDBãåãä¸ãã¾ããé£è¼ã¯2åã«ããã¦è¡ãã第1åã§ã¯ãMongoDBã®ã¤ã³ã¹ãã¼ã«ãããåºæ¬æä½ãéãã¦MongoDBç¹æã®æ¦å¿µããã¼ã¿ã®æ±ãæ¹ã解説ãã¾ãã第2åã§ã¯ãJavaã®ã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã§ããSpringã®ãµãããã¸ã§ã¯ãã§ããSpring Dataã使ã£ã¦MongoDBãå©ç¨ããã¢ããªã±ã¼ã·ã§ã³ã®éçºã解説ãã¾ãã
MongoDBã¨ã¯
ãMongoDBã¨ã¯ãã¢ã¡ãªã«ã®10gen社ã«ãã£ã¦éçºãè¡ããã¦ããé常ã«ã¹ã±ã¼ã©ãã«ã§é«æ§è½ãªãªã¼ãã³ã½ã¼ã¹ã®ããã¥ã¡ã³ãæåãã¼ã¿ãã¼ã¹ã§ããWindows/Linux/MacOS/Soralisã®å種ãã©ãããã©ã¼ã åãã®ãã¤ããªãé å¸ãã¦ããä»ãã©ã¤ãã©ãªãJava/Ruby/Python/PHPãªã©å¤ãã®è¨èªã«å¯¾å¿ãã¦ãã¾ãã
ãMongoDBã®ç¹å¾´ã¨ãã¦ããã¼ã¿ãBSONï¼Binary JSONï¼ã¨ããå½¢å¼ã§ä¿åããããã¨ã§ããBSONã¨ã¯JSONï¼JavaScript Object Notationï¼ã©ã¤ã¯ãªããã¥ã¡ã³ãããã¤ããªå½¢å¼ã«ã·ãªã¢ã©ã¤ãºãããã®ã§ãããJSONåæ§ã«ååã¨å¤ã®ãã¢ã®éã¾ãã«ãã£ã¦ãã¼ã¿ã表ç¾ãã¾ãã
ãã¾ããKVSã¨ã¯ç°ãªã£ã¦ã¯ã¨ãªãç¨ãã¦é常ã«è¤éãªãã¼ã¿æ¤ç´¢ãå¯è½ã§ãããã以å¤ã«ãã¤ã³ããã¯ã¹ã®ãµãã¼ããã¬ããªã±ã¼ã·ã§ã³ããªã¼ãã·ã£ã¼ãã£ã³ã°ã¨ãã£ãé常ã«è±å¯ãªæ©è½ãåãã¦ãNoSQLãã¼ã¿ãã¼ã¹ã¨ããã¾ããä¸æ¹ãMongoDBãåãã¦ããªããã®ã¨ãã¦ä»£è¡¨çãã®ã¯ãã©ã³ã¶ã¯ã·ã§ã³ã§ããã¾ãSQLã®JOINã®ãããªãªãã¬ã¼ã·ã§ã³ãè¡ãã¾ããããã©ã³ã¶ã¯ã·ã§ã³ã¯ä½¿ãã¾ãããã1ããã¥ã¡ã³ãã«å¯¾ããã¢ãããã¯ãªå¦çã¯ãµãã¼ãããã¦ãã¾ãã
ããã®ãããªç¹å¾´ãæã¤MongoDBã§ãããçè ã®å人çãªææ³ãè¨ããã¦ããã ãã¨ãKVSããã¯ãªãã¿ãããRDBMSããæè»ã¨ãã£ãæãã§ãé常ã«ä½¿ãåæããããã®ã ã¨æãã¾ããã¾ããã¯ã¨ãªãªã©ã®åºæ¬æä½ãé常ã«ç´æçã§ãããã¨ãããåå¿è ã«ã使ããããã ãã§ãªãRDBMSã®å©ç¨è ããã移è¡ãããããã¨æããã¾ãã
MongoDBã®ã¤ã³ã¹ãã¼ã«
ãã§ã¯ãæ©éMongoDBãã¤ã³ã¹ãã¼ã«ãã¦ããããã¨æãã¾ããMongoDBã¯ã¤ã³ã¹ãã¼ã«ãé常ã«ç°¡åã§ããå è¿°ããããã«MongoDBã¯Windows/Linux/MacOS/Soralisããµãã¼ããã¦ããããããã®ãã©ãããã©ã¼ã åãã®ãã¤ããªãé å¸ããã¦ãã¾ããã¾ããyumãhomebrewãªã©ã®å種ãã©ãããã©ã¼ã ã®ããã±ã¼ã¸ç®¡çã·ã¹ãã åãã®ããã±ã¼ã¸ãç¨æããã¦ãã¾ããä»åã¯ä¾ã¨ãã¦é å¸ããã¦ãããã¤ããªãå©ç¨ããMac OS Xã§ã®å°å ¥ãç´¹ä»ãã¾ããããã®æ¹æ³ã®å ´åãã©ã®ãã©ãããã©ã¼ã ã§ãæé ã¯å¤§ããå¤ããã¾ãããå ¬å¼ãµã¤ãã®ãã¦ã³ãã¼ããã¼ã¸ããã使ãã®ç°å¢ã«ãããã¦ãã¦ã³ãã¼ããã¦ãã ããã
ãã§ã¯ã¾ãã¯ããã¦ã³ãã¼ããµã¤ãããä»»æã®ãã£ã¬ã¯ããªã«ãã¤ããªãã¡ã¤ã«ããã¦ã³ãã¼ããã¦å±éãã¾ããä»åã¯è¨äºå·çæç¹ã§ææ°ã®2.2.2ã使ã£ã¦ãã¾ãã
$ wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.2.2.tgz $ tar xvfz mongodb-osx-x86_64-2.2.2.tgz x mongodb-osx-x86_64-2.2.2/GNU-AGPL-3.0 x mongodb-osx-x86_64-2.2.2/README x mongodb-osx-x86_64-2.2.2/THIRD-PARTY-NOTICES x mongodb-osx-x86_64-2.2.2/bin/mongodump x mongodb-osx-x86_64-2.2.2/bin/mongorestore x mongodb-osx-x86_64-2.2.2/bin/mongoexport x mongodb-osx-x86_64-2.2.2/bin/mongoimport x mongodb-osx-x86_64-2.2.2/bin/mongostat x mongodb-osx-x86_64-2.2.2/bin/mongotop x mongodb-osx-x86_64-2.2.2/bin/mongooplog x mongodb-osx-x86_64-2.2.2/bin/mongofiles x mongodb-osx-x86_64-2.2.2/bin/bsondump x mongodb-osx-x86_64-2.2.2/bin/mongoperf x mongodb-osx-x86_64-2.2.2/bin/mongosniff x mongodb-osx-x86_64-2.2.2/bin/mongod x mongodb-osx-x86_64-2.2.2/bin/mongos x mongodb-osx-x86_64-2.2.2/bin/mongo
ã次ã«ãã¼ã¿ç¨ã®ãã£ã¬ã¯ããªãä½æãã¾ããMongoDBã¯ãæ¨æºã§ã¯/data/dbã«ãã¼ã¿ãæ¸ãè¾¼ã¿ã¾ããå¾ã£ã¦ä»åã¯/data/dbã¨ãããã£ã¬ã¯ããªãä½æãã¾ãããªããèµ·åæã®ã³ãã³ãã©ã¤ã³ã®ãªãã·ã§ã³ã§--dbpathãæå®ããã¨ãæ¨æºãã£ã¬ã¯ããªä»¥å¤ããã¼ã¿ãã£ã¬ã¯ããªã¨ãã¦å©ç¨ã§ãã¾ãã
$ mkdir -p /data/db
ãã¤ã³ã¹ãã¼ã«ã¯ä»¥ä¸ã§ããã§ã¯ç¶ãã¦èµ·åãã¦ã¿ã¾ããããèµ·åã«ä½¿ç¨ããã³ãã³ãã¯mongodã§ããåããã£ã¬ã¯ããªã«ã¯mongoã¨ããã³ãã³ããããã¾ãã®ã§ãééãã®ãªãããã«ãã¾ãããã
$ cd mongodb-osx-x86_64-2.2.2 $ ./bin/mongod
ãèµ·åãéå§ããã¨ã¡ãã»ã¼ã¸ã表示ãããæå¾ã«"waiting for connections on port 27017"ã¨ããã¡ãã»ã¼ã¸ã表示ããã¦ããã°èµ·åã¯æåã§ãã
ããªãããã®èµ·åã®ä»æ¹ã§ã¯ãã©ã¢ã°ã©ã¦ã³ãã§èµ·åããã¦ããããããã°ã¢ã¦ãããã¨ããã»ã¹ãåæ¢ãã¾ãããã¼ã¢ã³ã¨ãã¦ããã¯ã°ã©ã¦ã³ãã§èµ·åããã«ã¯ãmongodã³ãã³ãã®å®è¡æã«--forkãªãã·ã§ã³ãã¤ãã¦å®è¡ãã¦ãã ããããã ããããã¯ã°ã©ã¦ã³ãã§èµ·åããå ´åã¯ããããã¦"--logpath"ãããã¯"--syslog"ãªãã·ã§ã³ã®æå®ãå¿ è¦ã§ãããããã¯ãæ¨æºåºåã«åºåããããã°ã¡ãã»ã¼ã¸ããã¡ã¤ã«ã«ãã®ã³ã°ããããã®ãªãã·ã§ã³ã§ãããã®ä»ã®ç´°ãããªãã·ã§ã³ã¯"mongod -h"ã§ç¢ºèªãããã¨ãã§ãã¾ãã
ãç¶ãã¦ãèµ·åããMongoDBã«å¯¾ãã¦MongoDBã¯ã©ã¤ã¢ã³ãã使ã£ã¦æ¥ç¶ãã¦ã¿ã¾ããMongoDBã¯ã©ã¤ã¢ã³ãã¯å¯¾è©±åã®ã·ã§ã«ã¨ãªã£ã¦ãããmongodã¨åããã£ã¬ã¯ããªã«ããmongoã³ãã³ãã§èµ·åãã¾ãã
$ ./bin/mongo MongoDB shell version: 2.2.2 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user >
ãMongoDBã¯ã©ã¤ã¢ã³ãã®å®è¡æã«ä½ããªãã·ã§ã³ãæå®ããªãå ´åã¯ãèªåçã«ãã¼ã«ã«ã®MongoDBï¼localhostã®27017ãã¼ãï¼ã®testã¨ãããã¼ã¿ãã¼ã¹ã«æ¥ç¶ããã¾ãããªãããã®ãã¼ã«ã¯JavaScriptã®ã·ã§ã«ã§ãMongoDBã®ã³ãã³ããå®è¡ã§ããã ãã§ãªãJavaScriptã®å®è¡ãå¯è½ã¨ãªã£ã¦ãã¾ããæ¬è¨äºã§ã¯ä»¥éãMongoDBã¯ã©ã¤ã¢ã³ãã®ãã¨ã¯åã«ã·ã§ã«ã¨å¼ã³ã¾ãã