-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_db.py
102 lines (77 loc) · 2.29 KB
/
convert_db.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/local/bin/python3
# coding: utf-8
# BagAndDrag - convert_db.py
# 1/12/21 18:24
#
__author__ = "Benny <[email protected]>"
# convert to mongodb and con_sqlite
import sqlite3
import pymongo
import pymysql
import tqdm
import json
from typing import List
con_mysql = pymysql.Connect(host="127.0.0.1", user="root", password="root", charset="utf8mb4", database="yyets",
cursorclass=pymysql.cursors.DictCursor
)
mongo_client = pymongo.MongoClient()
con_sqlite = sqlite3.connect("yyets.db")
SIZE = 2000
def create_sqlite_database():
sql = ["""
DROP TABLE IF EXISTS resource;
""",
"""
create table resource
(
id int primary key,
url varchar(255) null unique ,
name text null,
expire int null,
expire_cst varchar(255) null,
data longtext null
)
"""
]
cur = con_sqlite.cursor()
for s in sql:
cur.execute(s)
con_sqlite.commit()
def clear_mongodb():
mongo_client.drop_database("yyets")
def sqlite_insert(data: List[dict]):
cur = con_sqlite.cursor()
sql = "INSERT INTO resource VALUES(?,?,?,?,?,?)"
cur.executemany(sql, [list(i.values()) for i in data])
con_sqlite.commit()
def mongodb_insert(data: List[dict]):
db = mongo_client["yyets"]
col = db["resource"]
# deserialize data.data
inserted = []
for i in data:
i["data"] = json.loads(i["data"])
inserted.append(i)
col.insert_many(inserted)
def main():
create_sqlite_database()
clear_mongodb()
mysql_cur = con_mysql.cursor()
mysql_cur.execute("select count(id) from resource")
count = mysql_cur.fetchall()[0]["count(id)"]
mysql_cur.execute("SELECT * FROM resource")
with tqdm.tqdm(total=count * 2) as pbar:
while True:
data = mysql_cur.fetchmany(SIZE)
if data:
sqlite_insert(data)
pbar.update(SIZE)
mongodb_insert(data)
pbar.update(SIZE)
else:
break
if __name__ == '__main__':
main()
con_mysql.close()
con_sqlite.close()
mongo_client.close()