将数据刷入zinc

This commit is contained in:
2023-12-08 19:23:56 +08:00
parent f3a5d44c57
commit c1257c6d29

59
main.py Normal file
View File

@@ -0,0 +1,59 @@
import json
import base64
import pymysql
import requests
import dotenv
CONFIG = dotenv.dotenv_values(".env")
user = "admin"
password = "Complexpass#123"
bas64encoded_creds = base64.b64encode(bytes(f"{user}:{password}", "utf-8")).decode("utf-8")
headers = {"Content-type": "application/json", "Authorization": f"Basic {bas64encoded_creds}"}
index = "images"
zinc_host = "https://zincsearch.gameui.net"
zinc_url = f"{zinc_host}/api/{index}/_doc"
# 将数据刷入zinc, 并保持同步更新
# 如果SQL中某一条数据被删除, 那么zinc中也要删除
def connect_to_mysql():
return pymysql.connect(host=CONFIG['MYSQL_HOST'], user=CONFIG['MYSQL_USER'], password=CONFIG['MYSQL_PASSWORD'], database=CONFIG['MYSQL_NAME'], cursorclass=pymysql.cursors.SSDictCursor)
# 查询已存在的数据写入zinc LIMIT 0,10
def query_data(conn):
with conn.cursor(pymysql.cursors.SSCursor) as cursor:
cursor.execute("SELECT id, text FROM web_images WHERE text!='' AND text!='[]'")
for id, text in cursor:
data = { "_id": str(id), "text": " ".join([item['text'] for item in json.loads(text)]) }
res = requests.put(zinc_url, headers=headers, data=json.dumps(data), proxies={'http': '', 'https': ''})
print("\033[1;32m{}\033[0m".format(id) if json.loads(res.text)['message'] == 'ok' else id, data['text'])
query_data(connect_to_mysql())
# TODO 数据被删除时, zinc中也要删除
# TODO 可以监听SQL日志, 一旦有数据变动, 就更新zinc
# TODO 为数据之间建立事件关联, 当删除一条图像数据时, 也要删除对应的图像
## 查询数据
#query = {
# "query": {
# "bool": {
# "must": [
# {
# "query_string": {
# "query": "City:是否"
# }
# }
# ]
# }
# },
# "sort": [
# "-@timestamp"
# ],
# "from": 0,
# "size": 100
#}
#zinc_url = zinc_host + "/es/" + index + "/_search"
#res = requests.post(zinc_url, headers=headers, data=json.dumps(query), proxies={'http': '', 'https': ''})
#print(json.dumps(json.loads(res.text), indent=4, ensure_ascii=False))