Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created April 26, 2009 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/101968 to your computer and use it in GitHub Desktop.
Save jamesu/101968 to your computer and use it in GitHub Desktop.
#!/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