Sha256: 5981ece4b9980bb7b0e2d226271867f5158eb5ed2ca244949d4ae86d372c2711

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

require_relative '../loader'

require_relative '../../helpers/helpers'

require 'couchbase'

require 'securerandom'

module Jetel
  module Loaders
    class Couchbase < Loader
      attr_reader :client

      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

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

        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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jetel-0.0.15 lib/jetel/loaders/couchbase/couchbase.rb
jetel-0.0.14 lib/jetel/loaders/couchbase/couchbase.rb
jetel-0.0.12 lib/jetel/loaders/couchbase/couchbase.rb
jetel-0.0.11 lib/jetel/loaders/couchbase/couchbase.rb
jetel-0.0.10 lib/jetel/loaders/couchbase/couchbase.rb