Class: Mssql2Output

Inherits:
Fluent::BufferedOutput
  • Object
show all
Includes:
Fluent::SetTagKeyMixin, Fluent::SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_mssql2.rb

Overview

require ‘logger’

Instance Method Summary collapse

Instance Method Details

#clientObject



27
28
29
30
31
32
33
34
35
# File 'lib/fluent/plugin/out_mssql2.rb', line 27

def client
  begin
    db = Sequel.tinytds(username: @username, password: @password, host: @host, database: @database)
    # db.loggers << Logger.new($stdout)
  rescue
    raise Fluent::ConfigError, "Cannot open database, check user or password"
  end  
  db
end

#configure(conf) ⇒ Object



18
19
20
21
# File 'lib/fluent/plugin/out_mssql2.rb', line 18

def configure(conf)
  super
  @format_proc = Proc.new{|tag, time, record| record.to_json}
end

#format(tag, time, record) ⇒ Object



23
24
25
# File 'lib/fluent/plugin/out_mssql2.rb', line 23

def format(tag, time, record)
  [tag, time, @format_proc.call(tag, time, record)].to_msgpack
end

#write(chunk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin/out_mssql2.rb', line 37

def write(chunk)    
  a = []
  c = client

  chunk.msgpack_each{|tag, time, data|
    a << JSON.parse(data)
  }

  # pp a
  begin
    c[@table.gsub('.', '__').to_sym].multi_insert(a)    
  ensure
    c.disconnect
  end
end