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]y


Enter 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
zhangdapeng520

apm_system
zhangdapeng520

kibana_system
zhangdapeng520

logstash_system
zhangdapeng520

beats_system
zhangdapeng520

remote_monitoring_user
zhangdapeng520

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

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

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

建立连接

from es import Elasticsearch

auth = ("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_data


print(get_search_data(r))

# 删除索引
edb.indices.delete(index=index)

根据ID修改数据

import time

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,
)

# 修改
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_data


print(get_search_data(r))

# 删除索引
edb.indices.delete(index=index)

根据ID删除数据

import time

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,
)

# 删除
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_data


print(get_search_data(r))

# 删除索引
edb.indices.delete(index=index)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/371029.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

esp8266 步骤

安装驱动 http://arduino.esp8266.com/stable/package_esp8266com_index.json oled库 esp8266-oled-ssd1306

网络规划与部署实训

一 实训目的及意义 本周实训主要是了解网络规划与部署,熟悉三大厂商华为、思科、锐捷交换机路由器以及相关协议的原理和配置,提高学生的动手能力和分析规划部署能力。 实训主要针对计算机网络系统集成的设计与实现的实际训练,着重锻炼学生熟练…

网站不收录,与服务器不备案有关吗

随着互联网的快速发展,网站已经成为企业、个人和机构宣传和展示自己的重要平台。然而,许多网站在建设完成后却面临着不收录的问题,这给网站的管理者和拥有者带来了很大的困扰。其中,一些人认为,网站不收录的原因与服务…

什么是前端工程化,请举例说明

前端工程化 前端工程化的定义为什么需要前端工程化前端工程化的核心概念 模块化开发:组件化开发:规范化开发:自动化开发:持续集成 前端工程化的主要工具前端工程化的应用总结: 前端工程化 前端工程化的定义 前端工程…

百面嵌入式专栏(面试题)华为面试题

文章目录 题目:static有什么用途?(请至少说明两种)题目:引用与指针有什么区别?题目:描述实时系统的基本特性题目:全局变量和局部变量在内存中是否有区别?如果有,是什么区别?题目:什么是平衡二叉树?题目:堆栈溢出一般是由什么原因导致的?题目:什么函数不能声明为…

2020年通信工程师初级专业实务真题

文章目录 一、第1章 现代通信网概述:信令网、同步网、管理网。第10章 通信业务:通信产业链,通信终端的分类,通信业务的定义及分类二、第3章 接入网:无线接入网的优点,接入网的接口(UNI&#xff…

2024 Google Chrome 浏览器回退安装旧版本

2024 Google Chrome 浏览器回退安装旧版本 查看当前谷歌版本备份浏览器数据卸载浏览器双击重新安装旧版本浏览器 查看当前谷歌版本 详细参考:参考 笔记:最近谷歌浏览器更新后,用着总感觉别扭:不习惯 备份浏览器数据 &#xff…

【自然语言处理】P4 神经网络基础 - 激活函数

目录 激活函数SigmoidTanhReLUSoftmax 本节博文介绍四大激活函数,Sigmoid、Tanh、ReLU、Softmax。 激活函数 为什么深度学习需要激活函数? 博主认为,最重要的是 引入非线性。 神经网络是将众多神经元相互连接形成的网络。如果神经元没有激…

【Spring】Spring 启示录

一、OCP 开闭原则 核⼼:在扩展系统功能时不需要修改原先写好的代码,就是符合OCP原则的,反之修改了原先写好的代码,则违背了OCP原则的 若在扩展系统功能时修改原先稳定运⾏程序,原先的所有程序都需要进⾏重新测试&…

【RT-DETR有效改进】利用SENetV1重构化网络结构 (ILSVRC冠军得主)

👑欢迎大家订阅本专栏,一起学习RT-DETR👑 一、本文介绍 本文给大家带来的改进机制是SENet(Squeeze-and-Excitation Networks)其是一种通过调整卷积网络中的通道关系来提升性能的网络结构。SENet并不是一个独立的网络模型,而是一个可以和现有的任何一个模型相结合…

Spark Shuffle Service简介与测试

一 Dynamic Resource Allocation(动态资源分配) 了解Shuffle Service之前,我们需要先了解和Shuffle Service有关的另一个特性:动态资源分配。 Spark管理资源有两种方式:静态资源分配和动态资源分配。 静态资源分配:spark提交任…

FastAdmin青动CRM-E售后

应用介绍 一款基于FastAdminThinkPHP和uniapp开发的CRM售后管理系统,旨在助力企业销售售后全流程精细化、数字化管理,主要功能:客户、合同、工单、任务、报价、产品、库存、出纳、收费,适用于:服装鞋帽、化妆品、机械机…

数据描述的统计量解释-上

目录 一.导读 二.介绍 ①算数平均数 ②几何平均数 ③标准差 ④变异系数 ⑤分位数 ⑥方差 三.结尾 一.导读 在讲到数据描述的时候,我们提及了数据集中位置、离散程度、偏度和峰度以及单个数据变量的分布情况。而在这些当中,我们遇到了一些统计量…

机器学习系列——(十)支持向量机

一、背景 支持向量机(Support Vector Machine,SVM)是一种用于分类、回归和离群点检测等领域的监督学习方法。它最初由Vapnik和Cortes在1995年提出,被认为是机器学习领域中最成功的算法之一。 二、原理 2.1 线性SVM 我们先从最简…

openssl3.2 - use openssl cmd create ca and p12

文章目录 openssl3.2 - use openssl cmd create ca and p12概述笔记实验的openssl环境建立CA生成私钥和证书请求生成CA证书用CA签发应用证书用CA对应用证书进行签名将已经签名好的PEM证书封装为P12证书验证P12证书是否可用END openssl3.2 - use openssl cmd create ca and p12 …

Kafka系列(二)将消息数据写入Kafka系统--生产者【异步发送、同步发送、单线程发送、多线程发送、配置生产者属性、自定义序列化、自定义主题分区】

Kafka系列 发送消息到 Kafka 主题了解异步模式了解同步模式线程发送消息的步骤生产者用单线程发送消息生产者用多线程发送消息 配置生产者属性保存对象的各个属性一序列化序列化一个对象序列化对象的存储格式自己实现 序列化的步骤1. 创建序列化对象2. 编写序列化工具类3. 编写…

企业级大数据安全架构(九)FreeIPA管理员密码忘记后如何修改

作者:楼高 1重置Directory Server管理员密码 1.1停止directory server服务 [rootipa schema]# start-dirsrv HDP-HADOOP 如果你不知道你的实例名,可以通过如下方式获取 1.2生成一个新的HASH密码 停止服务后使用pwdhash命令生成一个新的HASH密码 [r…

HashMap的put和get流程

一、put流程图 首先进行哈希值的扰动,获取一个新的哈希值。(key null) ? 0 : (h key.hashCode()) ^ (h >>> 16); 判断tab是否位空或者长度为0,如果是则进行扩容操作。 if ((tab table) null || (n tab.length) 0)n (tab resize()).l…

JAVASE进阶:Collection高级(2)——源码剖析ArrayList、LinkedList、迭代器

👨‍🎓作者简介:一位大四、研0学生,正在努力准备大四暑假的实习 🌌上期文章:JAVASE进阶:Collection高级(1)——源码分析contains方法、lambda遍历集合 📚订阅…

Java学习-内部类

内部类概述 1.成员内部类 注意: 2.静态内部类 3.局部内部类(看看就行) 4.匿名内部类 应用场景:通常作为一个参数传给方法 Eg.小猫和小狗都参加游泳比赛