第一步:
否则会报找不到 pymysql的错;
2、源码
from elasticsearch import Elasticsearch
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus as urlquote
import time
pd.set_option('expand_frame_repr', False)
pd.set_option('display.max_rows', 10000)
userName = "xxxx"
password = "xxxx"
dbHost = "xxx.xx.xx.xx"
dbPort = "xxxx"
dbName = "xxxx"
es = Elasticsearch([{"scheme": "http", "host": "xxx.xx.xx.xx", "port": xxxx}])
json_body = {
"from": 0,
"size": 10000,
"track_total_hits": True,
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"range": {
"createTime": {
"gt": "2020-11-01 00:00:00",
"lt": "2020-11-01 23:59:59"
}
}
}
}
}
}
query = es.search(index="xxxx", body=json_body)
results = query['hits']['hits']
total = query['hits']['total']
print(total)
df = pd.DataFrame(results)
col = pd.DataFrame(list(df["_source"]))
print(col)
start = time.time()
db_connect = """mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8""" % (userName, urlquote(password), dbHost, dbPort, dbName)
engine = create_engine(db_connect)
con = engine.connect()
col.to_sql(name='xxxx', con=con, if_exists='append')
con.close()
end = time.time()
print("程序运行结束!!!")
print("程序运行时间:", end - start)