gpt4 book ai didi

python - 如何使用python加载Mysql到Elasticsearch

转载 作者:行者123 更新时间:2023-12-05 06:51:07 26 4
gpt4 key购买 nike

我有一个表名 employees

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

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

最佳答案

这是我的快速想法😎

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

关于python - 如何使用python加载Mysql到Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66261692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com