Home > Database > Mysql Tutorial > body text

How to push data to Mongo in batches using MySQL

WBOY
Release: 2023-05-31 17:28:13
forward
1477 people have browsed it

import pymongo 
import mysql.connector
Copy after login

Connect to MySQL database

mysql_conn = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" )
Copy after login

Connect to MongoDB database

mongo_client = pymongo.MongoClient("mongodb://localhost:27017/") mongo_db = mongo_client["mydatabase"] mongo_collection = mongo_db["mycollection"]
Copy after login

Create an index for the specified collection in MongoDB

mongo_collection.create_index([("myfield", pymongo.ASCENDING)])
Copy after login

Buffering lists and counters to batch after every 10000 rows

bulk_data = [] bulk_count = 0
Copy after login

Create cursor object and retrieve data from MySQL database

mysql_cursor = mysql_conn.cursor() mysql_cursor.execute("SELECT * FROM mytable")
Copy after login

Loop through the result set and process each row.

for row in mysql_cursor: # 将一条记录转换成你的MongoDB文档,然后将其添加到缓冲列表。 
doc = { "myfield": row[0], "anotherfield": row[1], "yetanotherfield": row[2] } bulk_data.append(doc) bulk_count += 1
Copy after login
# 如果我们达到了10000,请在集合中批量插入缓冲数据。
if bulk_count == 10000:
    mongo_collection.insert_many(bulk_data)
    # 重置计数器并清除缓冲数据列表
    bulk_count = 0
    bulk_data.clear()
Copy after login

Process the remaining rows, if anything needs to be processed.

if bulk_count > 0: mongo_collection.insert_many(bulk_data)
Copy after login

Close the MySQL connection.

mysql_conn.close()

import pymongo
import mysql.connector
# 连接MySQL数据库
mysql_conn = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="yourdatabase"
)
# 连接MongoDB数据库
mongo_client = pymongo.MongoClient("mongodb://localhost:27017/")
mongo_db = mongo_client["mydatabase"]
mongo_collection = mongo_db["mycollection"]
# 在MongoDB中为指定集合创建索引
mongo_collection.create_index([("myfield", pymongo.ASCENDING)])
# 缓冲列表和计数器以在每10000行处理后进行批处理
bulk_data = []
bulk_count = 0
# 创建游标对象并从MySQL数据库检索数据
mysql_cursor = mysql_conn.cursor()
mysql_cursor.execute("SELECT * FROM mytable")
# 遍历结果集并处理每个行。
for row in mysql_cursor:
    # 将一条记录转换成你的MongoDB文档,然后将其添加到缓冲列表。
    doc = {
        "myfield": row[0],
        "anotherfield": row[1],
        "yetanotherfield": row[2]
    }
    bulk_data.append(doc)
    bulk_count += 1
    # 如果我们达到了10000,请在集合中批量插入缓冲数据。
    if bulk_count == 10000:
        mongo_collection.insert_many(bulk_data)
        # 重置计数器并清除缓冲数据列表
        bulk_count = 0
        bulk_data.clear()
# 处理剩余的行,如果有任何事情需要处理。
if bulk_count > 0:
    mongo_collection.insert_many(bulk_data)
# 关闭MySQL连接。
mysql_conn.close()
Copy after login

The above is the detailed content of How to push data to Mongo in batches using MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template