Elasticsearch-26.Ingest Pipeline 与 Painless Script

Elasticsearch

Ingest Pipeline 与 Painless Script

需求:修复与增强写⼊的数据

在这里插入图片描述

Ingest Node

  • Elasticsearch 5.0 后,引⼊的⼀种新的节点类型。默认配置下,每个节点都是 Ingest Node
    • 具有预处理数据的能⼒,可拦截 Index 或 Bulk API 的请求
    • 对数据进⾏转换,并重新返回给 Index 或 Bulk API
  • ⽆需 Logstash,就可以进⾏数据的预处理,例如
    • 为某个字段设置默认值;重命名某个字段的字段名;对字段值进⾏ Split 操作
    • ⽀持设置 Painless 脚本,对数据进⾏更加复杂的加⼯

Pipeline & Processor

  • Pipeline - 管道会对通过的数据(⽂档),按照顺序进⾏加⼯
  • Processor - Elasticsearch 对⼀些加⼯的⾏为进⾏了抽象包装
    • Elasticsearch 有很多内置的 Processors。也⽀持通过插件的⽅式,实现⾃⼰的 Processor
      在这里插入图片描述

使⽤ Pipeline 切分字符串

在这里插入图片描述

为⽂档增加字段

在这里插入图片描述

Pipeline API

在这里插入图片描述

添加 Pipeline 并测试

在这里插入图片描述

Index & Update By Query

在这里插入图片描述

⼀些内置 Processors

  • https://www.elastic.co/guide/en/elasticsearch/reference/7.1/ingest-processors.html
    • Split Processor (例:将给定字段值分成⼀个数组)
    • Remove / Rename Processor (例:移除⼀个重命名字段)
    • Append (例:为商品增加⼀个新的标签)
    • Convert(例:将商品价格,从字符串转换成 float 类型)
    • Date / JSON(例:⽇期格式转换,字符串转 JSON 对象)
    • Date Index Name Processor (例:将通过该处理器的⽂档,分配到指定时间格式的索引中)
    • Fail Processor (⼀旦出现异常,该 Pipeline 指定的错误信息能返回给⽤户)
    • Foreach Process(数组字段,数组的每个元素都会使⽤到⼀个相同的处理器)
    • Grok Processor(⽇志的⽇期格式切割)
    • Gsub / Join / Split(字符串替换 / 数组转字符串/ 字符串转数组)
    • Lowercase / Upcase(⼤⼩写转换)

Ingest Node v.s Logstash

LogstashIngest Node
数据输⼊与输出⽀持从不同的数据源读取,并写⼊不同的数据源⽀持从 ES REST API 获取数据,并且写⼊ Elasticsearch
数据缓冲实现了简单的数据队列,⽀持重写不⽀持缓冲
数据处理⽀持⼤量的插件,也⽀持定制开发内置的插件,可以开发 Plugin 进⾏扩展(Plugin 更新需要重启)
配置和使⽤增加了⼀定的架构复杂度⽆需额外部署

https://www.elastic.co/cn/blog/should-i-use-logstash-or-elasticsearch-ingest-nodes

Painless 简介

  • ⾃ Elasticsearch 5.x 后引⼊,专⻔为 Elasticsearch 设计,扩展了 Java 的语法。
  • 6.0 开始,ES 只⽀持 Painless。Groovy, JavaScript 和 Python 都不再⽀持
  • Painless ⽀持所有 Java 的数据类型及 Java API ⼦集
  • Painless Script 具备以下特性
    • ⾼性能 / 安全
    • ⽀持显示类型或者动态定义类型

Painless 的⽤途

  • 可以对⽂档字段进⾏加⼯处理
    • 更新或删除字段,处理数据聚合操作
    • Script Field:对返回的字段提前进⾏计算
    • Function Score:对⽂档的算分进⾏处理
  • 在 Ingest Pipeline 中执⾏脚本
  • 在 Reindex API,Update By Query 时,对数据进⾏处理

通过 Painless 脚本访问字段

上下⽂语法
Ingestionctx.field_name
Updatectx._source.field_name
Search & Aggregationdoc[“field_name”]
案例 1:Script Processor

在这里插入图片描述

案例 2:⽂档更新计数

在这里插入图片描述

案例 3:搜索时的 Script 字段

在这里插入图片描述

Script: Inline v.s Stored

在这里插入图片描述

脚本缓存

在这里插入图片描述

本节知识点

  • 概念讲解:Ingest Node,Pipeline 与 Processor
  • Ingest Node 与 Logstash 的⽐较
  • Pipeline 的 相关操作 / 内置 Processor 讲解与演示
  • Painless 脚本与
    • Ingestion (Pipeline)
    • Update
    • Search & Aggregation

demoAPI

#########Demo for Pipeline###############

DELETE tech_blogs

#Blog数据,包含3个字段,tags用逗号间隔
PUT tech_blogs/_doc/1
{
  "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
}


