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

Python實(shí)現(xiàn)將MongoDB中的數(shù)據(jù)導(dǎo)入到MySQL

 更新時(shí)間:2023年05月04日 14:11:31   作者:小小鳥(niǎo)愛(ài)吃辣條  
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python封裝一個(gè)將?MongoDB?中的數(shù)據(jù)導(dǎo)入到?MySQL?中的?Python?工具類(lèi)?MongoToMysql,感興趣的可以了解一下

本文主要介紹了一個(gè)將 MongoDB 中的數(shù)據(jù)導(dǎo)入到 MySQL 中的 Python 工具類(lèi) MongoToMysql。該工具類(lèi)實(shí)現(xiàn)了獲取 MongoDB 數(shù)據(jù)類(lèi)型、創(chuàng)建 MySQL 表結(jié)構(gòu)以及將數(shù)據(jù)從 MongoDB 推送到 MySQL 等功能。

通過(guò)該工具類(lèi),用戶(hù)可以輕松地將 MongoDB 中的數(shù)據(jù)導(dǎo)入到 MySQL 中,實(shí)現(xiàn)數(shù)據(jù)的轉(zhuǎn)移和使用。

使用該工具類(lèi),用戶(hù)需要傳入相應(yīng)的參數(shù),包括 MongoDB 的連接信息,MySQL 的連接信息,以及表名、是否設(shè)置最大長(zhǎng)度、批處理大小和表描述等信息。具體使用可以參考代碼中的注釋。

實(shí)現(xiàn)代碼

import pymysql
from loguru import logger

class MongoToMysql:
    def __init__(self, mongo_host, mongo_port, mongo_db, mongo_collection, mysql_host, mysql_port, mysql_user,
                 mysql_password, mysql_db,table_name=None,set_max_length=False,batch_size=10000,table_description=''):
        self.mongo_host = mongo_host
        self.mongo_port = mongo_port
        self.mongo_db = mongo_db
        self.mongo_collection = mongo_collection
        self.mysql_host = mysql_host
        self.mysql_port = mysql_port
        self.mysql_user = mysql_user
        self.mysql_password = mysql_password
        self.mysql_db = mysql_db
        self.table_name = table_name
        self.set_max_length = set_max_length
        self.batch_size = batch_size
        self.table_description = table_description
        self.data_types = self.get_mongo_data_types()
        self.create_mysql_table(self.data_types,set_max_length= self.set_max_length,table_description=self.table_description)
        self.push_data_to_mysql(self.batch_size)

    def get_mongo_data_types(self):
        logger.debug('正在獲取mongo中字段的類(lèi)型!')
        client = pymongo.MongoClient(host=self.mongo_host, port=self.mongo_port)
        db = client[self.mongo_db]
        collection = db[self.mongo_collection]
        data_types = {}
        for field in collection.find_one().keys():
            data_types[field] = type(collection.find_one()[field]).__name__
        return data_types

    def check_mysql_table_exists(self):
        logger.debug('檢查是否存在該表,有則刪之!')
        conn = pymysql.connect(host=self.mysql_host, port=self.mysql_port, user=self.mysql_user,
                               password=self.mysql_password, db=self.mysql_db)
        cursor = conn.cursor()
        sql = f"DROP TABLE IF EXISTS {self.mongo_collection}"
        cursor.execute(sql)
        conn.commit()
        conn.close()

    def get_max_length(self, field):
        logger.debug(f'正在獲取字段 {field} 最大長(zhǎng)度......')
        client = pymongo.MongoClient(host=self.mongo_host, port=self.mongo_port)
        db = client[self.mongo_db]
        collection = db[self.mongo_collection]
        max_length = 0
        for item in collection.find({},{field:1,'_id':0}):
            value = item.get(field)
            if isinstance(value, str) and len(value) > max_length:
                max_length = len(value)
        return max_length

    def create_mysql_table(self, data_types,set_max_length,table_description):
        logger.debug('正在mysql中創(chuàng)建表結(jié)構(gòu)!')
        self.check_mysql_table_exists()
        conn = pymysql.connect(host=self.mysql_host, port=self.mysql_port, user=self.mysql_user,
                               password=self.mysql_password, db=self.mysql_db)
        cursor = conn.cursor()
        if self.table_name:
            table_name = self.table_name
        else:
            table_name = self.mongo_collection
        fields = []
        for field, data_type in data_types.items():
            if data_type == 'int':
                fields.append(f"{field} INT")
            elif data_type == 'float':
                fields.append(f"{field} FLOAT")
            elif data_type == 'bool':
                fields.append(f"{field} BOOLEAN")
            else:
                if set_max_length:
                    fields.append(f"{field} TEXT)")
                else:
                    max_length = self.get_max_length(field)
                    fields.append(f"{field} VARCHAR({max_length + 200})")
        fields_str = ','.join(fields)
        sql = f"CREATE TABLE {table_name} (id INT PRIMARY KEY AUTO_INCREMENT,{fields_str}) COMMENT='{table_description}'"
        cursor.execute(sql)
        conn.commit()
        conn.close()


    def push_data_to_mysql(self, batch_size=10000):
        logger.debug('--- 正在準(zhǔn)備從mongo中每次推送10000條數(shù)據(jù)到mysql ----')
        client = pymongo.MongoClient(host=self.mongo_host, port=self.mongo_port)
        db = client[self.mongo_db]
        collection = db[self.mongo_collection]
        conn = pymysql.connect(host=self.mysql_host, port=self.mysql_port, user=self.mysql_user,
                               password=self.mysql_password, db=self.mysql_db)
        cursor = conn.cursor()
        if self.table_name:
            table_name = self.table_name
        else:
            table_name = self.mongo_collection
        # table_name = self.mongo_collection
        data = []
        count = 0
        for item in collection.find():
            count += 1
            row = []
            for field, data_type in self.data_types.items():
                value = item.get(field)
                if value is None:
                    row.append(None)
                elif data_type == 'int':
                    row.append(str(item.get(field, 0)))
                elif data_type == 'float':
                    row.append(str(item.get(field, 0.0)))
                elif data_type == 'bool':
                    row.append(str(item.get(field, False)))
                else:
                    row.append(str(item.get(field, '')))
            data.append(row)
            if count % batch_size == 0:
                placeholders = ','.join(['%s'] * len(data[0]))
                sql = f"INSERT INTO {table_name} VALUES (NULL,{placeholders})"
                cursor.executemany(sql, data)
                conn.commit()
                data = []
                logger.debug(f'--- 已完成推送:{count} 條數(shù)據(jù)! ----')
        if data:
            placeholders = ','.join(['%s'] * len(data[0]))
            sql = f"INSERT INTO {table_name} VALUES (NULL,{placeholders})"
            cursor.executemany(sql, data)
            conn.commit()
            logger.debug(f'--- 已完成推送:{count} 條數(shù)據(jù)! ----')
        conn.close()


