Class: Embulk::InputRedis

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/redis.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guessObject



9
10
# File 'lib/embulk/input/redis.rb', line 9

def self.guess
end

.transaction(config, &control) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/embulk/input/redis.rb', line 12

def self.transaction(config, &control)
  task = {
    'host' => config.param('host', :string, :default => 'localhost'),
    'port' => config.param('port', :integer, :default => 6379),
    'db' => config.param('db', :integer, :default => 0),
    'key_prefix' => config.param('key_prefix', :string, :default => ''),
    'encode' => config.param('encode', :string, :default => 'json'),
    'columns' =>
      config.param('columns', :array, :default => []).inject({}){|a, col|
        a[col['name']] = col['type'].to_sym
        a
      },
    'rows' => 0
  }

  columns = task['columns'].map.with_index{|(name, type), i|
    Column.new(i, name, type)
  }

  puts "Redis input started."
  commit_reports = yield(task, columns, 1)
  puts "Redis input finished. Commit reports = #{commit_reports.to_json}"

  return {}
end

Instance Method Details

#deserialize_element(x) ⇒ Object



38
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
66
67
68
69
# File 'lib/embulk/input/redis.rb', line 38

def deserialize_element(x)
  @task['columns'].map{|(name, type)|
    begin
      val = x[name]
      case type.to_sym  # Converted to String implicitly?
      when :boolean
        if val.is_a?(TrueClass) || val.is_a?(FalseClass)
          val
        else
          downcased_val = val.downcase
          case downcased_val
          when 'true' then true
          when 'false' then false
          else nil
          end
        end
      when :long
        Integer(val)
      when :double
        Float(val)
      when :string
        val
      when :timestamp
        Time.parse(val)
      else
        raise "Shouldn't reach here: val:#{val}, col_name:#{name}, col_type:#{type}"
      end
    rescue => e
      STDERR.puts "Failed to deserialize: val:#{val}, col_name:#{name}, col_type:#{type}, error:#{e.inspect}"
    end
  }
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/embulk/input/redis.rb', line 71

def run
  puts "Redis input thread #{@index}..."
  r = Redis.new(:host => @task['host'], :port => @task['port'], :db => @task['db'])
  r.keys("#{@task['key_prefix']}*").each do |k|
    case @task['encode']
    when 'json'
      v = r.get(k)
      x = JSON.parse(v)
      @page_builder.add(deserialize_element(x))
    when 'hash'
      x = r.hgetall(k)
      @page_builder.add(deserialize_element(x))
    end
    @task['rows'] += 1
  end
  @page_builder.finish  # don't forget to call finish :-)

  commit_report = {
    "rows" => @task['rows']
  }
  return commit_report
end