Class: Fluent::Plugin::Sqlite3Output
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::Sqlite3Output
- Defined in:
- lib/fluent/plugin/out_sqlite3.rb
Constant Summary collapse
- DEFAULT_BUFFER_TYPE =
"memory"
- DELIMITER =
/ *, */
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
- #formatted_to_msgpack_binary? ⇒ Boolean
-
#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.
21 22 23 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 21 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 25 def configure(conf) compat_parameters_convert(conf, :buffer) 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
58 59 60 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 58 def format(tag, time, record) [tag, time, record].to_msgpack end |
#formatted_to_msgpack_binary? ⇒ Boolean
74 75 76 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 74 def formatted_to_msgpack_binary? true end |
#shutdown ⇒ Object
51 52 53 54 55 56 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 51 def shutdown log.debug "shutdown" @stmts.each {|k,v| v.close} @db.close super end |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 36 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
46 47 48 49 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 46 def to_insert(table, columns) cols = columns.split(DELIMITER).map {|e| ":#{e}"}.join(",") "INSERT INTO #{table}(#{columns}) VALUES(#{cols})" end |
#write(chunk) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 62 def write(chunk) @db.transaction begin write1(chunk) @db.commit rescue => ex @db.rollback log.error "rollback: ", ex raise end end |
#write1(chunk) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/fluent/plugin/out_sqlite3.rb', line 78 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.split('.')[1]) # param 'table' or 2nd later part of tag. 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 |