if __name__ == '__main__':
    """MySQL"""
    mongo_host = '127.0.0.1'
    mongo_port = 27017
    mongo_db = 'db_name'
    mongo_collection = 'collection_name'

    """MongoDB"""
    mysql_host = '127.0.0.1'
    mysql_port = 3306
    mysql_user = 'root'
    mysql_password = '123456'
    mysql_db = 'mysql_db'

    table_description = ''  # 表描述

    mongo_to_mysql = MongoToMysql(mongo_host, mongo_port, mongo_db, mongo_collection, mysql_host, mysql_port,
                                  mysql_user, mysql_password, mysql_db,table_description=table_description)


    #
    # table_name = None  # 默認(rèn)為None 則MySQL中的表名跟Mongo保持一致
    # set_max_length = False # 默認(rèn)為False 根據(jù)mongo中字段最大長(zhǎng)度 加200 來(lái)設(shè)置字段的VARCHART長(zhǎng)度 , 否則定義TEXT類(lèi)型
    # batch_size = 10000   # 控制每次插入數(shù)據(jù)量的大小
    # table_description = '' # 表描述
    # mongo_to_mysql = MongoToMysql(mongo_host, mongo_port, mongo_db, mongo_collection, mysql_host, mysql_port,
    #                               mysql_user, mysql_password, mysql_db,table_name,set_max_length,batch_size,table_description)

代碼使用了 PyMongo、PyMySQL 和 Loguru 等 Python 庫(kù),并封裝了一個(gè) MongoToMysql 類(lèi)。在類(lèi)的初始化時(shí),會(huì)自動(dòng)獲取 MongoDB 中字段的類(lèi)型,并根據(jù)字段類(lèi)型創(chuàng)建 MySQL 表結(jié)構(gòu)。在將數(shù)據(jù)從 MongoDB 推送到 MySQL 時(shí),還可以控制每次插入數(shù)據(jù)量的大小,以避免一次性插入大量數(shù)據(jù)造成系統(tǒng)崩潰或性能下降。

