常规操作elasticSearch查看和索引(存储)数据
  9OVhFvwkhDei 2023年12月13日 21 0


常规操作elasticSearch:

对于elasticSearch的操作 通常用rest API完成

查看所有节点:
GET:
192.168.31.125:9200/_cat/nodes

示例返回:

127.0.0.1 16 97 10 0.23 0.56 0.62 dilm * 6a850788e223
查看健康状态:
GET:
192.168.31.125:9200/_cat/health

示例返回:

1635298278 01:31:18 elasticsearch green 1 1 4 4 0 0 0 0 - 100.0%
查看主节点:
GET:
192.168.31.125:9200/_cat/master

示例返回:

LxbmZJweRySmiwKYs4eAHg 127.0.0.1 127.0.0.1 6a850788e223
查看所有索引(类比mysql查看所数据库):
GET:
192.168.31.125:9200/_cat/indices

示例返回:

green  open .kibana_task_manager_1       CS2sQjxhTmqK_iwwyI0X4Q 1 0    2 0  31.2kb  31.2kb
green  open kibana_sample_data_ecommerce mPzvtBuMQ3KmIWZs9mGlyQ 1 0 4675 0   4.7mb   4.7mb
green  open .apm-agent-configuration     HNEJe3GNSBipQlb-3_Ltow 1 0    0 0    283b    283b
green  open .kibana_1                    hIKbqbavQhKjvhM9znRc9A 1 0   54 1 943.2kb 943.2kb
yellow open customer                     -EYnoz-SQSyFAuXWL_4J5w 1 1    1 0   3.3kb   3.3kb
保存一条数据 put 保存:

注意:

put保存必须有id(唯一识别)

PUT:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/1
json参数:
{"name":"zxl"}

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 9,
    "_primary_term": 1
}
不带唯一识别失败:
{
    "error": "Incorrect HTTP method for uri [/customer/external/] and method [PUT], allowed: [POST]",
    "status": 405
}
保存一条数据 POST保存:

注意:

不带id(唯一识别) 新增(唯一识别自动生成)

带id(唯一识别) id已存在 更新

带id(唯一识别) id已存在 新增

POST:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/2
json参数:
{"name":"sss"}

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "2",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 12,
    "_primary_term": 1
}
不带唯一识别成功实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "y7atv3wBC3_10cmr4V9N",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 1
}
查看数据:
GET:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/1

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 7,
    "_seq_no": 11,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "ooo"
    }
}

_seq_no 和 _primary_term 常用语存储数据并发时乐观锁操作 防止同时请求被多次修改

***乐观锁保存示例:
PUT:
192.168.31.125:9200/customer/external/1?if_seq_no=11&if_primary_term=1
json参数:
{"name":"yyds"}

示例返回:

第一次操作条件符合成功:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 8,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 14,
    "_primary_term": 1
}
继续点击提交 _seq_no发生变化 保存失败:
{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]",
                "index_uuid": "-EYnoz-SQSyFAuXWL_4J5w",
                "shard": "0",
                "index": "customer"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]",
        "index_uuid": "-EYnoz-SQSyFAuXWL_4J5w",
        "shard": "0",
        "index": "customer"
    },
    "status": 409
}
***POST的_update更新数据更新文档示例:
POST:
192.168.31.125:9200/customer/external/1/_update
json参数:
{"name":"yyds"}

POST更新文档指定 索引/类型/唯一识别/_update

json参数应该为:{“doc”:{“name”:“333”}}

数据参数项必须在json对象的doc内。

数据会对比原数据,如果数据相同, 数据不会有操作,并且返回result:noop。此时操作:版本号_version 操作序列号_seq_no 不变。

示例返回:

第一次操作条件符合成功:

继续点击提交 保存:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 11,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}
继续提交:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 11,
    "result": "noop",
    "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}
只有POST带有_update参数,更新数据 数据才会对比原数据。

put和post不带有_udpate参数,都是更新数据。

如果增加唯一识别下数据的其他属性,直接带上添加保存即可。增加新属性 ,数据肯定变化,post带有udpate也是更新哦!

删除文档
DELETE请求:
192.168.31.125:9200/customer/external/1/

示例返回:

成功实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 14,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 20,
    "_primary_term": 1
}
继续提交 删除不存在的主键文档:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 15,
    "result": "not_found",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 21,
    "_primary_term": 1
}

删除文档后查找数据

get:
192.168.31.125:9200/customer/external/1/
实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "found": false
}

删除是可以针对索引 文档。没有直接删除类型的操作

删除索引
DELETE请求:
192.168.31.125:9200/customer/

示例返回:

成功实例:
{
    "acknowledged": true
}

删除索引后查找数据

get:
192.168.31.125:9200/customer/external/1/
实例:
{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [customer]",
                "resource.type": "index_expression",
                "resource.id": "customer",
                "index_uuid": "_na_",
                "index": "customer"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [customer]",
        "resource.type": "index_expression",
        "resource.id": "customer",
        "index_uuid": "_na_",
        "index": "customer"
    },
    "status": 404
}
批量导入数据:
POST:
192.168.31.125:9200/customer/external/_bulk

json参数语法:

{"index":{"_id":"1"}} 文档1的主键
{"name":"zhao"}       文档1的内容
{"index":{"_id":"2"}} 文档2的主键
{"name":"qian"}       文档2的主键

常规操作elasticSearch查看和索引(存储)数据_big data

复杂批量实例:
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123"}}
{ "doc":{"title":"My updated blog post"} }

POST /_bulk
{ “delete”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }} 删除
{ “create”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }}创建
{ “title”: “My first blog post” } 创建的内容
{ “index”: { “_index”: “website”, “_type”: “blog” }} 插入
{ “title”: “My second blog post” } 插入的内容
{ “update”: { “_index”: “website”, “_type”: “blog”, “_id”: “123”}} 更新
{ “doc”:{“title”:“My updated blog post”} } 更新的内容

常规操作elasticSearch查看和索引(存储)数据_数据_02


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月13日 0

暂无评论

推荐阅读
9OVhFvwkhDei