Class: Embulk::OutputRedisWithExpires

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, schema, index) ⇒ OutputRedisWithExpires

Returns a new instance of OutputRedisWithExpires.



28
29
30
31
32
33
34
# File 'lib/embulk/output/redisWithExpires.rb', line 28

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

Class Method Details

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



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

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'),
    'expires' => config.param('expires', :integer, :default => 0)
  }

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

  return {}
end

Instance Method Details

#abortObject



70
71
# File 'lib/embulk/output/redisWithExpires.rb', line 70

def abort
end

#add(page) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/embulk/output/redisWithExpires.rb', line 39

def add(page)
  page.each do |record|
    hash = Hash[schema.names.zip(record)]
    k = "#{task['key_prefix']}#{hash[task['key']]}"
    unless @unique_keys.include? k
      case task['encode']
      when 'json'
        v = hash.to_json
        if task['expires'] > 0
          @redis.set(k, v, ex: task['expires'])
        else
          @redis.set(k, v)
        end
      when 'hash'
        if task['expires'] > 0
          @redis.hmset(k, hash.to_a.flatten, ex: task['expires'])
        else
          @redis.hmset(k, hash.to_a.flatten)
        end
      end
      @unique_keys << k
    else
      puts "Warning: #{k} is already exists"
    end
    @rows += 1  # inrement anyway
  end
end

#closeObject



36
37
# File 'lib/embulk/output/redisWithExpires.rb', line 36

def close
end

#commitObject



73
74
75
76
77
78
79
# File 'lib/embulk/output/redisWithExpires.rb', line 73

def commit
  commit_report = {
    "rows" => @rows,
    "unique_keys" => @unique_keys.size
  }
  return commit_report
end

#finishObject



67
68
# File 'lib/embulk/output/redisWithExpires.rb', line 67

def finish
end