public
Last active

  • Download Gist
csv_to_couch.rb
Ruby
1 2 3 4 5 6 7 8 9 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/ruby
 
# csv_to_couch.rb - Simple script which plonks CSV files into CouchDB
# (C) 2009 James S Urquhart (jamesu at gmail dot com)
# Licensed under MIT
 
require 'rubygems'
require 'fastercsv'
require 'couchrest'
 
DOCBUFFER_SIZE = 1024 # number of entries to send per-batch
 
rows = {}
 
puts "#{ARGV[0]} -> #{ARGV[1]}..."
 
@db = CouchRest.database!("http://127.0.0.1:5984/#{ARGV[1]}")
@doc_fields = []
@doc_buffer = []
first = true
 
# Output
unless @db.nil?
FasterCSV.foreach(ARGV[0], :col_sep => ',') do |row|
# Header
if first
@doc_fields = []
row.each {|v| @doc_fields << v}
first = false
next
end
new_doc = {}
count = 0
for value in row
next if count >= @doc_fields.length
new_doc[@doc_fields[count]] = value
count += 1
end
@doc_buffer << new_doc
if @doc_buffer.length >= DOCBUFFER_SIZE
@db.bulk_save(@doc_buffer)
@doc_buffer = []
end
end
end
 
if @doc_buffer.length > 0
@db.bulk_save(@doc_buffer)
end
 
puts "Done."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.