mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
allow custom separator in csv filter
This commit is contained in:
parent
63ef8b4f36
commit
aa4c29a866
2 changed files with 23 additions and 2 deletions
|
@ -23,12 +23,17 @@ class LogStash::Filters::CSV < LogStash::Filters::Base
|
|||
# Optional.
|
||||
config :fields, :validate => :array, :default => []
|
||||
|
||||
# Define the column separator value. If this is not specified the default
|
||||
# is a comma ','
|
||||
# Optional.
|
||||
config :col_sep, :validate => :string, :default => ","
|
||||
|
||||
public
|
||||
def register
|
||||
@csv = {}
|
||||
|
||||
@config.each do |field, dest|
|
||||
next if (RESERVED + ["fields"]).member?(field)
|
||||
next if (RESERVED + ["fields", "col_sep"]).member?(field)
|
||||
@csv[field] = dest
|
||||
end
|
||||
|
||||
|
@ -60,7 +65,7 @@ class LogStash::Filters::CSV < LogStash::Filters::Base
|
|||
|
||||
raw = event[key].first
|
||||
begin
|
||||
values = CSV.parse_line(raw)
|
||||
values = CSV.parse_line(raw, {:col_sep => @col_sep})
|
||||
data = {}
|
||||
values.each_index do |i|
|
||||
field_name = @fields[i] || "field#{i+1}"
|
||||
|
@ -82,3 +87,4 @@ class LogStash::Filters::CSV < LogStash::Filters::Base
|
|||
@logger.debug("Event after csv filter", :event => event)
|
||||
end # def filter
|
||||
end # class LogStash::Filters::Csv
|
||||
|
||||
|
|
|
@ -38,6 +38,21 @@ describe LogStash::Filters::CSV do
|
|||
end
|
||||
end
|
||||
|
||||
describe "custom separator" do
|
||||
config <<-CONFIG
|
||||
filter {
|
||||
csv {
|
||||
col_sep => ";"
|
||||
}
|
||||
}
|
||||
CONFIG
|
||||
|
||||
sample "big,bird;sesame street" do
|
||||
insist { subject["field1"] } == "big,bird"
|
||||
insist { subject["field2"] } == "sesame street"
|
||||
end
|
||||
end
|
||||
|
||||
describe "parse csv with more data than defined field names" do
|
||||
config <<-CONFIG
|
||||
filter {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue