Mysql數據導入到MongoDB (1)從mysql導出數據位csv文件,如: Java代碼 SELECT * FROM APP_CALL_LOG_1362959187037 WHERE CREATE_TIME < '2013-03-07 14:45:04' into outfile '/tmp/all_call_log_012.csv' fields terminated by ',' lines terminated by '\n'; (2)導入csv文件到mongodb,如: Java代碼 bin/mongoimport --host 114.112.94.104 --port 27017 -d lb_v1_log -c APP_CALL_LOG --type csv --ignoreBlanks -f ID,IP,URI,URL,D_CODE,D_TYPE,UID,APP_FROM,CREATE _TIME,USER_AGENT,REMARK -file /usr/local/all_call_log_012.csv (3)對導入數據矯正處理,如: Java代碼 pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({UID:"\\N"}, { $set: { 'UID': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({IP:"\\N"}, { $set: { 'IP': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({D_TYPE:"\\N"}, { $set: { 'D_TYPE': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({D_CODE:"\\N"}, { $set: { 'D_CODE': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({CREATE_TIME:"\\N"}, { $set: { 'CREATE_TIME': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({APP_FROM:"\\N"}, { $set: { 'APP_FROM': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({URI:"\\N"}, { $set: { 'URI': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({URL:"\\N"}, { $set: { 'URL': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({USER_AGENT:"\\N"}, { $set: { 'USER_AGENT': null} },false,true); pmy_log_rs:PRIMARY> db.APP_CALL_LOG.update({REMARK:"\\N"}, { $set: { 'REMARK': null} },false,true); (4)字段類型矯正,如: Java代碼 db.APP_CALL_LOG.find({}).forEach( function (x) {x.D_CODE = ""+x.D_CODE;db.APP_CALL_LOG.save(x);});