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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg