class Jetel::Loaders::Couchbase

Attributes

client[R]

Public Class Methods

new(uri) click to toggle source
Calls superclass method
# 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

Public Instance Methods

load(modul, source, file, opts) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# 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