使用 Fluentd 在 Elasticsearch 中创建映射

Creating mapping in Elasticsearch with Fluentd

我正在尝试使用 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 () 函数不添加新文档(在映射中自动分配数据类型),即使用自己的数据类型创建自己的映射?

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>