Class: TreasureData::FileReader
- Inherits:
-
Object
- Object
- TreasureData::FileReader
- Defined in:
- lib/td/file_reader.rb
Overview
json and msgpack format supports array types with columns
-
when –column-header option
- “a”, “b”, “c”
-
# first line is header
- “v”, 10, true
-
# array types, e.g. generate => “v”, “b” => 10, “c” => true
…
-
when –columns a,b,c
- “v”, 10, true
-
# array types
…
Defined Under Namespace
Modules: RegexpParserMixin Classes: ApacheParser, AutoTypeConvertParserFilter, DecompressIOFilter, DelimiterParser, HashBuilder, JSONParser, LineReader, MessagePackParsingReader, SeparatedValueParsingReader, SetTimeParserFilter, SyslogParser, TimeParserFilter
Instance Attribute Summary collapse
-
#default_opts ⇒ Object
readonly
Returns the value of attribute default_opts.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#parser_class ⇒ Object
Returns the value of attribute parser_class.
Instance Method Summary collapse
- #compose_factory ⇒ Object
- #init_optparse(op) ⇒ Object
-
#initialize ⇒ FileReader
constructor
A new instance of FileReader.
- #parse(io, error, &block) ⇒ Object
- #set_format_template(name) ⇒ Object
Constructor Details
#initialize ⇒ FileReader
Returns a new instance of FileReader.
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/td/file_reader.rb', line 302 def initialize @format = "text" @default_opts = { :delimiter_expr => /\t|,/, #:line_delimiter_expr => /\r?\n/, :null_expr => /\A(?:null||\-|\\N)\z/i, :true_expr => /\A(?:true)\z/i, :false_expr => /\A(?:false)\z/i, #:quote_char => "\"", } @opts = {} @parser_class = nil end |
Instance Attribute Details
#default_opts ⇒ Object (readonly)
Returns the value of attribute default_opts.
316 317 318 |
# File 'lib/td/file_reader.rb', line 316 def default_opts @default_opts end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
316 317 318 |
# File 'lib/td/file_reader.rb', line 316 def opts @opts end |
#parser_class ⇒ Object
Returns the value of attribute parser_class.
317 318 319 |
# File 'lib/td/file_reader.rb', line 317 def parser_class @parser_class end |
Instance Method Details
#compose_factory ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/td/file_reader.rb', line 404 def compose_factory opts = @default_opts.merge(@opts) case @format when 'text' Proc.new {|io,error| io = DecompressIOFilter.filter(io, error, opts) reader = LineReader.new(io, error, opts) parser = DelimiterParser.new(reader, error, opts) if opts[:column_header] column_names = parser.forward elsif opts[:column_names] column_names = opts[:column_names] else raise "--column-header or --columns option is required" end unless opts[:all_string] parser = AutoTypeConvertParserFilter.new(parser, error, opts) end parser = HashBuilder.new(parser, error, column_names) if opts[:time_value] parser = SetTimeParserFilter.new(parser, error, opts) else parser = TimeParserFilter.new(parser, error, opts) end } when 'apache', 'syslog' Proc.new {|io,error| io = DecompressIOFilter.filter(io, error, opts) reader = LineReader.new(io, error, opts) parser = if @format == 'apache' ApacheParser.new(reader, error, opts) else SyslogParser.new(reader, error, opts) end if opts[:column_names] column_names = opts[:column_names] else raise "--columns option is required" end unless opts[:all_string] parser = AutoTypeConvertParserFilter.new(parser, error, opts) end parser = HashBuilder.new(parser, error, column_names) if opts[:time_value] parser = SetTimeParserFilter.new(parser, error, opts) else parser = TimeParserFilter.new(parser, error, opts) end } when 'json' Proc.new {|io,error| io = DecompressIOFilter.filter(io, error, opts) reader = LineReader.new(io, error, opts) parser = JSONParser.new(reader, error, opts) if opts[:column_header] column_names = parser.forward elsif opts[:column_names] column_names = opts[:column_names] end if column_names parser = HashBuilder.new(parser, error, column_names) end if opts[:time_value] parser = SetTimeParserFilter.new(parser, error, opts) else parser = TimeParserFilter.new(parser, error, opts) end } when 'msgpack' Proc.new {|io,error| io = DecompressIOFilter.filter(io, error, opts) parser = MessagePackParsingReader.new(io, error, opts) if opts[:column_header] column_names = parser.forward elsif opts[:column_names] column_names = opts[:column_names] end if column_names parser = HashBuilder.new(parser, error, column_names) end if opts[:time_value] parser = SetTimeParserFilter.new(parser, error, opts) else parser = TimeParserFilter.new(parser, error, opts) end } end end |
#init_optparse(op) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/td/file_reader.rb', line 319 def init_optparse(op) op.on('-f', '--format NAME', "source file format [csv, tsv, msgpack, json]") {|s| set_format_template(s) } op.on('-h', '--columns NAME,NAME,...', 'column names (use --column-header instead if the first line has column names)') {|s| @opts[:column_names] = s.split(',') } op.on('-H', '--column-header', 'first line includes column names', TrueClass) {|b| @opts[:column_header] = b } op.on('-d', '--delimiter REGEX', "delimiter between columns (default: #{@default_opts[:delimiter_expr].to_s})") {|s| @opts[:delimiter_expr] = Regexp.new(s) } #op.on('-D', '--line-delimiter REGEX', "delimiter between rows (default: #{@default_opts[:line_delimiter_expr].inspect[1..-2]})") {|s| # @opts[:line_delimiter_expr] = Regexp.new(s) #} op.on('--null REGEX', "null expression for the automatic type conversion (default: #{@default_opts[:null_expr].to_s})") {|s| @opts[:null_expr] = Regexp.new(s) } op.on('--true REGEX', "true expression for the automatic type conversion (default: #{@default_opts[:true_expr].to_s})") {|s| @opts[:true_expr] = Regexp.new(s) } op.on('--false REGEX', "false expression for the automatic type conversion (default: #{@default_opts[:false_expr].to_s})") {|s| @opts[:false_expr] = Regexp.new(s) } # TODO #op.on('-E', '--escape CHAR', "escape character (default: no escape character)") {|s| # @opts[:escape_char] = s #} #op.on('-Q', '--quote CHAR', "quote character (default: #{@default_opts[:quote_char]}") {|s| # @opts[:quote_char] = s #} op.on('-S', '--all-string', 'disable automatic type conversion', TrueClass) {|b| @opts[:all_string] = b } op.on('-t', '--time-column NAME', 'name of the time column') {|s| @opts[:time_column] = s } op.on('-T', '--time-format FORMAT', 'strftime(3) format of the time column') {|s| @opts[:time_format] = s } op.on('--time-value TIME', 'value of the time column') {|s| if s.to_i.to_s == s @opts[:time_value] = s.to_i else require 'time' @opts[:time_value] = Time.parse(s).to_i end } op.on('-e', '--encoding NAME', "text encoding") {|s| @opts[:encoding] = s } op.on('-C', '--compress NAME', 'compression format name [plain, gzip] (default: auto detect)') {|s| @opts[:compress] = s } end |
#parse(io, error, &block) ⇒ Object
496 497 498 499 500 501 502 503 504 505 |
# File 'lib/td/file_reader.rb', line 496 def parse(io, error, &block) factory = compose_factory parser = factory.call(io, error) begin while record = parser.forward block.call(record) end rescue EOFError end end |
#set_format_template(name) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/td/file_reader.rb', line 376 def set_format_template(name) case name when 'csv' @format = 'text' @opts[:delimiter_expr] = /,/ when 'tsv' @format = 'text' @opts[:delimiter_expr] = /\t/ when 'apache' @format = name @opts[:column_names] = ApacheParser::COLUMNS @opts[:null_expr] = /\A(?:\-|)\z/ @opts[:time_column] = 'time' @opts[:time_format] = ApacheParser::TIME_FORMAT when 'syslog' @format = name @opts[:column_names] = SyslogParser::COLUMNS @opts[:time_column] = 'time' @opts[:time_format] = SyslogParser::TIME_FORMAT when 'msgpack' @format = 'msgpack' when 'json' @format = 'json' else raise "Unknown format: #{name}" end end |