guopengfa
发布于 2020-11-10 / 556 阅读 / 0 评论 / 0 点赞

elasticsearch的python用法

安装elasticsearch插件

pip install elasticsearch

一、单条数据的插入

rom datetime import datetime
from elasticsearch import Elasticsearch

es = Elasticsearch( "localhost:9200" ) 
data = {
    "@timestamp" : datetime.now().strftime( "%Y-%m-%dT%H:%M:%S.000+0800" ),
    "name" : "英语",
    "count" : "10"
}

es.index( index="textbook", doc_type="book", body=data )

二、批量导入

from datetime import datetime
from elasticsearch import Elasticsearch
import elasticsearch.helpers
import random

es = Elasticsearch( "localhost:9200" ) 
package = []
for i in range( 10 ):
    row = {
        "@timestamp":datetime.now().strftime( "%Y-%m-%dT%H:%M:%S.000+0800" ),
        "name" : "英语"+str(i),
        "count" : random.randint(  1, 100 )
    }
    package.append( row )

actions = [
    {
        '_op_type': 'index',
        '_index': "textbook",  //index
        '_type': "book",  //type
        '_source': d
    }
    for d in package
]    

elasticsearch.helpers.bulk( es, action )

三、题外扩展

from datetime import datetime
from elasticsearch import Elasticsearch
import elasticsearch.helpers
import random
# 如果源数据类型为DataFrame;可以进行转换;
df = df.to_dict(orient='records')
es = Elasticsearch('http://localhost:9200/')
    actions = [{
        '_op_type':'index',
        '_index':'fba_history_sale_stat',
        '_type':'data',
        '_source':listing
    }
    for listing in df
    ]
    helpers.bulk(es,actions)

评论