-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add null_value_pattern and null_empty_string option for LabeledTSVParser, CSVParser, TSVParser. #657
Add null_value_pattern and null_empty_string option for LabeledTSVParser, CSVParser, TSVParser. #657
Changes from 3 commits
863357f
78a7bd6
e884191
1227ebe
a4487a9
032f640
0ec1c53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,6 +295,8 @@ class ValuesParser < Parser | |
end | ||
config_param :time_key, :string, :default => nil | ||
config_param :time_format, :string, :default => nil | ||
config_param :null_value_pattern, :string, :default => nil | ||
config_param :null_empty_string, :bool, :default => false | ||
|
||
def configure(conf) | ||
super | ||
|
@@ -308,11 +310,16 @@ def configure(conf) | |
end | ||
|
||
@time_parser = TimeParser.new(@time_format) | ||
|
||
if @null_value_pattern | ||
@null_value_pattern = /#{@null_value_pattern}/ | ||
end | ||
|
||
@mutex = Mutex.new | ||
end | ||
|
||
def values_map(values) | ||
record = Hash[keys.zip(values)] | ||
record = Hash[keys.zip(values.map { |value| convert_value_to_nil(value) })] | ||
|
||
if @time_key | ||
value = @keep_time_key ? record[@time_key] : record.delete(@time_key) | ||
|
@@ -345,6 +352,16 @@ def convert_field_type!(record) | |
end | ||
} | ||
end | ||
|
||
def convert_value_to_nil(value) | ||
if @null_empty_string | ||
value = (value == '') ? nil : value | ||
end | ||
if @null_value_pattern | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
value = (value =~ @null_value_pattern) ? nil : value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String scrub is needed? cf. http://blog.livedoor.jp/sonots/archives/34702351.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually does String#scrub in my plugin as https://github.com/fluent/fluentd/blob/master/lib/fluent/plugin/filter_grep.rb#L73-L83. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry. I think this private method add ValuesParser, Is it OK? def match(regexp, string)
begin
return regexp.match(string)
rescue ArgumentError => e
raise e unless e.message.index("invalid byte sequence in".freeze).zero?
log.info "invalid byte sequence is replaced in `#{string}`"
string = string.scrub('?')
retry
end
return true
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating Fluent::StringUtil should be better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah OK! |
||
end | ||
value | ||
end | ||
end | ||
|
||
class TSVParser < ValuesParser | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regexp.new
is better because users do not need to care of escaping/
with it.