简体   繁体   中英

How to load Mysql to Elasticsearch using python

I have a table name employees

I need to push the employees to Elasticsearch index using python

import MySQLdb
import json
from elasticsearch import Elasticsearch
db = MySQLdb.connect("localhost", "admin", "password", "dbname")
cursor = db.cursor()

Here is my quick idea

from sqlalchemy import create_engine
import pymysql
import pandas as pd

from elasticsearch import Elasticsearch
from elasticsearch import helpers
#Replaceme
CONSTR = 'mysql+pymysql://root:@127.0.0.1'
sqlEngine       = create_engine(CONSTR, pool_recycle=3600)
dbConnection    = sqlEngine.connect()
df           = pd.read_sql("select * from employees", dbConnection);
rows = df.to_json(orient='records')
es = Elasticsearch()
actions=[]
for item in rows:
    action = {
        #replace me if need to
        "_id": "employee_%s"%item['id'],
        "doc_type": "_doc",
        "doc": item
    }
    actions.append(action)

response = helpers.bulk(es, actions, index="employees", doc_type='_doc')
 

dbConnection.close()

Dump out a CSV file ( SELECT.. INTO OUTFILE ) from MySQL, Load that into Elasticsearch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM