Class: HiveMeta::Table

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/hivemeta/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
# File 'lib/hivemeta/table.rb', line 9

def initialize(name)
  @name = name
  @path = nil
  @indexes   = {} # column indexes by name
  @columns   = [] # column names by index
  @comments  = []
  @delimiter = "\001"
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/hivemeta/table.rb', line 7

def columns
  @columns
end

#commentsObject

Returns the value of attribute comments.



7
8
9
# File 'lib/hivemeta/table.rb', line 7

def comments
  @comments
end

#delimiterObject

Returns the value of attribute delimiter.



7
8
9
# File 'lib/hivemeta/table.rb', line 7

def delimiter
  @delimiter
end

#indexesObject

Returns the value of attribute indexes.



7
8
9
# File 'lib/hivemeta/table.rb', line 7

def indexes
  @indexes
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/hivemeta/table.rb', line 7

def path
  @path
end

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
# File 'lib/hivemeta/table.rb', line 38

def <=>(other)
  self.to_s <=> other.to_s
end

#eachObject Also known as: each_col



22
23
24
25
26
# File 'lib/hivemeta/table.rb', line 22

def each
  @columns.each do |column_name|
    yield column_name if column_name
  end
end

#each_with_indexObject Also known as: each_col_with_index



30
31
32
33
34
# File 'lib/hivemeta/table.rb', line 30

def each_with_index
  @columns.each_with_index do |column_name, index|
    yield column_name, index if column_name
  end
end

#process(opts = {}) ⇒ Object

process all input (default to STDIN for Hadoop Streaming) via a provided block



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hivemeta/table.rb', line 55

def process(opts = {})
  f = opts[:file] || STDIN

  if not block_given?
    return process_row(f.readline, opts)
  end

  f.each_line do |line|
    begin
      process_row(line, opts) {|row| yield row}
    rescue HiveMeta::FieldCountError
      warning = opts[:field_count_warning]
      warning ||= "reporter:counter:HiveMeta,FieldCountError,1"
      STDERR.puts warning
      next
    end
  end
end

#process_row(line, opts = {}) ⇒ Object

process a row and return a record that can be queried by column name in a variety of ways



44
45
46
47
48
49
50
51
# File 'lib/hivemeta/table.rb', line 44

def process_row(line, opts = {})
  return nil if not line
  if block_given?
    yield Record.new(line, self, opts)
  else
    return Record.new(line, self, opts)
  end
end

#to_sObject



18
19
20
# File 'lib/hivemeta/table.rb', line 18

def to_s
  "#{@name}"
end