12345678910111213141516171819202122232425262728293031323334 |
- # 创建Elasticsearch客户端
- import requests
- import json
- # Elasticsearch地址
- base_url = 'http://elasticsearch-master.rxdpdev.svc.k5.bigtree.zone:9200'
- # 插入一条数据
- def insert_data(id, name):
- url = f'{base_url}/rxdp_tag_all/_doc/{id}'
- doc = {
- 'id': id,
- 'name': name
- }
- headers = {'Content-Type': 'application/json'}
- response = requests.put(url, data=json.dumps(doc), headers=headers)
- # print(response.json())
- # 根据ID查询数据
- def get_data_by_id(id):
- url = f'{base_url}/rxdp_tag_all/_doc/{id}'
- headers = {'Content-Type': 'application/json'}
- response = requests.get(url, headers=headers)
- print(response.json()['_source'])
- # 调用插入数据函数
- insert_data(2, 'John Doe')
- # 调用根据ID查询数据函数
- get_data_by_id(2)
|