Class: Embulk::InputRandom

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, schema, index, page_builder) ⇒ InputRandom

Returns a new instance of InputRandom.



41
42
43
# File 'lib/embulk/input/random.rb', line 41

def initialize(task, schema, index, page_builder)
  super
end

Class Method Details

.transaction(config, &control) ⇒ Object



10
11
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
37
38
39
# File 'lib/embulk/input/random.rb', line 10

def self.transaction(config, &control)
  schema = config.param('schema', :hash)
  rows = config.param('rows', :integer, default: 5000)
  threads = config.param('threads', :integer, default: 1)

  columns = schema.each_with_index.map{|column, index|
    attr, type = column
    # TODO: type should more flexible
    case type.downcase
    when "boolean"
      Column.new(index, attr, :boolean)
    when "string"
      Column.new(index, attr, :string)
    when "integer", "int", "long", "primary_key"
      Column.new(index, attr, :long)
    when "double", "float"
      Column.new(index, attr, :double)
    when "date"
      Column.new(index, attr, :timestamp)
    end
  }

  task = {'schema' => schema, 'rows' => rows}

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

  return {}
end

Instance Method Details

#runObject



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
70
71
72
73
74
# File 'lib/embulk/input/random.rb', line 45

def run
  puts "Random generator input thread #{@index}..."
  rows = @task['rows']
  schema = @task['schema']

  rows.times{|n|
    @page_builder.add(schema.map{|attr, type|
                        case type
                        when "string"
                          SecureRandom.urlsafe_base64(32)
                        when "integer", "int", "long"
                          (Random.rand * 10000).to_i
                        when "primary_key"
                          n
                        when 'float', 'double'
                          Random.rand * 10000
                        when 'date'
                          Time.at(rand * Time.now.to_f)
                        else
                          raise "unknown type: #{type}"
                        end
                      })
  }
  @page_builder.finish

  {  # commit report
    "rows" => rows,
    "columns" => schema.size
  }
end