Class: Fluent::BigObjectOutput::TableElement

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/fluent/plugin/out_bigobject.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, bo_hostname, bo_port) ⇒ TableElement

Returns a new instance of TableElement.



31
32
33
34
35
36
37
# File 'lib/fluent/plugin/out_bigobject.rb', line 31

def initialize(log, bo_hostname, bo_port)
  super()
  @log = log
  @bo_hostname = bo_hostname
  @bo_port = bo_port
  @bo_url="http://#{@bo_hostname}:#{@bo_port}/cmd"
end

Instance Attribute Details

#mpatternObject (readonly)

Returns the value of attribute mpattern.



29
30
31
# File 'lib/fluent/plugin/out_bigobject.rb', line 29

def mpattern
  @mpattern
end

Instance Method Details

#configure(conf) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/out_bigobject.rb', line 39

def configure(conf)
  super
  if (@table==nil)&&(@schema_file==nil)
    raise "Table name and schema_file cannot be both nil. Please specify <schema_file> if using avro input or <table> is using restful api." 
  end 
  if (isBinary) 
    @avro_schema = Avro::Schema.parse(File.open(@schema_file, "rb").read)
    @avro_writer = Avro::IO::DatumWriter.new(@avro_schema)
  else 
    @avro_schema = nil
    @avro_writer = nil
  end

  @mpattern = Fluent::MatchPattern.create(pattern)
  @mapping = (@column_mapping==nil)? nil:parse_column_mapping(@column_mapping)
  @log.info("column mapping for #{table} - #{@mapping}")
  @format_proc = Proc.new { |record|
    if (@mapping==nil)
      record
    else
      new_record = {}
      @mapping.each { |k, c|
        new_record[c] = record[k]
        }
      new_record
    end
  }
end

#isBinaryObject



68
69
70
# File 'lib/fluent/plugin/out_bigobject.rb', line 68

def isBinary()
  return !(@schema_file.to_s.empty?)
end

#send(chunk) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/fluent/plugin/out_bigobject.rb', line 138

def send(chunk)
    if (isBinary) 
      send_binary(chunk)
    else 
      send_rest(chunk)
    end
end

#send_binary(chunk) ⇒ Object

Send data to Bigobject using binary AVRO



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fluent/plugin/out_bigobject.rb', line 105

def send_binary(chunk)
  
  buffer = StringIO.new()      
  dw = Avro::DataFile::Writer.new(buffer, @avro_writer, @avro_schema)
  i=0
  chunk.msgpack_each { |tag, time, data|
     data = @format_proc.call(data)
     dw<<data
     i+=1
  }
  dw.flush

  begin
    socket = TCPSocket.open(@bo_hostname, @bo_port)
    begin
      #timeout=60
      opt = [1, 60].pack('I!I!')  # { int l_onoff; int l_linger; }
      socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
  
      opt = [60, 0].pack('L!L!')  # struct timeval
      socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, opt)
      socket.write(buffer.string)
    ensure
      socket.close
    end
    
  rescue Exception => e 
      @log.error(e.message)  
      raise "Failed to send_binary: #{e.message}"
  end
  @log.debug("bigobject send #{i} rows")
end

#send_rest(chunk) ⇒ Object

Send Data to Bigobject using Restful API



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fluent/plugin/out_bigobject.rb', line 73

def send_rest(chunk)
  stmts = Array.new
  i=0
  columns = nil
  chunk.msgpack_each { |tag, time, data|
     keys = Array.new
     values = Array.new
     data = @format_proc.call(data)
     data.keys.sort.each do |key|
       keys << key
       values << data[key].to_json
     end
     if columns.to_s.empty?
       columns = "(#{keys.join(",")})"
     end
     stmts.push("(#{values.join(",")})")
     #stmts.push("(\"#{values.join("\",\"")}\")")
     i+=1
  }
  
  sendStmt = "INSERT INTO #{@table}  #{columns} VALUES" + stmts.join(",")
  resp = sendBO(@bo_url, sendStmt)
  parsed = JSON.parse(resp)
  err = parsed['Err']
  if (err.to_s!='')
    @log.error("[BigObject] #{err}")
  end
  @log.debug("bigobject insert #{i} rows")
  
end

#to_sObject



146
147
148
# File 'lib/fluent/plugin/out_bigobject.rb', line 146

def to_s
  "table:#{table}, column_mapping:#{column_mapping}, pattern:#{pattern}"
end