Elasticsearch批量匯入資料指令碼(python)

ypan@007發表於2018-08-11
#!/usr/local/bin/python2.7    //確定你的python版本
from datetime import datetime
from elasticsearch import Elasticsearch
import elasticsearch.helpers
import random
import time
es = Elasticsearch( “192.168.76.**:9200” )  //es的地址與埠
metrics = [`business.biz.real.bm.fileCount`,`business.biz.real.bm.contFailCount`,`business.biz.real.bm.updateCount`,`business.biz.real.bm.uInCount`,
`business.biz.real.bm.uNotCount`,`business.biz.real.bm.downTime`,`business.biz.real.bm.ocrTime`,`business.biz.real.bm.elleTime`,
`business.biz.real.bm.contTime`,`business.biz.real.bm.updateTime`,`business.biz.real.bm.contErrCount`]
package = []
for i in range( 1 ):
for j in metrics:
now = datetime.now()
timeStamp = time.time()
timeNow = datetime.now().strftime(“%Y-%m-%dT%H:%M:%S.000Z”)
row = {
“@timestamp”: timeNow,
“endpoint” : “0”+str(random.randint(0, 9))+”0″+str(random.randint(0, 9))+”0″+str(random.randint(0, 9)),
“metric” : j,
“step” : 0,
“timestamp” : long(timeStamp),
“value” : int(1),
“tags”: “uid”+str(random.randint(0, 10000000))
}
package.append( row )
actions = [
{
`_op_type`: `index`,
`_index`: “argus”,
`_type`: “ibam_business_dynamic_metadata”,
`_source`: d
}
for d in package
]
elasticsearch.helpers.bulk( es, actions )


相關文章