-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_library_mysql2mongo.py
68 lines (61 loc) · 1.89 KB
/
text_library_mysql2mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""
@File : text_library_mysql2mongo.py
@Author : Yuka
@Time : 2021/1/7 14:53
@Version : 1.0.0
@Description:
"""
import json
import mysql.connector.pooling
import pymongo
DBS = {
"host": "rm-uf6x8t115nb94hrak9o.mysql.rds.aliyuncs.com",
# "host": "49.74.204.242",
"port": 3306,
"user": "ringdata_web",
# "user": "root",
# "password": "monetware",
"password": "ringdata2019@sh",
"database": "ringanalyzer",
"charset": "utf8"
}
pool = mysql.connector.pooling.MySQLConnectionPool(**DBS, pool_size=10, pool_name="pool")
connection = pool.get_connection()
cursor = connection.cursor(dictionary=True, bufferd=True)
cursor.execute('SHOW TABLES')
import re
# tables = [name["Tables_in_ringanalyzer"]
# for name in cursor.fetchall()
# if re.match("rs_textlibrary_data_(\d+)$", name["Tables_in_ringanalyzer"])]
tables = ["rs_analysis_define_library"]
client = pymongo.MongoClient(
"mongodb://monetware:monetware2020%[email protected]:20000/admin?connectTimeoutMS=10000&authSource=admin")
db = client["ring_service_v2"]
collection = db["rs_seg_config"]
for name in tables:
# project_id = re.findall("rs_textlibrary_data_(\d+)$", name)[0]
sql = "select * from " + name
print(name)
cursor.execute(sql)
buffer = []
count = 0
for record in cursor:
if record["is_delete"] == 1:
record["is_delete"] = False
else:
record["is_delete"] = True
count += 1
buffer.append(record)
if len(buffer) >= 2000:
collection.insert_many(buffer)
buffer.clear()
print("insert 2000...")
if count < 1:
continue
if len(buffer) > 0:
collection.insert_many(buffer)
print("insert {}...".format(len(buffer)))
buffer.clear()
print("{}:迁移了{}条数据".format(name, count))
client.close()