Class: Embulk::OutputRedis

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output_redis.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, schema, index) ⇒ OutputRedis

Returns a new instance of OutputRedis.



26
27
28
29
30
31
# File 'lib/embulk/output_redis.rb', line 26

def initialize(task, schema, index)
  puts "Example output thread #{index}..."
  super
  @records = 0
  @redis = ::Redis.new(:host => task['host'], :port => task['port'], :db => task['db'])
end

Class Method Details

.transaction(config, schema, processor_count, &control) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/embulk/output_redis.rb', line 9

def self.transaction(config, schema, processor_count, &control)
  task = {
    'host' => config.param('host', :string, :default => 'localhost'),
    'port' => config.param('port', :integer, :default => 6379),
    'db' => config.param('db', :integer, :default => 0),
    'key' => config.param('key', :string),
    'key_prefix' => config.param('key_prefix', :string, :default => ''),
    'encode' => config.param('encode', :string, :default => 'json'),
  }

  puts "Redis output started."
  commit_reports = yield(task)
  puts "Redis output finished. Commit reports = #{commit_reports.to_json}"

  return {}
end

Instance Method Details

#abortObject



51
52
# File 'lib/embulk/output_redis.rb', line 51

def abort
end

#add(page) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/embulk/output_redis.rb', line 36

def add(page)
  page.each do |record|
    hash = Hash[schema.names.zip(record)]
      case task['encode']
      when 'json'
        v = hash.to_json
        @redis.set("#{task['key_prefix']}#{hash[task['key']]}", v)
      end
    @records += 1
  end
end

#closeObject



33
34
# File 'lib/embulk/output_redis.rb', line 33

def close
end

#commitObject



54
55
56
57
58
59
# File 'lib/embulk/output_redis.rb', line 54

def commit
  commit_report = {
    "records" => @records
  }
  return commit_report
end

#finishObject



48
49
# File 'lib/embulk/output_redis.rb', line 48

def finish
end