Class: Fluent::CouchOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::CouchOutput
- Includes:
- SetTagKeyMixin, SetTimeKeyMixin
- Defined in:
- lib/fluent/plugin/out_couch.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ CouchOutput
constructor
A new instance of CouchOutput.
- #shutdown ⇒ Object
- #start ⇒ Object
- #update_docs(records) ⇒ Object
- #update_view_index ⇒ Object
-
#write(chunk) ⇒ Object
todo : this sucks.
- #write_by_key(chunk) ⇒ Object
- #write_by_key_per(chunk) ⇒ Object
- #write_default(chunk) ⇒ Object
Constructor Details
#initialize ⇒ CouchOutput
Returns a new instance of CouchOutput.
30 31 32 33 34 35 36 37 38 |
# File 'lib/fluent/plugin/out_couch.rb', line 30 def initialize super require 'msgpack' require 'jsonpath' Encoding.default_internal = 'UTF-8' require 'couchrest' Encoding.default_internal = 'ASCII-8BIT' end |
Instance Method Details
#configure(conf) ⇒ Object
41 42 43 |
# File 'lib/fluent/plugin/out_couch.rb', line 41 def configure(conf) super end |
#format(tag, time, record) ⇒ Object
94 95 96 |
# File 'lib/fluent/plugin/out_couch.rb', line 94 def format(tag, time, record) record.to_msgpack end |
#shutdown ⇒ Object
89 90 91 |
# File 'lib/fluent/plugin/out_couch.rb', line 89 def shutdown super end |
#start ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fluent/plugin/out_couch.rb', line 46 def start super if @db_key or @db_per raise "update_docs is not compatible with db_key or db_per yet!" if @update_docs raise "refresh_view_index is not compatible with db_key or db_per yet!" if @refresh_view_index end @account = "#{@user}:#{@password}@" if @user && @password @db = {} # # todo : doesn't work # if @db_per and @db_key @write = self.method :write_by_key_per elsif @db_per raise "todo: do not currenty support db_per without db_key" elsif @db_key @write = self.method :write_by_key else @write = self.method :write_default end if not @db_key or @db_per @db[:default] = CouchRest.database!("#{@protocol}://#{@account}#{@host}:#{@port}/#{@database}") end @views = [] if @refresh_view_index begin @db[:default].get("_design/#{@refresh_view_index}")['views'].each do |view_name, func| @views.push([@refresh_view_index, view_name]) end rescue $log.error 'design document not found!' end end end |
#update_docs(records) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/fluent/plugin/out_couch.rb', line 183 def update_docs(records) if records.length > 0 records.each do |record| doc = nil begin doc = @db[:default].get(record['_id']) rescue end record['_rev']=doc['_rev'] unless doc.nil? $log.debug record @db[:default].save_doc(record) end end end |
#update_view_index ⇒ Object
202 203 204 205 206 |
# File 'lib/fluent/plugin/out_couch.rb', line 202 def update_view_index() @views.each do |design, view| @db[:default].view("#{design}/#{view}", {'limit' => '0'}) end end |
#write(chunk) ⇒ Object
todo : this sucks
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fluent/plugin/out_couch.rb', line 100 def write(chunk) if @db_per and @db_key write_by_key_per chunk elsif @db_per raise "todo: do not currenty support db_per without db_key" elsif @db_key write_by_key chunk else write_default chunk end end |
#write_by_key(chunk) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/fluent/plugin/out_couch.rb', line 159 def write_by_key(chunk) records ={} chunk.msgpack_each do |record| id = record[@doc_key_field] id = JsonPath.new(@doc_key_jsonpath).first(record) if id.nil? && !@doc_key_jsonpath.nil? record['_id'] = id unless id.nil? key = record[@db_key] records[key] = [] unless records[key] records[key] << record end records.each_pair do |key, data| if not @db[key] @db[key] = CouchRest.database!("#{@protocol}://#{@account}#{@host}:#{@port}/#{@database+"_"+key}") end @db[key].bulk_save(data) end end |
#write_by_key_per(chunk) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fluent/plugin/out_couch.rb', line 132 def write_by_key_per(chunk) records = {} chunk.msgpack_each do |record| id = record[@doc_key_field] id = JsonPath.new(@doc_key_jsonpath).first(record) if id.nil? && !@doc_key_jsonpath.nil? record['_id'] = id unless id.nil? date = record['dt'] key = "#{record[@db_key].downcase}_#{date[0]}#{date[1].to_s.rjust(2, '0')}#{date[2].to_s.rjust(2, '0')}" records[key] = [] unless records[key] records[key] << record end records.each_pair do |key, data| if not @db[key] @db[key] = CouchRest.database!("#{@protocol}://#{@account}#{@host}:#{@port}/#{@database+"_"+key}") end @db[key].bulk_save(data) end end |
#write_default(chunk) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fluent/plugin/out_couch.rb', line 111 def write_default(chunk) records = [] chunk.msgpack_each do |record| id = record[@doc_key_field] id = JsonPath.new(@doc_key_jsonpath).first(record) if id.nil? && !@doc_key_jsonpath.nil? record['_id'] = id unless id.nil? records << record end if @update_docs update_docs(records) else @db[:default].bulk_save(records) end update_view_index end |