【问题标题】:ElasticSearch throws string to date Conversion error when importing csv with Logstash使用 Logstash 导入 csv 时,ElasticSearch 抛出字符串到日期转换错误
【发布时间】:2017-05-10 11:13:03
【问题描述】:

我正在尝试使用 Logstash 到 ElasticSearch 运行一个简单的 csv 文件。

但是当我运行它时,将字符串转换为日期格式时出现以下错误(第一个 Date 列)。

"error"=>{
"type"=>"mapper_parsing_exception", 
"reason"=>"failed to parse [Date]",
"caused_by"=>{
    "type"=>"illegal_argument_exception", 
    "reason"=>"Invalid format: \"Date\""}}}}

当我删除日期列时,一切正常。

我正在使用以下 csv 文件:

Date,Open,High,Low,Close,Volume,Adj Close
2015-04-02,125.03,125.56,124.19,125.32,32120700,125.32
2015-04-01,124.82,125.12,123.10,124.25,40359200,124.25
2015-03-31,126.09,126.49,124.36,124.43,41852400,124.43
2015-03-30,124.05,126.40,124.00,126.37,46906700,126.37

以及以下logstash.conf:

input {
  file {
    path => "path/file.csv"
    type => "core2"
    start_position => "beginning"    
  }
}
filter {
  csv {
      separator => ","
      columns => ["Date","Open","High","Low","Close","Volume","Adj Close"]
  }
  mutate {convert => ["High", "float"]}
  mutate {convert => ["Open", "float"]}
  mutate {convert => ["Low", "float"]}
  mutate {convert => ["Close", "float"]}
  mutate {convert => ["Volume", "float"]}
  date {
    match => ["Date", "yyyy-MM-dd"]
    target => "Date"
  }
}
output {  
    elasticsearch {
        action => "index"
        hosts => "localhost"
        index => "stock15"
        workers => 1
    }
    stdout {}
}

看来我处理日期很好。知道可能出了什么问题吗?

谢谢!

【问题讨论】:

    标签: csv elasticsearch logstash logstash-configuration


    【解决方案1】:

    问题出在文件本身。 Logstash 正在读取第一行,无法解析:

    Date,Open,High,Low,Close,Volume,Adj Close
    

    删除文件头不是解决方案:

    2015-04-02,125.03,125.56,124.19,125.32,32120700,125.32
    2015-04-01,124.82,125.12,123.10,124.25,40359200,124.25
    2015-03-31,126.09,126.49,124.36,124.43,41852400,124.43
    2015-03-30,124.05,126.40,124.00,126.37,46906700,126.37
    

    应该没问题的。

    GitHub 上有一个关于此的问题:https://github.com/elastic/logstash/issues/2088

    【讨论】:

      【解决方案2】:

      感谢@Yeikel,我最终更改了logstash 配置,而不是数据本身。

      在应用 csv 过滤器之前,我使用正则表达式检查它是否是标题。因此,如果它是标题,我将其删除,并继续到下一行(将使用 csv 过滤器处理)

      请查看解决标头问题的更新配置:

      input {  
        file {
          path => "path/file.csv"
          start_position => "beginning"    
        }
      }
      filter {  
          if ([message] =~ "\bDate\b") {
              drop { }
          } else {
              csv {
                  separator => ","
                  columns => ["Date","Open","High","Low","Close","Volume","Adj Close"]
              }
              mutate {convert => ["High", "float"]}
              mutate {convert => ["Open", "float"]}
              mutate {convert => ["Low", "float"]}
              mutate {convert => ["Close", "float"]}
              mutate {convert => ["Volume", "float"]}
            date {
              match => ["Date", "yyyy-MM-dd"]
            }
          }
      }
      output {  
          elasticsearch {
              action => "index"
              hosts => "localhost"
              index => "stock15"
              workers => 1
          }
          stdout {
              codec => rubydebug
           }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 1970-01-01
        • 2017-11-16
        • 2019-10-09
        相关资源
        最近更新 更多