Class: Jetel::Loaders::Couchbase

Inherits:
Loader
  • Object
show all
Defined in:
lib/jetel/loaders/couchbase/couchbase.rb

Instance Attribute Summary collapse

Attributes inherited from Loader

#uri

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Couchbase

Returns a new instance of Couchbase.



36
37
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
# File 'lib/jetel/loaders/couchbase/couchbase.rb', line 36

def initialize(uri)
  super

  tmp = uri.split('://')
  tmp = tmp[1].split('@')

  parts = tmp[0].split(':')
  user = parts[0]
  password = parts[1]

  parts = tmp[1].split('/')
  host, port = parts[0].split(':')
  bucket = parts[1]

  opts = {
    :host => host,
    :port => (port && port.to_i) || 8091,
    # :options => '',
    # :tty => '',
    :bucket => bucket,
    # :username => user,
    # :password => password,
    :connection_timeout => 360e6,
    :timeout => 360e6
  }

  @client = ::Couchbase.connect(opts)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



34
35
36
# File 'lib/jetel/loaders/couchbase/couchbase.rb', line 34

def client
  @client
end

Instance Method Details

#load(modul, source, file, opts) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/jetel/loaders/couchbase/couchbase.rb', line 92

def load(modul, source, file, opts)
  super

  if file =~ /\.csv/
    load_csv(modul, source, file, opts)
  elsif file =~ /\.json/
    load_json(modul, source, file, opts)
  end
end

#load_csv(modul, source, file, opts) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jetel/loaders/couchbase/couchbase.rb', line 65

def load_csv(modul, source, file, opts)
  cache = {}
  CSV.open(file, 'rt', :headers => true, :converters => :all) do |csv|
    csv.each do |row|
      cache[SecureRandom.uuid] = row.to_hash
      if cache.length === 5_000
        client.add(cache)
        cache = {}
        print '.'
      end
    end

    if cache.length > 0
      client.add(cache)
      cache = {}
    end
  end
end

#load_json(modul, source, file, opts) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/jetel/loaders/couchbase/couchbase.rb', line 84

def load_json(modul, source, file, opts)
  uuid = SecureRandom.uuid
  doc = {
    uuid => MultiJson.load(File.read(file))
  }
  client.add(doc)
end