painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

需求:修复与增强写入的数据Tags字段中,逗号分割的文本应该是数组,而不是一个字符串需求:后期需要对Tags进行Aggregation统计IngestNodeElasticsearch5.0后,引入的一种新的节点类型。默认配置下,每个节点都是IngestNode具有预处理数据的能力,可拦截Index或者BulckAPI的请求对数据进行转换,并重新返回给Index和…

大家好,又见面了,我是你们的朋友全栈君。

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

Tags 字段中,逗号分割的文本应该是数组,而不是一个字符串需求:后期需要对 Tags 进行 Aggregation 统计

Ingest Node

Elasticsearch 5.0 后,引入的一种新的节点类型。默认配置下,每个节点都是 Ingest Node具有预处理数据的能力,可拦截 Index 或者 Bulck API 的请求

对数据进行转换,并重新返回给 Index 和 Bluck API

无需 Logstash ,就可以进行数据的预处理,例如为某个字段设置默认值;重命名某个字段的字段名;对字段值进行 Split 操作

支持设置 Painless 脚本,对数据进行更加复杂的加工

Pipeline & Processor

Pipeline – 管道会对通过的数据(文档),按照顺序进行加工

Processor – Elasticsearch 对一些加工的行为进行了抽象包装Elasticsearch 有很多内置的 Processors。也支持通过插件的方式,实现自己的 Processsor

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

使用 Pipeline 切分字符串

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

# 测试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”

}

}

]

}

为文档增加字段

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

#同时为文档,增加一个字段。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”

}

}

]

}

Pipeline API

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

添加 Pipeline 并测试

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

# 为ES添加一个 Pipeline

PUT _ingest/pipeline/blog_pipeline

{

“description”: “a blog pipeline”,

“processors”: [

{

“split”: {

“field”: “tags”,

“separator”: “,”

}

},

{

“set”: {

“field”: “views”,

“value”: 0

}

}

]

}

#测试pipeline

POST _ingest/pipeline/blog_pipeline/_simulate

{

“docs”: [

{

“_source”: {

“title”: “Introducing cloud computering”,

“tags”: “openstack,k8s”,

“content”: “You konw, for cloud”

}

}

]

}

Index & Update By Query

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

#不使用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”

}

}

}

}

}

一些内置的 Processors

https://www.elastic.co/guide/en/elasticsea…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

|| Logstash| Ingest Node|

|–|–|

|数据输入与输出|支持从不同的数据源读取,并写入不同的数据源|支持从ES REST API 获取数据,并且写入ES|

|数据源缓冲| 实现了简单的数据队列,支持重写| 不支持缓冲|

|数据处理| 支持大量的的插件,也支持定制开发|内置的插件,可以开发 Plugin 进行扩展(Plugin 更新需要重启)|

|配置和使用| 增加了一定的架构复杂度| 无需额外部署|

https://www.elastic.co/cn/blog/should-i-us…

Painless 简介

自 ES 5.x 后引入,专门为 ES 设置,扩展了 Java 的语法

6.0 开始,ES 只支持 Painless。Grooby ,JavaScript 和 Python 都不在支持

Painless 支持所有的 Java 的数据类型及 Java API 子集

Painless Script 具备以下特性高性能 、 安全

支持显示类型或者动态定义类型

Painless 的用途

可以对文档字段进行加工处理更新或者删除字段,处理数据聚合操作

Script Field: 对返回的字段提前进行计算

Function Score:对文档的算分进行处理

在Ingest Pipeline 中执行脚本

在Reindex API,Update By Query 时,对数据进行处理

通过 Painless 脚本访问字段

上线文

语法Ingestion

ctx.field_name

Update

ctx._source.field_name

Search & Aggregation

doc{“field_name”]

案例1:Script Processsor

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

# 增加一个 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”

}

}

]

}

案例2:文档更新计数

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

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

案例3:搜索时的Script 字段

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

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”: {}

}

}

Script :Inline v.s Stored

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

#保存脚本在 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

}

}

}

脚本缓存

编译的开销相较大

Elasticsearch 会将甲苯编译后缓存在 Cache 中Inline scripts 和 Stored Scripts 都会被缓存

默认缓存 100个脚本

painless数字类型转换_笔记四十五: Ingest Pipeline 与 Painless Script

本节知识点

概念讲解:Ingest Node,Pipeline 与 Processor

Ingest Node 与 Logstash 的⽐较

Pipeline 的 相关操作 / 内置 Processor 讲解与演示

Painless 脚本与Ingestion (Pipeline)

Update

Search & Aggregation

本作品采用《CC 协议》,转载必须注明作者和本文链接

快乐就是解决一个又一个的问题!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126741.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月8日 下午1:40
下一篇 2022年4月8日 下午1:40


相关推荐

  • pycharm连接mysql数据库代码_怎么把Python与pycharm连接

    pycharm连接mysql数据库代码_怎么把Python与pycharm连接PyCharm版本:2020.3使用PyCharm连接数据库(MySQL)前言步骤SQLite总结前言最好使用PyCharmProfessional版步骤前期需要安装包(比如:pymysql)1.在PyCharm右侧工具栏有Database,点击打开如果没有,则在view|ToolWindows|Database选择显示2.点击Database中的+,选择DataSource,选择MySQL3.填写远程连接MySQL数据库的参数Host:

    2022年8月28日
    6
  • 代码走查1

    代码走查1线上问 和研发一起简单的从代码层面去解决问题 略 low 代码走查 1 存在的问题 SQL 查询 1 对于业务上没有用的字段没必要添加比如 IsDel 在具体的业务上 删除数据是直接硬删除 直接删除了物理数据 所以 在 SQL 查询的时候 SQL 读着可能没有问题 但实际执行却没有意义 没必要添加 2 不等于 查空值 要和判断语句相符合 不要在 SQL 语句中否定 而 if 判断中肯定 很容易迷茫

    2025年10月24日
    5
  • eureka集群配置_hadoop高可用集群搭建

    eureka集群配置_hadoop高可用集群搭建Eruka高可用(集群)EurekaServer高可用配置(1)高可用是什么?“高可用性”(HighAvailability)通常来描述一个系统经过专门的设计,从而减少停工时间,而保持其服务的高度可用性如:给EurekaServer搞一个备份(2)服务同步原理多个EurekaServer之间也会互相注册为服务,当服务提供者注册到EurekaServer集群中的某个节点时,该节点会把服务的信息同步给集群中的每个节点,从而实现数据同步。因此,无论客户端访问到EurekaServer集

    2022年8月21日
    9
  • 网站背景音乐HTML代码_ppt播放背景音乐

    网站背景音乐HTML代码_ppt播放背景音乐这篇文章主要为大家详细介绍了HTML5页面背景音乐代码网页背景音乐通用代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。网页背景音乐是个比较老旧的问题和技术了,上世纪90年代就是十分流行的了,给自己的网页加上一段背景音乐轻快而且于感染力,随着网页技术的发展,除了少部分音乐站点、个人博客、游戏站点外几乎很少有使用到网页背景音乐的地方,当然了这也是织梦361小…

    2026年3月7日
    4
  • 数据库的简介与类型

    数据库的简介与类型数据库的简介与类型

    2022年4月22日
    39
  • flex垂直居中问题「建议收藏」

    flex垂直居中问题「建议收藏」.container{ display:flex; justify-content:center; aligin-items:center;}垂直居中当内容超过container,上方会超出容器设置.item{ margin:auto;}或:justify-content:safecenteralign-self:safecenter

    2022年6月4日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号