Mysql到Mongodb的数据转换程序_MySQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Mysql到Mongodb的数据转换程序_MySQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2785字,纯文字阅读大概需要4分钟。
内容图文
![Mysql到Mongodb的数据转换程序_MySQL](/upload/InfoBanner/zyjiaocheng/580/26a8d55e23694a02967beeaa9cf512e0.jpg)
# -*- coding: utf-8 -*- import sys, os import multiprocessing import logging import random import time, datetime import MySQLdb from MySQLdb import cursors from pymongo import MongoClient class Config: tables = ['hs_card', 'hs_hero', 'hs_set', 'hs_skill', 'hs_level', 'hs_pack', 'hs_salesevent'] #tables = ['hs_card', 'hs_hero', 'hs_set', 'hs_skill', 'hs_level'] index = { 'hs_card': ['name'], } class Mysql2Mongo(object): mysql_host = '50.23.4.2' mysql_port = 3306 mysql_user = "root" mysql_pass = "stm123" mysql_db = "ccg_alpha" mongo_host = '50.23.4.2' mongo_port = 27017 conn = None cursor = None mongo = None mongodb = None def __init__(self, logger): self.logger = logger self.conn = self.getMysqlConn() self.cursor = self.conn.cursor() self.mongo = MongoClient(host=self.mongo_host, port=self.mongo_port) self.mongodb = self.mongo['ccg_alpha'] def getMysqlConn(self): return MySQLdb.connect(host=self.mysql_host, port=self.mysql_port, user=self.mysql_user, passwd=self.mysql_pass, db=self.mysql_db, cursorclass=MySQLdb.cursors.SSCursor) def setMongoCollectionDocument(self, table, data): if(isinstance(data, dict) == False): return False else: self.mongodb[table].insert(data) def getMysqlTableDesc(self, table): sql = """desc %s""" % (table) n = self.cursor.execute(sql) data = self.cursor.fetchall() keys = [] types = [] for row in data: key = str(row[0]) if(row[1].find('int') >= 0): type = 1 elif (row[1].find('char') >= 0): type = 2 elif (row[1].find('text') >= 0): type = 2 elif(row[1].find('decimal') >= 0): type = 3 else: type = 2 keys.append(key) types.append(type) return keys, types def mysql2Mongo(self, table): self.mongodb[table].drop() keys, types = self.getMysqlTableDesc(table) sql = """select * from %s order by id asc""" % (table) n = self.cursor.execute(sql) data = self.cursor.fetchall() #print table, keys, types for row in data: ret = {} for k, key in enumerate(keys): if key == 'id': key = '_id' #ret[key] = int(row[k]) if(types[k] == 1): if row[k]==None: ret[key]= 0 continue #print k, key, row ret[key] = int(row[k]) elif(types[k] == 2): if row[k]==None: ret[key]= '' continue ret[key] = str(row[k]) elif(types[k] == 3): if row[k]==None: ret[key]= '' continue ret[key] = float(row[k]) else: if row[k]==None: ret[key]= '' continue ret[key] = str(row[k]) #if(table== 'hs_card') or (table== 'hs_hero'): #ret['rand'] = random.random() print ret self.setMongoCollectionDocument(table, ret) def __del__(self): self.mongo.close() self.cursor.close() self.conn.close() if __name__ == '__main__': multiprocessing.log_to_stderr() logger = multiprocessing.get_logger() logger.setLevel(logging.INFO) # args = sys.argv t1 = time.time() cls = Mysql2Mongo(logger) for tb in Config.tables: cls.mysql2Mongo(tb) #index for t, f in Config.index.items(): pass print time.time() - t1 logger.info("done")
内容总结
以上是互联网集市为您收集整理的Mysql到Mongodb的数据转换程序_MySQL全部内容,希望文章能够帮你解决Mysql到Mongodb的数据转换程序_MySQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。