You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Logstash jdbc输入插件生成多值数组类型字段

你需要在Logstash pipeline中新增filter阶段,使用mutate过滤器将逗号分隔的字符串切割为数组,如有需要可以同步转换字段类型为数值,完整配置参考如下:

input {
  jdbc {
    jdbc_driver_library => ""
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:............."
    jdbc_user => ""
    jdbc_password => ""
    schedule => "* * * * *"
    statement => "SELECT songid,name,name_pinyin,artistid,artist_name,'1,2,3' as tagids, '4,5,6' as categories,create_at from song"
  }
}

filter {
  # 切割逗号分隔字符串为数组
  mutate {
    split => {
      "tagids" => ","
      "categories" => ","
    }
    # 如果需要数组元素为数值类型而非字符串,添加以下转换配置
    convert => {
      "tagids" => "integer"
      "categories" => "integer"
    }
  }
}

output {
  elasticsearch {
    hosts => ["你的ES服务地址"]
    index => "song"
    document_id => "%{songid}"
  }
}

补充说明:

  • 仅执行split操作得到的是字符串类型数组,比如["1","2","3"],如果你的ES索引Mapping中对应字段定义为数值数组,就需要加上convert配置,将数组元素转为整型,得到[1,2,3]的效果。
  • 如果你的MySQL中tagids、categories本身是关联表存储的多值,也可以直接修改SQL用GROUP_CONCAT拼接为逗号分隔字符串,再用上述方法切割,不需要修改现有表结构。

内容的提问来源于stack exchange,提问作者LF-DevJourney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 22:36:05