from elasticsearch import Elasticsearch from elasticsearch import helpers import pymysql import time # 連接ES es = Elasticsearch( ['127.0.0.1'], port=9200 ) # 連接MySQL print("Connect to mysql...") mysql_db = "test" m_conn = pymysql.connect('localhost', 'root', '數據庫密碼', 'table表') m_cursor = m_conn.cursor() try: num_id = 0 while True: s = time.time() # 查詢數據 sql = "select good_id, title,description from goods LIMIT {}, 100000".format(num_id*100000) # 這里假設查詢出來的結果為 張三 26 北京 m_cursor.execute(sql) query_results = m_cursor.fetchall() if not query_results: print("MySQL查詢結果為空 num_id=<{}>".format(num_id)) break else: actions = [] for line in query_results: # 拼接插入數據結構 action = { "_index": "tenco2019", "_type": "goods", "_id":line[0], "_source": { "good_title": line[1], "good_description": line[2], } } # 形成一個長度與查詢結果數量相等的列表 actions.append(action) # 批量插入 a = helpers.bulk(es, actions) e = time.time() print("{} {}s".format(a, e-s)) num_id += 1 finally: m_cursor.close() m_conn.close() print("MySQL connection close...")