tmp2.py 808 B

12345678910111213141516171819202122232425262728293031323334
  1. # 创建Elasticsearch客户端
  2. import requests
  3. import json
  4. # Elasticsearch地址
  5. base_url = 'http://elasticsearch-master.rxdpdev.svc.k5.bigtree.zone:9200'
  6. # 插入一条数据
  7. def insert_data(id, name):
  8. url = f'{base_url}/rxdp_tag_all/_doc/{id}'
  9. doc = {
  10. 'id': id,
  11. 'name': name
  12. }
  13. headers = {'Content-Type': 'application/json'}
  14. response = requests.put(url, data=json.dumps(doc), headers=headers)
  15. # print(response.json())
  16. # 根据ID查询数据
  17. def get_data_by_id(id):
  18. url = f'{base_url}/rxdp_tag_all/_doc/{id}'
  19. headers = {'Content-Type': 'application/json'}
  20. response = requests.get(url, headers=headers)
  21. print(response.json()['_source'])
  22. # 调用插入数据函数
  23. insert_data(2, 'John Doe')
  24. # 调用根据ID查询数据函数
  25. get_data_by_id(2)