如何使用Python将MySQL加载到Elasticsearch?

3

我有一个名为employees的表

我需要使用Python将employees推送到Elasticsearch索引

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

0

这是我的快速想法

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()

3
操作 = [],我认为你需要在外部进行声明。 - user14257643

0
从MySQL中导出CSV文件(SELECT .. INTO OUTFILE),将其加载到Elasticsearch中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接