# 测试split tags
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "to split blog tags",
    "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_id": "id",
      "_source": {
        "title": "Introducing big data......",
        "tags": "hadoop,elasticsearch,spark",
        "content": "You konw, for big data"
      }
    },
    {
      "_index": "index",
      "_id": "idxx",
      "_source": {
        "title": "Introducing cloud computering",
        "tags": "openstack,k8s",
        "content": "You konw, for cloud"
      }
    }
  ]
}


#同时为文档,增加一个字段。blog查看量
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "to split blog tags",
    "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      },

      {
        "set":{
          "field": "views",
          "value": 0
        }
      }
    ]
  },

  "docs": [
    {
      "_index":"index",
      "_id":"id",
      "_source":{
        "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
      }
    },


    {
      "_index":"index",
      "_id":"idxx",
      "_source":{
        "title":"Introducing cloud computering",
  "tags":"openstack,k8s",
  "content":"You konw, for cloud"
      }
    }

    ]
}



# 为ES添加一个 Pipeline
PUT _ingest/pipeline/blog_pipeline
{
  "description": "a blog pipeline",
  "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      },

      {
        "set":{
          "field": "views",
          "value": 0
        }
      }
    ]
}

#查看Pipleline
GET _ingest/pipeline/blog_pipeline


#测试pipeline
POST _ingest/pipeline/blog_pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "title": "Introducing cloud computering",
        "tags": "openstack,k8s",
        "content": "You konw, for cloud"
      }
    }
  ]
}

#不使用pipeline更新数据
PUT tech_blogs/_doc/1
{
  "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
}

#使用pipeline更新数据
PUT tech_blogs/_doc/2?pipeline=blog_pipeline
{
  "title": "Introducing cloud computering",
  "tags": "openstack,k8s",
  "content": "You konw, for cloud"
}


#查看两条数据,一条被处理,一条未被处理
POST tech_blogs/_search
{}

#update_by_query 会导致错误
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
}

#增加update_by_query的条件
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "views"
                }
            }
        }
    }
}


#########Demo for Painless###############

# 增加一个 Script Prcessor
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "to split blog tags",
    "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      },
      {
        "script": {
          "source": """
          if(ctx.containsKey("content")){
            ctx.content_length = ctx.content.length();
          }else{
            ctx.content_length=0;
          }


          """
        }
      },

      {
        "set":{
          "field": "views",
          "value": 0
        }
      }
    ]
  },

  "docs": [
    {
      "_index":"index",
      "_id":"id",
      "_source":{
        "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
      }
    },


    {
      "_index":"index",
      "_id":"idxx",
      "_source":{
        "title":"Introducing cloud computering",
  "tags":"openstack,k8s",
  "content":"You konw, for cloud"
      }
    }

    ]
}


DELETE tech_blogs
PUT tech_blogs/_doc/1
{
  "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data",
  "views":0
}

POST tech_blogs/_update/1
{
  "script": {
    "source": "ctx._source.views += params.new_views",
    "params": {
      "new_views":100
    }
  }
}

# 查看views计数
POST tech_blogs/_search
{

}

#保存脚本在 Cluster State
POST _scripts/update_views
{
  "script":{
    "lang": "painless",
    "source": "ctx._source.views += params.new_views"
  }
}

POST tech_blogs/_update/1
{
  "script": {
    "id": "update_views",
    "params": {
      "new_views":1000
    }
  }
}


GET tech_blogs/_search
{
  "script_fields": {
    "rnd_views": {
      "script": {
        "lang": "painless",
        "source": """
          java.util.Random rnd = new Random();
          doc['views'].value+rnd.nextInt(1000);
        """
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
elasticsearch-7.12.0-py2.py3-none-any.whl 是 Elasticsearch 的 Python 客户端库的一个安装文件。Elasticsearch 是一个开源的实时分布式搜索和分析引擎,用于处理大规模数据集。它提供了一个简单可扩展的 RESTful API 接口,允许用户进行高效的数据搜索、分析以及存储。 这个安装文件的命名规则是根据 Python 的支持版本以及可运行平台来命名的。-py2.py3 表示可以同时兼容 Python 2 和 Python 3 版本的代码。-none-any 表示它是一个纯 Python 代码的库,不依赖于特定的操作系统或平台。 通过安装 elasticsearch-7.12.0-py2.py3-none-any.whl,您可以轻松地在您的 Python 环境中使用 Elasticsearch。这个库提供了许多功能,包括连接到 Elasticsearch 实例、执行索引、搜索和分析操作,以及管理和维护 Elasticsearch 的集群和节点。您可以使用这个库来构建各种应用,如全文搜索引擎、实时日志分析等。 要安装 elasticsearch-7.12.0-py2.py3-none-any.whl,您可以使用 pip 工具,在命令行中运行以下命令: ``` pip install elasticsearch-7.12.0-py2.py3-none-any.whl ``` 安装成功后,您就可以在您的 Python 代码中导入 elasticsearch 模块,并开始使用 Elasticsearch 的功能了。 总结:elasticsearch-7.12.0-py2.py3-none-any.whl 是 Elasticsearch 的 Python 客户端库的安装文件,用于连接、操作和管理 Elasticsearch 实例。通过安装这个库,您可以在您的 Python 项目中轻松使用 Elasticsearch 的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值