微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Logstash - 如何更改字段类型

如何解决Logstash - 如何更改字段类型

大家好,我是 Elasticsearch 的新手,我有一张名为 'american_football_sacks_against_stats' 的表。它由三列组成。

id sacks_against_total sacks_against_yards
1 12 5
2 15 3
... ... ...

问题是 sacks_against_total 和 sacks_against_yards 不是作为整数/长/浮点数“导入”的,而是作为文本字段和关键字字段。如何将它们转换为数字?

我试过了,但它不起作用:

mutate {
        convert => {
             "id" => "integer"
             "sacks_against_total" => "integer"
             "sacks_against_yards" => "integer"
        } 
    }

这是我的 logstash.conf 文件

input {
    jdbc {
        jdbc_connection_string => "jdbc:postgresql://localhost:5432/sportsdb"
        jdbc_user => "user"
        jdbc_password => "password"
        jdbc_driver_class => "org.postgresql.Driver"
        schedule => "*/5 * * * *" 
        statement => "SELECT * FROM public.american_football_sacks_against_stats"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "300"
    }
}

filter {
    mutate {
        convert => {
             "id" => "integer"
             "sacks_against_total" => "integer"
             "sacks_against_yards" => "integer"
        } 
    }
}

output {
    stdout { codec => "json" }
    elasticsearch {
        hosts => "http://localhost:9200"
        index => "sportsdb"
        doc_as_upsert => true #
    }
}

解决方法

Elasticsearch 不提供更改现有字段类型的功能。

您可以在这里阅读一些选项: https://medium.com/@max.borysov/change-field-type-in-elasticsearch-index-2d11bb366517

,

这是我一直在寻找的解决方案: https://linuxhint.com/elasticsearch-reindex-change-field-type/

首先从索引创建输入并更改类型。

PUT _ingest/pipeline/convert_pipeline
{
  “description”: “converts the field sacks_against_total,sacks_against_yards fields to a long from string”,"processors" : [
    {
      "convert" : {
        "field" : "sacks_against_yards","type": "long"
      }
    },{
      "convert" : {
        "field" : "sacks_against_total","type": "long"
      }
    }
  ]
}

或在 cURL 中:

curl -XPUT "http://localhost:9200/_ingest/pipeline/convert_pipeline" -H 'Content-Type: application/json' -d'{  "description": "converts the sacks_against_total field to a long from string","processors" : [    {      "convert" : {        "field" : "sacks_against_total","type": "long"      }    },{"convert" : {        "field" : "sacks_against_yards","type": "long"     } }  ]}'

然后将索引重新索引为另一个索引

POST _reindex
{
  “source”: {
    "index": "american_football_sacks_against_stats"
  },"dest": {
    "index": "american_football_sacks_against_stats_withLong","pipeline": "convert_pipeline"
  }
}

或在 cURL 中:

curl -XPOST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'{  "source": {    "index": "sportsdb"  },"dest": {    "index": "sportsdb_finish","pipeline": "convert_pipeline"  }}'

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?