将MySQL值转换为建议字段

时间:2017-08-08 20:34:23

标签: elasticsearch logstash logstash-jdbc

在这里大声呼救,当我尝试使用logstash将MySQL值转换为嵌套的elasticsearch字段时,我收到以下错误。

{"exception"=>"expecting List or Map, found class org.logstash.bivalues.StringBiValue", "backtrace"=>["org.logstash.Accessors.newCollectionException(Accessors.java:195)"

使用以下配置文件:

input {
    jdbc {
        jdbc_driver_library => "/logstash/mysql-connector-java-5.1.42-bin.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string => "jdbc:mysql://localhost:3306/data"
        jdbc_user => "username"
        jdbc_password => "password"
        statement => "SELECT id, suggestions, address_count FROM `suggestions` WHERE id <= 100"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
    }
}
filter {
  mutate {
  rename => { 'address_count' => '[suggestions][payload][count]' }
  }
}
output {
    elasticsearch {
    hosts => [
        "localhost:9200"
    ]
        index => "dev_suggestions"
        document_type => "address"
    }
}

但是,如果我将address_count重命名为我的映射中尚未存在的字段,那么它工作得很好并且它正确地将值添加为嵌套属性,我已尝试过我的其他字段索引,而不仅仅是suggestions.payloads.address_count,我得到同样的问题,只有在映射中没有定义字段时才有效。

这给我带来了一些严重的麻烦,如果有人能帮助我解决这个问题,我会非常感激,因为我花了最近48个小时在桌子上敲我的头!

我最初假设我可以使用MySQL查询执行以下操作:

SELECT id, suggestion, '[suggestions][payload][count]' FROM `suggestions` WHERE id <= 100

然后我也尝试了

SELECT id, suggestion, 'suggestions.payload.count' FROM `suggestions` WHERE id <= 100

两者都未能使用后面的选项插入值,导致字段不能包含点的错误。

最后是映射:

{
  "mappings": {
    "address": {
      "properties": {
        "suggestions": {
          "type": "completion",
          "payloads" : true
        }
      }
    }
  }
}

感谢Val - 对于与我相同情况的未来用户,需要使用logstash将MySQL数据转换为嵌套的Elasticsearch对象,这是一个使用Logstash 5和Elasticsearch的工作解决方案2。*

input {
    jdbc {
        jdbc_driver_library => "/logstash/mysql-connector-java-5.1.42-bin.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string => "jdbc:mysql://localhost:3306/data"
        jdbc_user => "username"
        jdbc_password => "password"
        statement => "SELECT addrid, suggestion, address_count FROM `suggestions` WHERE id <= 20"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
    }
}

filter {
  ruby {
       code => "
           event.set('[suggestions][input]', event.get('suggestion'))
           event.set('[suggestions][payload][address_count]', event.get('address_count'))
           event.set('[v][payload][id]', event.get('addrid'))
       "
       remove_field => [ 'suggestion', 'address_count', 'addrid' ]
  }
}

output {
    elasticsearch {
        hosts => [
            "localhost:9200"
        ]
        index => "dev_suggestions"
        document_type => "address"
    }
}

1 个答案:

答案 0 :(得分:3)

我认为你需要以不同的方式行事。首先,我将SQL查询中的suggestions字段重命名为其他字段,然后根据从SQL查询中获取的值构建suggestions对象。

    statement => "SELECT id, suggestion, address_count FROM `suggestions` WHERE id <= 100"

然后您可以使用ruby过滤器(并删除mutate个过滤器)来构建suggestions字段,如下所示:

Logstash 2.x代码:

ruby {
     code => "
         event['suggestions']['input'] = event['suggestion']
         event['suggestions']['payload']['count'] = event['address_count']
     "
     remove_field => [ 'suggestion', 'address_count' ]
}

Logstash 5.x代码:

ruby {
     code => "
         event.set('[suggestions][input]', event.get('suggestion'))
         event.set('[suggestions][payload][count]', event.get('address_count'))
     "
     remove_field => [ 'suggestion', 'address_count' ]
}

PS:所有这些假设您使用的是ES 2.x,因为payload字段已在ES 5.x中消失