-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcfkv.py
87 lines (72 loc) · 2.23 KB
/
cfkv.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
#!/usr/local/bin/python3
# coding: utf-8
# BagAndDrag - cfkv.py
# 1/17/21 12:08
#
__author__ = "Benny <[email protected]>"
import pymysql
import json
import os
con = pymysql.Connect(host="127.0.0.1", user="root", password="root", charset="utf8mb4", database="yyets",
cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()
SIZE = 3000
cur.execute("select count(id) from resource")
count = cur.fetchall()[0]["count(id)"]
LIMIT = count // SIZE + 1
def convert_kv():
for i in range(1, LIMIT + 1):
SQL = "select id,data from resource limit %d offset %d" % (SIZE, (i - 1) * SIZE)
print(SQL)
cur = con.cursor()
cur.execute(SQL)
data = cur.fetchall()
write_data = []
for datum in data:
write_data.append({
"key": str(datum["id"]), # keys need to be str
"value": datum['data']}
)
with open(f"kv/kv_data{i - 1}.json", "w") as f:
json.dump(write_data, f, ensure_ascii=False)
def verify_kv_data():
files = os.listdir("kv")
rows = 0
for file in files:
if file.startswith("kv_data"):
with open(f"kv/{file}") as f:
data = json.load(f)
rows += len(data)
print(rows, count)
# assert rows == count
def dump_index():
cur = con.cursor()
indexes = {}
cur.execute("select name, id from resource")
data = cur.fetchall()
for datum in data:
name = datum["name"]
rid = datum["id"]
indexes[name] = rid
with open("kv/index.json", "w") as f:
write_data = [
{
"key": "index",
"value": json.dumps(indexes, ensure_ascii=False)
}
]
json.dump(write_data, f, ensure_ascii=False, indent=2)
def generate_command():
files = os.listdir("kv")
tpl = "wrangler kv:bulk put --namespace-id=01d666b5ebae464193998bb074f672cf {filename}"
shell = []
for file in files:
if file.endswith(".json"):
shell.append(tpl.format(filename=file) + "\n")
with open("kv/bulk.sh", "w") as f:
f.writelines(shell)
if __name__ == '__main__':
convert_kv()
verify_kv_data()
dump_index()
generate_command()