最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中MySQL數(shù)據(jù)遷移到MongoDB腳本的方法

 更新時間:2016年04月28日 16:52:39   作者:韓德田  
MongoDB 是一個介于關系數(shù)據(jù)庫和非關系數(shù)據(jù)庫之間的產(chǎn)品,是非關系數(shù)據(jù)庫當中功能最豐富,最像關系數(shù)據(jù)庫的。本文給大家介紹Python中MySQL數(shù)據(jù)遷移到MongoDB腳本的方法,需要的朋友參考下

MongoDB簡介

MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數(shù)據(jù)存儲解決方案。

MongoDB 是一個介于關系數(shù)據(jù)庫和非關系數(shù)據(jù)庫之間的產(chǎn)品,是非關系數(shù)據(jù)庫當中功能最豐富,最像關系數(shù)據(jù)庫的。

MongoDB是一個文檔數(shù)據(jù)庫,在存儲小文件方面存在天然優(yōu)勢。隨著業(yè)務求的變化,需要將線上MySQL數(shù)據(jù)庫中的行記錄,導入到MongoDB中文檔記錄。

一、場景:線上MySQL數(shù)據(jù)庫某表遷移到MongoDB,字段無變化。

二、Python模塊:

使用Python的torndb,pymongo和time模塊。

*注釋:首先安裝setup.py,pip,MySQLdb

執(zhí)行如下命令即可:

pip install torndb
pip install pymongo

三、腳本內容如下:

[root ~]#cat nmytomongo.py

#!/usr/bin/env python#fielName: mytomongo.py#Author:xkops#coding: utf-8import torndb,pymongo,time# connect to mysql databasemysql = torndb.Connection(host='127.0.0.1', database='database', user='username', password='password')#connect to mongodb and obtain total lines in mysqlmongo = pymongo.MongoClient('mongodb://ip').databasemongo.authenticate('username',password='password')countlines = mysql.query('SELECT max(table_field) FROM table_name')count = countlines[0]['max(table_field)']#count = 300print counti = 0 j = 100start_time = time.time()#select from mysql to insert mongodb by 100 lines.for i in range(0,count,100): #print a,b #print i #print 'SELECT * FROM quiz_submission where quiz_submission_id > %d and quiz_submission_id <= %d' %(i,j) submission = mysql.query('SELECT * FROM table_name where table_field > %d and table_field <= %d' %(i,j)) #print submission if submission: #collection_name like mysql table_name mongo.collection_name.insert_many(submission) else: i +=100 j +=100 continue i +=100 j +=100end_time = time.time()deltatime = end_time - start_timetotalhour = int(deltatime / 3600)totalminute = int((deltatime - totalhour * 3600) / 60)totalsecond = int(deltatime - totalhour * 3600 - totalminute * 60)#print migrate data total time consuming.print "Data Migrate Finished,Total Time Consuming: %d Hour %d Minute %d Seconds" %(totalhour,totalminute,totalsecond)

*注釋:按照自己的需求更改上述代碼中的數(shù)據(jù)庫地址,用戶,密碼,庫名,表名以及字段名等。

四、執(zhí)行遷移腳本:

[root ~]#python nmytomongo.py &> /tmp/migratelog.txt &

腳本執(zhí)行完成后查看/tmp/migratelog.txt數(shù)據(jù)遷移消耗的時間。

相關文章

最新評論

宜春市| 驻马店市| 元朗区| 宕昌县| 德钦县| 盐津县| 汕头市| 辽中县| 龙南县| 罗江县| 温州市| 滦平县| 河曲县| 万年县| 海兴县| 施秉县| 五寨县| 保康县| 安丘市| 诸暨市| 吉林省| 大同县| 黄龙县| 余庆县| 巴林右旗| 铁岭市| 西贡区| 会东县| 黎川县| 普格县| 二连浩特市| 新野县| 定远县| 精河县| 井冈山市| 陆川县| 五大连池市| 临泉县| 铜鼓县| 惠来县| 保定市|