用python批量更新es数据根据id_python批量插入数据到es和读取es数据

一、插入数据

1、首先准备类似如下数据

{"_type": "type1", "_id": 1, "_index": "test", "_source": {"JOBNAME0": "guba_eastmoney_com_265162", "JOBNAME1": "guba_eastmoney_com_265162"}}

2、调用es相关模块插入数据到es中

#!/usr/bin/python

import threading

import queue

import json

import time

from elasticsearch import Elasticsearch

from elasticsearch import helpers

import os

import sys

#

# host_list = [

# {"host":"10.58.7.190","port":9200},

# {"host":"10.58.55.191","port":9200},

# {"host":"10.58.55.192","port":9200},

# ]

#

host_list = [

{"host":"10.87.7.190","port":9200},

]

# create a es clint obj

client = Elasticsearch(host_list)

with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),"insert.json"),"r") as f:

for line in f:

actions = []

actions.append(json.loads(line))

try:

for k, v in helpers.parallel_bulk(client=client, thread_count=1, actions=actions):

# 这里的actions是插入es的数据,这个格式必须是列表的格式,列表的每个元素又必须是字典

pass

except Exception as e:

sys.stderr(e)

3、查看es

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值