Python使用zdppy_es国产框架操作Elasticsearch实现增删改查

本文主要是介绍Python使用zdppy_es国产框架操作Elasticsearch实现增删改查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Python使用zdppy_es国产框架操作Elasticsearch实现增删改查

本套教程配套有录播课程和私教课程,欢迎私信我。

在这里插入图片描述

Docker部署ElasticSearch7

创建基本容器

docker run -itd --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms2g -Xmx2g"  elasticsearch:7.17.17

配置账号密码

容器中配置文件的路径:

/usr/share/elasticsearch/config/elasticsearch.yml

把配置文件复制出来:

# 准备目录
sudo mkdir -p /docker
sudo chmod 777 -R /docker
mkdir -p /docker/elasticsearch/config
mkdir -p /docker/elasticsearch/data
mkdir -p /docker/elasticsearch/log# 拷贝配置文件
docker cp elasticsearch:/usr/share/elasticsearch/config/elasticsearch.yml /docker/elasticsearch/config/elasticsearch.yml

将配置文件修改为如下内容:

cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
# 此处开启xpack
xpack.security.enabled: true

把本机的配置文件复制到容器里面:

docker cp /docker/elasticsearch/config/elasticsearch.yml elasticsearch:/usr/share/elasticsearch/config/elasticsearch.yml

重启ES服务:

docker restart elasticsearch

进入容器,设置es的密码:

docker exec -it elasticsearch bash
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive

执行上面的命令以后,输入y,会有如下提示,全都输入:zhangdapeng520

Please confirm that you would like to continue [y/N]yEnter password for [elastic]: 
Reenter password for [elastic]: 
Enter password for [apm_system]: 
Reenter password for [apm_system]: 
Passwords do not match.
Try again.
Enter password for [apm_system]: 
Reenter password for [apm_system]: 
Enter password for [kibana_system]: 
Reenter password for [kibana_system]: 
Enter password for [logstash_system]: 
Reenter password for [logstash_system]: 
Enter password for [beats_system]: 
Reenter password for [beats_system]: 
Enter password for [remote_monitoring_user]: 
Reenter password for [remote_monitoring_user]: 
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]

将会得到如下用户名和密码:

elastic
zhangdapeng520apm_system
zhangdapeng520kibana_system
zhangdapeng520logstash_system
zhangdapeng520beats_system
zhangdapeng520remote_monitoring_user
zhangdapeng520

在宿主机中测试是否成功:

# 不带用户名密码
curl localhost:9200# 带用户名密码
curl localhost:9200 -u elastic

建立连接

from es import Elasticsearchauth = ("elastic", "zhangdapeng520")
es = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)
print(es.info())

创建索引

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 删除索引
edb.indices.delete(index=index)

新增数据

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
edb.index(index=index,id="1",document={"id": 1,"name": "张三","age": 23,}
)# 删除索引
edb.indices.delete(index=index)

根据ID查询数据

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
edb.index(index=index,id="1",document={"id": 1,"name": "张三","age": 23,}
)# 查询数据
resp = edb.get(index=index, id="1")
print(resp["_source"])# 删除索引
edb.indices.delete(index=index)

批量新增数据

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
data = [{"id": 1,"name": "张三1","age": 23,},{"id": 2,"name": "张三2","age": 23,},{"id": 3,"name": "张三3","age": 23,},
]
new_data = []
for u in data:new_data.append({"index": {"_index": index, "_id": f"{u.get('id')}"}})new_data.append(u)
edb.bulk(index=index,operations=new_data,refresh=True,
)# 查询数据
resp = edb.get(index=index, id="1")
print(resp["_source"])
resp = edb.get(index=index, id="2")
print(resp["_source"])
resp = edb.get(index=index, id="3")
print(resp["_source"])# 删除索引
edb.indices.delete(index=index)

查询所有数据

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
data = [{"id": 1,"name": "张三1","age": 23,},{"id": 2,"name": "张三2","age": 23,},{"id": 3,"name": "张三3","age": 23,},
]
new_data = []
for u in data:new_data.append({"index": {"_index": index, "_id": f"{u.get('id')}"}})new_data.append(u)
edb.bulk(index=index,operations=new_data,refresh=True,
)# 查询数据
r = edb.search(index=index,query={"match_all": {}},
)
print(r)
print(r.get("hits").get("hits"))# 删除索引
edb.indices.delete(index=index)

提取搜索结果

from es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
data = [{"id": 1,"name": "张三1","age": 23,},{"id": 2,"name": "张三2","age": 23,},{"id": 3,"name": "张三3","age": 23,},
]
new_data = []
for u in data:new_data.append({"index": {"_index": index, "_id": f"{u.get('id')}"}})new_data.append(u)
edb.bulk(index=index,operations=new_data,refresh=True,
)# 查询数据
r = edb.search(index=index,query={"match_all": {}},
)def get_search_data(data):new_data = []# 提取第一层hits = r.get("hits")if hits is None:return new_data# 提取第二层hits = hits.get("hits")if hits is None:return new_data# 提取第三层for hit in hits:new_data.append(hit.get("_source"))return new_dataprint(get_search_data(r))# 删除索引
edb.indices.delete(index=index)

根据ID修改数据

import timefrom es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
data = [{"id": 1,"name": "张三1","age": 23,},{"id": 2,"name": "张三2","age": 23,},{"id": 3,"name": "张三3","age": 23,},
]
new_data = []
for u in data:new_data.append({"index": {"_index": index, "_id": f"{u.get('id')}"}})new_data.append(u)
edb.bulk(index=index,operations=new_data,refresh=True,
)# 修改
edb.update(index=index,id="1",doc={"id": "1","name": "张三333","age": 23,},
)# 查询数据
time.sleep(1)  # 等一会修改才会生效
r = edb.search(index=index,query={"match_all": {}},
)def get_search_data(data):new_data = []# 提取第一层hits = r.get("hits")if hits is None:return new_data# 提取第二层hits = hits.get("hits")if hits is None:return new_data# 提取第三层for hit in hits:new_data.append(hit.get("_source"))return new_dataprint(get_search_data(r))# 删除索引
edb.indices.delete(index=index)

根据ID删除数据

import timefrom es import Elasticsearch# 连接es
auth = ("elastic", "zhangdapeng520")
edb = Elasticsearch("http://192.168.234.128:9200/", basic_auth=auth)# 创建索引
index = "user"
mappings = {"properties": {"id": {"type": "integer"},"name": {"type": "text"},"age": {"type": "integer"},}
}
edb.indices.create(index=index, mappings=mappings)# 添加数据
data = [{"id": 1,"name": "张三1","age": 23,},{"id": 2,"name": "张三2","age": 23,},{"id": 3,"name": "张三3","age": 23,},
]
new_data = []
for u in data:new_data.append({"index": {"_index": index, "_id": f"{u.get('id')}"}})new_data.append(u)
edb.bulk(index=index,operations=new_data,refresh=True,
)# 删除
edb.delete(index=index, id="1")# 查询数据
time.sleep(1)  # 等一会修改才会生效
r = edb.search(index=index,query={"match_all": {}},
)def get_search_data(data):new_data = []# 提取第一层hits = r.get("hits")if hits is None:return new_data# 提取第二层hits = hits.get("hits")if hits is None:return new_data# 提取第三层for hit in hits:new_data.append(hit.get("_source"))return new_dataprint(get_search_data(r))# 删除索引
edb.indices.delete(index=index)

这篇关于Python使用zdppy_es国产框架操作Elasticsearch实现增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/680021

相关文章

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi