ElasticSearch | Ingest Pipeline

Ingest Node

  • ElasticSearch 5.0 后,引入的一种新的节点类型,默认配置下,每个节点都是 Ingest Node;
  • Ingest Node 具有预处理数据的能力,可拦截 Index 或 Bulk API 的请求,并对数据进行转换,然后重新返回给 Index 或 Bulk API,最后写入到 ElasticSearch 中;
  • 无需 Logstash,就可以进行数据的预处理,例如:
    • 为某个字段设置默认值;
    • 重命名某个字段的字段名;
    • 对字段值进行 Split 操作;
    • 支持设置 Painless Script,对数据进行更加复杂的加工;

Ingest Node vs Logstash

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

Pipeline & Processor

Pipeline.png

在 Ingest Node 中可以定义 Pipeline

Pipeline
  • Pipeline 会对通过的数据(文档),按照顺序进行加工;
Processor
  • ElasticSearch 对一些加工的行为进行了抽象的包装;
  • ElasticSearch 有很多内置的 Processor,也支持通过插件的方式,实现自己的 Processor;

ElasticSearch | 内置 Processor

  • Split Processer - 将给定字段值分成一个数组
  • Remove / Rename Processer - 移除 / 重命名一个字段
  • Append Processer - 为商品增加一个新的标签
  • Convert Processer - 将商品价格,从字符串转成 float 类型
  • Date / JSON Processer - 日期格式转换 / 字符串转 JSON 对象
  • Date Index Name Processor - 将通过该 Processor 的文档,分配到指定时间格式的索引中
  • Fail Processer - 一旦出现异常,该 Pipeline 指定的错误信息能返回给用户
  • Foreach Processer - 数组字段,数组的每个元素都会使用到一个相同的处理器
  • Grok Processor Processer - 日志的日期格式切割
  • Gsub / Join / Split Processer - 字符串替换 / 数组转字符串 / 字符串转数组
  • Lowercase / Upcase Processer - 大小写转换

Pipeline | 举个栗子

准备数据
PUT tech_blogs/_doc/1
{
  "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
}
_simulate API | 将字段的值用 "," 分割
  • 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"
      }
    }
  ]
}
_simulate API | 在 Pipeline 中再添加一个 Processor
  • 为文档添加字段 views,并设置默认值 0;
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"
      }
    }
  ]
}
在 ElasticSearch 中添加一个 Pipeline
PUT _ingest/pipeline/blog_pipeline
{
  "description": "a blog pipeline",
  "processors": [
      {
        "split": {
          "field": "tags",
          "separator": ","
        }
      },
      {
        "set":{
          "field": "views",
          "value": 0
        }
      }
    ]
}
查看 Pipeline
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 和不使用 Pipeline 向索引中添加数据
  • 一条数据被 Pipeline 处理,另一条没有;
PUT tech_blogs/_doc/1
{
  "title":"Introducing big data......",
  "tags":"hadoop,elasticsearch,spark",
  "content":"You konw, for big data"
}

PUT tech_blogs/_doc/2?pipeline=blog_pipeline
{
  "title": "Introducing cloud computering",
  "tags": "openstack,k8s",
  "content": "You konw, for cloud"
}

POST tech_blogs/_search
{}
使用 blog_pipeline 重建 tech_blogs 中的所有文档
  • update_by_query 会导致错误,因为 id 为 1 的文档的 tags 字段是字符串数组,而不是字符串;
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
}
增加 update_by_query 的条件
  • 只对没有 views 字段的文档作用 blog_pipeline;
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "views"
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch 中,Ingest Pipelines 是一种机制,可用于在将文档索引到 Elasticsearch 之前对它们进行处理。这些处理可以包括数据转换、文本解析、数据验证等。Ingest Pipelines 通常用于数据预处理,以便更有效地索引数据,提高搜索性能。 以下是创建 Ingest Pipelines 的步骤: 1. 定义 Pipeline:使用 ElasticsearchIngest API 定义 Pipeline。您可以使用 PUT 请求来定义 Pipeline,指定 Pipeline 的名称和包含处理步骤的 JSON 配置。 例如,以下是一个简单的 Pipeline,它将从文档中提取 "message" 字段,并将其添加到 "myfield" 字段中: ``` PUT _ingest/pipeline/my-pipeline { "description": "My pipeline", "processors": [ { "set": { "field": "myfield", "value": "{{message}}" } } ] } ``` 2. 测试 Pipeline:使用 Elasticsearch 的 simulate API 测试 Pipeline。您可以使用 POST 请求来测试 Pipeline,指定 Pipeline 的名称和要处理的文档。 例如,以下是一个简单的测试请求,它将使用 "my-pipeline" Pipeline 处理一个包含 "message" 字段的文档: ``` POST _ingest/pipeline/my-pipeline/_simulate { "docs": [ { "_source": { "message": "Hello, World!" } } ] } ``` 3. 应用 Pipeline:将 Pipeline 应用于文档。在索引文档时,您可以使用 "pipeline" 参数指定要使用的 Pipeline。 例如,以下是一个简单的索引请求,它将使用 "my-pipeline" Pipeline 处理一个包含 "message" 字段的文档: ``` PUT my-index/_doc/1?pipeline=my-pipeline { "message": "Hello, World!" } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值