ã·ã§ã«ã¹ã¯ãªããã ãã§MySQLããMongoDBã¸ã®ç§»è¡ãã¡ãã
ãã®ã¨ã³ããªã¯ãMySQL Casual Advent Calendar 23æ¥ç®ã®ã¨ã³ããªã§ãã
ã©ããã©ããä¹ã«ã¬ã¼æ§ã§ããæ¡éã§ãã
ãé
ãå
¥ã£ã¦ãã¨ããããããªããã¨ããããã¨ãããããã§ããï¼ããããªããï¼
ãã¦ããã°æ¸ããã¨æã£ã¦ã¢ããã³ãã«ã¬ã³ãã¼ã®ãã¼ã¸è¦ãªãããããªããæ¸ãã¦ãããã§ãããã
MySQLã ã£ã¤ã£ã¦ãã ãã
ã¨ãããã¨ã§ä½æ¸ããããªã¨æã£ã¦ããã§ããããã¿è¨äºã¨ãã¦ã·ã§ã«ã¹ã¯ãªããã ãã§MySQLããMongoDBã¸ã®ç§»è¡ãã¡ããã®ãããããã¨æãã¾ãã
誰å¾ãªã®ãã¨ãããã¨ã§ããã°ä¿ºå¾ã§ã(∗ᵒ̶̶̷̀Ï˂̶́∗)੭₎₎̊₊♡ウケï¾ï¾ï½¨!
ã¨è¨ãããªããæãããããããããããªããããããªãããã¿ãæãã¤ããªãã£ããã§ããã³ãã§ãm(_ _)m
ã¡ã ã¼ããã§ãã¹ããã¼ãã«ä½ã£ã¦ã¿ãã
ã¾ãã¯ç§»è¡å
ã®MySQLã«ãã¼ãã«ä½ã£ã¦ã¿ã¾ããã
é©å½ã«ã
mysql> CREATE DATABASE d1; mysql> use d1 mysql> CREATE TABLE t1 ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), nosql VARCHAR(30), price INT UNSIGNED, created_at DATETIME ); mysql> INSERT INTO t1 (name, nosql, price, created_at) VALUES ("kuwa_tw", "mongodb", 10, now()); mysql> INSERT INTO t1 (name, nosql, price, created_at) VALUES ("oranie", "cassandra", 100, now()); mysql> INSERT INTO t1 (name, nosql, price, created_at) VALUES ("kakky_h", "hbase", 1000, now()); mysql> INSERT INTO t1 (name, nosql, price, created_at) VALUES ("la_luna_azul", "riak", 1500, now()); mysql> INSERT INTO t1 (name, nosql, price, created_at) VALUES ("saeoshi", "voldemote", 2000, now());
mysql> SELECT * FROM t1; +----+--------------+-----------+-------+---------------------+ | id | name | nosql | price | created_at | +----+--------------+-----------+-------+---------------------+ | 1 | kuwa_tw | mongodb | 10 | 2014-12-23 21:14:39 | | 2 | oranie | cassandra | 100 | 2014-12-23 21:14:40 | | 3 | kakky_h | hbase | 1000 | 2014-12-23 21:14:41 | | 4 | la_luna_azul | riak | 1500 | 2014-12-23 21:14:42 | | 5 | saeoshi | voldemote | 2000 | 2014-12-23 21:14:43 | +----+--------------+-----------+-------+---------------------+ 5 rows in set (0.00 sec)
ã»ãã§ãã¾ããã
移è¡ãã¾ãããã
MySQLã®ãã¼ã¿ãCSVåºå
ã·ã§ã«ã¹ã¯ãªããã¨ããããã¾ãã¯CSVåºåã§ãã¾ããã
$ mysql -h localhost -u root -p pass d1 -e "SELECT * FROM t1" | tr "\t" "," >> ./dump.sql
å ¨ãã¼ãã«ããããã£ããforã§åããããããããããããªãã§ãããï¼
$ for t in `mysql -h localhost -u root d1 -e "show tables from mysql;" | awk 'NR!=1{ print $1}'` do mysql -h localhost -u root -p pass d1 -e "SELECT * FROM ${t}" | tr "\t" "," >> ./dump_${t}.sql done
ãããªã®ãåºã¾ããã
cat dump.csv id,name,nosql,price,created_at 1,kuwa_tw,mongodb,10,2014-12-23 21:14:39 2,oranie,cassandra,100,2014-12-23 21:14:40 3,kakky_h,hbase,1000,2014-12-23 21:14:41 4,la_luna_azul,riak,1500,2014-12-23 21:14:42 5,saeoshi,voldemote,2000,2014-12-23 21:14:43
CSVåºåãä¿®æ£
ããã§ããMongoDBé£ããããããã§ãããDateãStringã«ãªã£ã¡ãã£ã¦ãã®ã§å¤æãã¦ã¿ã¾ãããã
MongoDBのextended json formatのページãåç
§ã«ä¿®æ£ãã¦ã¿ã¾ãããï¼CSVã ãã©ï¼
$ awk -F, -v OFS=, 'NR!=1{tmp="ISODate(\""substr($5,1,10)"T"substr($5,12,9)"+0900""\")";$NF="";print $0tmp ; next} NR=1{print $0}' dump.csv > dump_fix.csv
id,name,nosql,price,created_at 1,kuwa_tw,mongodb,10,ISODate("2014-12-23T21:14:39+0900") 2,oranie,cassandra,100,ISODate("2014-12-23T21:14:40+0900") 3,kakky_h,hbase,1000,ISODate("2014-12-23T21:14:41+0900") 4,la_luna_azul,riak,1500,ISODate("2014-12-23T21:14:42+0900") 5,saeoshi,voldemote,2000,ISODate("2014-12-23T21:14:43+0900")
ãããªã£ããæåã§ãã
MongoDB ã¸ã®ã¤ã³ãã¼ã
æå¾ã¯mongoimportã³ãã³ãã§MongoDBã«ãããã ãï¼
$ mongoimport --host localhost --port 27018 -d d1 -c t1 --type csv --file dump_fix.csv --headerline --drop connected to: localhost:27018 2014-12-23T22:34:53.535+0900 dropping: d1.t1 2014-12-23T22:34:53.539+0900 imported 5 objects
- mongoimportã®ãªãã·ã§ã³ã®æå³
- --host æ¥ç¶å ãã¹ã
- --port æ¥ç¶å ãã¼ã
- -d database
- -c collection
- --file èªã¿è¾¼ããã¡ã¤ã«
- --type ãã¡ã¤ã«ã¿ã¤ã
- --hederline æåã®è¡ããããã¨ãã¦è¦ãï¼ãã£ã¼ã«ãåã«ãªãï¼
- --drop ã¤ã³ãã¼ãåã«ã³ã¬ã¯ã·ã§ã³ãDROPããï¼è¥å¹²å±éºï¼
å
¥ã£ãå
¥ã£ãï¼ã¯ã©ã©ãç«ã£ãï¼
ãããã¨è¨ããã¨ã§ãã¼ã¿ã¿ã¦ã¿ã¾ãããã
$ mongo --host localhost --port 27018 MongoDB shell version: 2.6.4 connecting to: localhost:27018/test > use d1 switched to db d1 > db.t1.find() { "_id" : ObjectId("54996816e0035c426fd71b2c"), "id" : 1, "name" : "kuwa_tw", "nosql" : "mongodb", "price" : 10, "created_at" : "2014-12-23T21:14:39+0900" } { "_id" : ObjectId("54996816e0035c426fd71b2d"), "id" : 2, "name" : "oranie", "nosql" : "cassandra", "price" : 100, "created_at" : "2014-12-23T21:14:40+0900" } { "_id" : ObjectId("54996816e0035c426fd71b2e"), "id" : 3, "name" : "kakky_h", "nosql" : "hbase", "price" : 1000, "created_at" : "2014-12-23T21:14:41+0900" } { "_id" : ObjectId("54996816e0035c426fd71b2f"), "id" : 4, "name" : "la_luna_azul", "nosql" : "riak", "price" : 1500, "created_at" : "2014-12-23T21:14:42+0900" } { "_id" : ObjectId("54996816e0035c426fd71b30"), "id" : 5, "name" : "saeoshi", "nosql" : "voldemote", "price" : 2000, "created_at" : "2014-12-23T21:14:43+0900" }
ã©ãã§ããããã¯ãã£ã¦ããã¾ããããã¯å¥è·¡ã
ã¨ãããã¨ã§ããã¾ãã¦ãMySQLããMongoDBã«ç§»è¡ãããããªãã¦ããçããæ¹ãããã¾ãããæ¯éãããªæãã§è©¦ãã¦ã¿ã¦ã¯ãããã§ããããã
ãã ã責任ã¯åãã¾ããããããï¼æªè«çãªçµããæ¹ï¼
追è¨
ã§ãç· ã¾ã£ããã¨æã£ããã§ãããããããã¾ãããããä¸è¨ã®æ¹æ³ã§ãISODateã§ã¯å ¥ã£ã¦ã¾ããã§ãããããã
å¤æããå ´åã¯mongo shellã«å ¥ã£ã¦ã
db.t1.find({ },{ "created_at": 1 }).forEach(function(document){ var newDate = ISODate( document.created_at ); db.t1.update( { _id: document._id }, { "$set": { "created_at": newDate } } ); })
ã¨ããæãã§ã§ããã¨æãã¾ããããã·ã§ã«ã¹ã¯ãªããã ãã§ã§ãã¦ãªãããªããªã®ããããããããããã
> db.t1.find() { "_id" : ObjectId("54998426e0035c426fd71b7b"), "id" : 1, "name" : "kuwa_tw", "nosql" : "mongodb", "price" : 10, "created_at" : ISODate("2014-12-23T12:14:39Z") } { "_id" : ObjectId("54998426e0035c426fd71b7c"), "id" : 2, "name" : "oranie", "nosql" : "cassandra", "price" : 100, "created_at" : ISODate("2014-12-23T21:14:40Z") } { "_id" : ObjectId("54998426e0035c426fd71b7d"), "id" : 3, "name" : "kakky_h", "nosql" : "hbase", "price" : 1000, "created_at" : ISODate("2014-12-23T21:14:41Z") } { "_id" : ObjectId("54998426e0035c426fd71b7e"), "id" : 4, "name" : "la_luna_azul", "nosql" : "riak", "price" : 1500, "created_at" : ISODate("2014-12-23T21:14:42Z") } { "_id" : ObjectId("54998426e0035c426fd71b7f"), "id" : 5, "name" : "saeoshi", "nosql" : "voldemote", "price" : 2000, "created_at" : ISODate("2014-12-23T21:14:43Z") }
ã¨ããããã§ãããã§OKã§ãm(_ _)m
ã¨ãã¹ãã¼ãã®ããã®MySQL[éç¨+管ç]ãã©ãã«ã·ã¥ã¼ãã£ã³ã°ã¬ã¤ã
- ä½è : 奥éå¹¹ä¹
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2010/06/12
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 16人 ã¯ãªãã¯: 204å
- ãã®ååãå«ãããã° (35件) ãè¦ã
稲å·æ·³äº çç¸ã»ææã®ç¾å ´ æ¬å½ã®ææããã¡ã¦ã¯ãããªãç·¨ [DVD]
- åºç社/ã¡ã¼ã«ã¼: ãããããã»ãã¼ã¨ã
- çºå£²æ¥: 2013/08/02
- ã¡ãã£ã¢: DVD
- ãã®ååãå«ãããã° (2件) ãè¦ã
MongoDBã¤ã³ã»ã¢ã¯ã·ã§ã³
- ä½è : Kyle Banker,Skyæ ªå¼ä¼ç¤¾çå·ç«å¸
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2012/12/14
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 5人 ã¯ãªãã¯: 55å
- ãã®ååãå«ãããã° (8件) ãè¦ã