【问题标题】:Logstash - create terms from relational database group byLogstash - 从关系数据库组中创建术语
【发布时间】:2018-06-21 16:46:52
【问题描述】:

我在 MySQL 中有一个表,我想将其导入 Elasticsearch

例如,数据如下所示

team   buyer
====   ======
one    Q76876
one    Q66567
one    T99898
two    Q45456
two    S77676

我想使用logstash将它导入elasticsearch并创建一个看起来像这样的索引

{
  "id": "one",
  "team": one,
  "buyers": ["Q76876", "Q66567", "T99898"]
},
{
  "id": "two",
  "team": "two",
  "buyers": ["Q45456", "S77676"]
}

我将如何编写我的 .conf 脚本来实现这一点?

【问题讨论】:

    标签: mysql elasticsearch logstash


    【解决方案1】:

    Logstash 将事件放在索引中,除非您应用了一些过滤器。你的情况看起来很简单。如果您格式化您的 sql 查询以按照您需要的格式返回数据,那么您不需要应用任何过滤器,只需连接数据库和 sql 查询即可在 logstash 配置中运行,并将输出作为弹性搜索索引。

    例如:

    MySql 查询看起来像:(我不擅长 mysql,下面只是提供一个想法 - 请验证它是否有效)

    SELECT team as id, 
           team, 
           GROUP_CONCAT(DISTINCT buyer SEPARATOR ', ') as buyers
    FROM tablename GROUP BY team
    

    这将返回如下内容:

    +-----+------+------------------------+
    | id  | team |         buyers         |
    +-----+------+------------------------+
    | one | one  | Q76876, Q66567, T99898 |
    | two | two  | Q45456, S77676         |
    +-----+------+------------------------+
    

    logstash 配置看起来就像:

    input {
      jdbc {
         jdbc_driver_library => "${DATABASE_DRIVER_PATH}"
         jdbc_driver_class => "${DATABASE_DRIVER_PATH}"
         jdbc_connection_string => "{CONNECTIONSTRING}"
         jdbc_user => "${DATABASE_USERNAME}"
         jdbc_password => "${DATABASE_PASSWORD}"
         statement_filepath => "${LOGSTASH_SQL_FILEPATH}" #this will be the sql written above
      }
    }
    
    filter {
    }
    
    output {
        elasticsearch {
            action => "index"       
            hosts => ["${ELASTICSEARCH_HOST}"]
            user => "${ELASTICSEARCH_USER}"
            password => "${ELASTICSEARCH_PASSWORD}"
            index => "${INDEX_NAME}"       
            document_type => "doc"                      
            document_id => "%{id}"       
        }
        stdout { codec => rubydebug }
        stdout { codec => dots }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多