需要注意的是,在創(chuàng)建 MySQL 表結(jié)構(gòu)時(shí),如果用戶(hù)選擇了設(shè)置最大長(zhǎng)度,則會(huì)創(chuàng)建 TEXT 類(lèi)型的字段,否則會(huì)根據(jù) MongoDB 中字段的最大長(zhǎng)度加上200來(lái)設(shè)置 VARCHAR 類(lèi)型的字段長(zhǎng)度。

總之,本文介紹的 MongoToMysql 工具類(lèi)非常方便實(shí)用,對(duì)于需要將 MongoDB 數(shù)據(jù)遷移到 MySQL 的用戶(hù)來(lái)說(shuō),是一種很好的解決方案。

到此這篇關(guān)于Python實(shí)現(xiàn)將MongoDB中的數(shù)據(jù)導(dǎo)入到MySQL的文章就介紹到這了,更多相關(guān)Python MongoDB數(shù)據(jù)導(dǎo)入到MySQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 爬取微信文章

    python 爬取微信文章

    本文給大家分享的是使用python通過(guò)搜狗入口,爬取微信文章的小程序,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下
    2016-01-01
  • python實(shí)現(xiàn)多層感知器MLP(基于雙月數(shù)據(jù)集)

    python實(shí)現(xiàn)多層感知器MLP(基于雙月數(shù)據(jù)集)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多層感知器MLP,基于雙月數(shù)據(jù)集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例

    python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例

    下面小編就為大家?guī)?lái)一篇python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Python2.x中文亂碼問(wèn)題解決方法

    Python2.x中文亂碼問(wèn)題解決方法

    這篇文章主要介紹了Python2.x中文亂碼問(wèn)題解決方法,本文解釋問(wèn)題原因、給出了處理辦法并講解了編碼解碼的一些知識(shí),需要的朋友可以參考下
    2015-06-06
  • python opencv實(shí)現(xiàn)gif圖片分解的示例代碼

    python opencv實(shí)現(xiàn)gif圖片分解的示例代碼

    這篇文章主要介紹了python opencv實(shí)現(xiàn)gif圖片分解的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Python編寫(xiě)車(chē)票訂購(gòu)系統(tǒng)?Python實(shí)現(xiàn)快遞收費(fèi)系統(tǒng)

    Python編寫(xiě)車(chē)票訂購(gòu)系統(tǒng)?Python實(shí)現(xiàn)快遞收費(fèi)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Python編寫(xiě)車(chē)票訂購(gòu)系統(tǒng),Python實(shí)現(xiàn)快遞收費(fèi)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • python?openpyxl操作Excel的安裝使用

    python?openpyxl操作Excel的安裝使用

    這篇文章主要為大家介紹了python?openpyxl操作Excel的安裝使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • opencv 獲取rtsp流媒體視頻的實(shí)現(xiàn)方法

    opencv 獲取rtsp流媒體視頻的實(shí)現(xiàn)方法

    這篇文章主要介紹了opencv 獲取rtsp流媒體視頻的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python對(duì)象銷(xiāo)毀實(shí)例(垃圾回收)

    python對(duì)象銷(xiāo)毀實(shí)例(垃圾回收)

    今天小編就為大家分享一篇python對(duì)象銷(xiāo)毀實(shí)例(垃圾回收),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • flask框架配置mysql數(shù)據(jù)庫(kù)操作詳解

    flask框架配置mysql數(shù)據(jù)庫(kù)操作詳解

    這篇文章主要介紹了flask框架配置mysql數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式詳細(xì)分析了flask框架配置mysql數(shù)據(jù)庫(kù)及連接訪問(wèn)等相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11

最新評(píng)論

皮山县| 拜泉县| 富锦市| 綦江县| 兰溪市| 宜春市| 长沙县| 杭州市| 和龙市| 辽宁省| 五寨县| 阜新| 南通市| 香港| 通化市| 讷河市| 巴楚县| 西华县| 北京市| 大名县| 南华县| 汶川县| 通州市| 河池市| 上林县| 新建县| 雷山县| 宝清县| 邵东县| 酒泉市| 集贤县| 山东省| 湛江市| 通州区| 济宁市| 朝阳县| 蓬安县| 香格里拉县| 泰和县| 涟源市| 什邡市|