python mongo2mysql.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python mongo2mysql.py相关的知识,希望对你有一定的参考价值。

###########################
# Script for converting   #
# mongoDB to mySql        #
# Coded by Atul Yadav     #
###########################

#!/usr/bin/python

from pymongo import MongoClient
import MySQLdb
import unicodedata

#################################
#     mongoDb Server Details    #
#################################

mongoServer = raw_input("Enter MongoDb Server Address = ") # address of mongo server
mongoPort =  int(raw_input("Enter MongoDb Server Port Number = ")) # mongo port nubmer
mongoDatabase = raw_input("Enter MongoDb Database Name = ") # mongo database name
mongoCollectionName= mongoDatabase = raw_input("Enter MongoDb Database Collection Name = ") # mongo collection name

#################################
#      mySql Server Details     #
#################################

mySqlServer = raw_input("Enter mySql Server Address = ") # address of mySql Server
mySqlUser = raw_input("Enter mySql User Name = ") # mySql User name
mySqlPassword = raw_input("Enter mySql User Password = ") # mySql User password
mySqlDatabase = raw_input("Enter mySql Database Name = ") # name of mySql Database
mySqlTable = raw_input("Enter mySql Database Table Name = ") #name of mySql Table

#################################
#      Connection to mongo      #
#################################

print 'Trying to connect to MongoDB Server.... Please Wait...'
try:
    mongoConnection = MongoClient(mongoServer,mongoPort)
    mongoDB = mongoConnection[mongoDatabase]
    mongoCollection = mongoDB[mongoCollectionName]
    if mongoConnection.server_info():
        print 'Establishing connection to mongoDB successful :)'
    else:
        pass
except:
    print 'Error Connecting MongoDB. Please Check Connection Settings.'

#################################
#      Connection to mySql      #
#################################
print 'Trying to connect to mySql Server.... Please Wait...'
try:
    sqlConnection = MySQLdb.connect(mySqlServer,mySqlUser,mySqlPassword,mySqlDatabase)
    sqlCursor = sqlConnection.cursor()
    if sqlConnection.open:
        print 'Establishing connection to mySql Server successful :)'
    else:
        pass
except:
    print 'Error Connecting mySql Server. Please Check Connection Settings.'

#################################
#           Data Dumping        #
#################################

print 'Dumping Mongo Data to Sql .....'
try:
    document = mongoCollection.find()
    keyList = []
    valueList = []
    for details in document:
        del details['_id']
        cols = []
        values = []
        for key, value in details.items():
            key = key.encode('ascii')
            if isinstance(value,unicode):
                value = value.encode('ascii')
            cols.append(key)
            values.append(value)
        q = '''insert into profile ({}) VALUES {}''' .format(','.join(cols), tuple(values))
        print q
        sqlCursor.execute(q)
        sqlConnection.commit()
        keyList = []
        valueList = []

    print 'Done'
    sqlConnection.close()

except:
    print 'Error Dumping Data'

#################################

print '#### Thanks for Using ####'




以上是关于python mongo2mysql.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python