发布时间:2019-09-08 09:08:58编辑:auto阅读(2466)
在这里,我使用Elasticsearch官方推荐
elasticsearch第三方包来讲述插入数据的两种方法。
1. index
这是很简单的一个插入数据的方法,每条数据调用一个index方法,代码如下
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch( "localhost:9200" )
data = {
"@timestamp" : datetime.now().strftime( "%Y-%m-%dT%H:%M:%S.000+0800" ),
"http_code" : "404",
"count" : "10"
}
es.index( index="http_code", doc_type="error_code", body=data )
2. bulk
一次性插入多条数据的方法
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" ),
"http_code" : "404",
"count" : random.randint( 1, 100 )
}
package.append( row )
actions = [
{
'_op_type': 'index',
'_index': "http_code", //index
'_type': "error_code", //type
'_source': d
}
for d in package
]
elasticsearch.helpers.bulk( es, action )
上一篇: Windows 系统中Python常用的
下一篇: python的session方法使用记录
53642
40471
34893
30615
25490
25269
23704
19146
15275
14787
1304°
1209°
1294°
1319°
1340°
1514°
1465°
1402°
1502°
1455°