码迷,mamicode.com
首页 > 编程语言 > 详细

python elasticsearch 批量写入数据

时间:2019-01-09 14:16:13      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:esc   test   code   结构   cse   mysql查询   bre   密码   title   

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...")

 

python elasticsearch 批量写入数据

标签:esc   test   code   结构   cse   mysql查询   bre   密码   title   

原文地址:https://www.cnblogs.com/firebirdweb/p/10243892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!