import_to_gn_tree_elastic
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
import mariadb
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import datetime
|
||||
import json
|
||||
import uuid
|
||||
|
||||
import sys
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
|
||||
from app.common.elastic_helper import ElasticHelper
|
||||
from app.common.general_functions import *
|
||||
from app.common.text_normalizer import TextNormalizer
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def import_to_gn_tree_elastic(cursor, elastic, project_id, project_title, index_name_o) :
|
||||
|
||||
query = f"""
|
||||
SELECT id, title, parent, child_order, meta, tags from tlist.tl_subjects
|
||||
where project_id={project_id}
|
||||
ORDER BY parent, child_order
|
||||
"""
|
||||
# print('-'*10)
|
||||
# print(query)
|
||||
# print('-'*10)
|
||||
cursor.execute(query)
|
||||
|
||||
parent_nodes = {}
|
||||
bulk_data=[]
|
||||
total_count = 0
|
||||
|
||||
t1 = datetime.datetime.now()
|
||||
print(t1)
|
||||
i = 0
|
||||
while 1:
|
||||
# print(i)
|
||||
i = i + 1
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
|
||||
id = row[0]
|
||||
# eid = uuid.uuid4().hex[:8]
|
||||
eid = 'gnt'+str(id).zfill(5)
|
||||
title = row[1].strip()
|
||||
parent = row[2]
|
||||
parent_id = 0
|
||||
if parent != 0 :
|
||||
parent_id = 'gnt'+str(parent).zfill(5)
|
||||
child_order = row[3]
|
||||
meta = row[4]
|
||||
tags = row[5]
|
||||
|
||||
|
||||
meta_info = {}
|
||||
if meta :
|
||||
meta_info["id1"] = str(meta)
|
||||
|
||||
tags_list = []
|
||||
if tags :
|
||||
for tt in tags.strip().split(',') :
|
||||
if tt and not tt in tags_list :
|
||||
tags_list.append(tt)
|
||||
|
||||
|
||||
main_type_key = "tree"+str(project_id)
|
||||
data={
|
||||
"id" : eid,
|
||||
"version_key" : "base_1",
|
||||
"main_type_key" : main_type_key,
|
||||
"main_type" : project_title,
|
||||
# "group_key" : "",
|
||||
"title" : title,
|
||||
"parent_id" : parent_id,
|
||||
"child_order" : child_order,
|
||||
"tags" : tags_list,
|
||||
"meta_info" : meta_info
|
||||
}
|
||||
|
||||
if len(bulk_data) >= 100:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 10, 'to_elastic , total='+str(total_count) , str(i)+ ', id=' + eid )
|
||||
elastic.importBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
|
||||
bulk_data.append(
|
||||
{
|
||||
'_id' : eid,
|
||||
'_source' : data
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 10, ' to_elastic , total='+str(total_count) )
|
||||
elastic.importBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
print("end success import_to_elastic")
|
||||
t2 = datetime.datetime.now()
|
||||
print(t2 - t1)
|
||||
|
||||
|
||||
|
||||
load_dotenv(dotenv_path="config.env")
|
||||
|
||||
host = os.getenv("HOST_160_LOCAL")
|
||||
password = os.getenv("HOST_160_PASS")
|
||||
|
||||
# print(host)
|
||||
# print(password)
|
||||
|
||||
|
||||
path_base_map = "/home/sabr/back_new/elastic_backend/app/schemas/mappings/"
|
||||
elastic = ElasticHelper(host, password, "elastic", path_base_map)
|
||||
|
||||
|
||||
config_env = load_dotenv(dotenv_path="./config.env")
|
||||
MYSQL_username = os.getenv("MYSQL_username")
|
||||
MYSQL_root_password = os.getenv("MYSQL_root_password")
|
||||
MYSQL_port = os.getenv("MYSQL_port")
|
||||
MYSQL_host = os.getenv("MYSQL_import_host")
|
||||
cnx = mariadb.connect(user="root", password=MYSQL_root_password, host=MYSQL_host)
|
||||
cursor = cnx.cursor()
|
||||
|
||||
|
||||
query = "select id, title from tlist.tl_projects"
|
||||
cursor.execute(query)
|
||||
list_projects=[]
|
||||
while 1 :
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
|
||||
|
||||
pid = row[0] # "id"
|
||||
title = row[1] # "title"
|
||||
# print(title)
|
||||
|
||||
# پلانهای استفاده نشده در مجلس
|
||||
if pid >= 100 and pid <= 200 :
|
||||
continue
|
||||
|
||||
# موضوعات درختی قدیم که دیگه جایگزین با 6 شد
|
||||
if pid == 1 :
|
||||
continue
|
||||
|
||||
list_projects.append({
|
||||
"id" : pid,
|
||||
"title" : title
|
||||
})
|
||||
|
||||
|
||||
|
||||
for item in list_projects :
|
||||
print('*'*100)
|
||||
print(item)
|
||||
import_to_gn_tree_elastic( cursor, elastic, item["id"], item["title"],"gn_tree")
|
||||
|
||||
|
||||
cursor.close()
|
||||
Reference in New Issue
Block a user