【问题标题】:Creating mapping in Elasticsearch with Fluentd使用 Fluentd 在 Elasticsearch 中创建映射
【发布时间】:2020-03-17 12:52:22
【问题描述】:

我正在尝试使用 Node.js 连接到的 Fluentd 在 Elasticsearch 中创建映射。

Elasticsearch 映射示例:

PUT http://host:9200/test_mapping
{
  "mappings": {
    "properties": {
        "response_code": {
            "type": "text",
            "fielddata": true
        },
        "response_text": {
            "type": "text",
            "fielddata": true
        },
        "status": {
            "type": "boolean"
        },
        "ip": {
            "type": "ip"
        },
        "login": {
            "type": "text",
            "fielddata": true
        }
    }
  }
}

Fluentd 配置示例:

<source>
  @type forward
  port 24225
</source>

<match mapping.doc>
  @type elasticsearch
  logstash_format "#{ENV['LOGSTASH_FORMAT']}"
  scheme "#{ENV['SCHEME']}"
  host "#{ENV['HOST']}"
  port "#{ENV['PORT']}"
  write_operation index
  index_name "#{ENV['INDEX_NAME']}"
  flush_interval "#{ENV['FLUSH_INTERVAL']}"
</match>

Node.js 上的示例代码:

// ...
require('dotenv').config();
const env = process.env;
const loggerfluentd = require('fluent-logger');
loggerfluentd.configure('mapping', {
            host: env.FLUENTD_HOST,
            port: Number.parseInt(env.FLUENTD_PORT),
            timeout: 3.0,
            reconnectInterval: 10000 // 10 sec
        });

function EmitMapping(data) {
    loggerfluentd.emit(env.INDEX_NAME, data);
}

exports.EmitMapping = EmitMapping;

此配置不会创建映射,只是将新文档添加到 Elasticsearch。

是否可以通过执行 EmitMapping() 函数来更改配置,以便不添加新文档(映射中自动分配的数据类型),即使用自己的数据类型创建自己的映射?

【问题讨论】:

  • 如何在 fluentd 中运行 node.js 脚本?这个脚本会在每个日志插入中运行吗?如果它会在 fluentd conf 文件中配置?

标签: node.js elasticsearch logging fluentd


【解决方案1】:

elasticsearch 插件有可能不创建并且不更改索引,而只是写入索引,因此我使用了 http 插件:

<match mapping.doc>
  @type http
  endpoint "#{ENV['SCHEME']}://#{ENV['HOST']}:#{ENV['PORT']}/#{ENV['INDEX_NAME']}"
  http_method put
  headers {"Content-Type":"application/json"}
  open_timeout 2
  <buffer>
    flush_interval "#{ENV['FLUSH_INTERVAL']}"
  </buffer>
</match>

【讨论】:

  • 我认为这是在记录之前创建映射的好选择。但是你怎么能保证它在插入任何文档之前只工作一次呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
相关资源
最近更新 更多