Class: Fluent::Sqlite3Output
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::Sqlite3Output
- Defined in:
- lib/fluent/plugin/out_sqlite3.rb
Constant Summary collapse
- DELIMITER =
/ *, */
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ Sqlite3Output
constructor
A new instance of Sqlite3Output.
- #shutdown ⇒ Object
- #start ⇒ Object
- #to_insert(table, columns) ⇒ Object
- #write(chunk) ⇒ Object
- #write1(chunk) ⇒ Object
Constructor Details
#initialize ⇒ Sqlite3Output
Returns a new instance of Sqlite3Output.
12 13 14 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 12 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 16 def configure(conf) super @type = conf["type"] if (@table and not(@columns)) or (not(@table) and @columns) raise "strict mode requires table and columns parameters" end end |
#format(tag, time, record) ⇒ Object
48 49 50 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 48 def format(tag, time, record) [tag, time, record].to_msgpack end |
#shutdown ⇒ Object
41 42 43 44 45 46 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 41 def shutdown super $log.debug "shutdown" @stmts.each {|k,v| v.close} @db.close end |
#start ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 26 def start super @db = ::SQLite3::Database.new @path @stmts = {} if @table cols = @columns.split(DELIMITER).map {|e| ":#{e}"}.join(",") @stmts[@table] = @db.prepare "INSERT INTO #{@table}(#{@columns}) VALUES(#{cols})" end end |
#to_insert(table, columns) ⇒ Object
36 37 38 39 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 36 def to_insert(table, columns) cols = columns.split(DELIMITER).map {|e| ":#{e}"}.join(",") "INSERT INTO #{table}(#{columns}) VALUES(#{cols})" end |
#write(chunk) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 52 def write(chunk) @db.transaction begin write1(chunk) @db.commit rescue => ex @db.rollback $log.error "rollback: ", ex end end |
#write1(chunk) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 63 def write1(chunk) chunk.msgpack_each do |tag, time, record| if record.keys.length == 0 $log.warn "no any keys for #{tag}" return end table = (@table or tag.slice(@type.length + 1, tag.length)) if @includes (record.keys - @includes.split(DELIMITER)).each {|e| record.delete e} end if @excludes @excludes.split(DELIMITER).each {|e| record.delete e} end unless @stmts[table] cols = record.keys.join "," @db.execute "CREATE TABLE IF NOT EXISTS #{table} (id INTEGER PRIMARY KEY AUTOINCREMENT,#{cols})" @stmts[table] = @db.prepare (a = to_insert(table, cols)) $log.debug "create a new table, #{table.upcase} (it may have been already created)" end @stmts[table].execute record end end |