Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.