migrate data from CouchDB to MongoDB
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jan 2010 15:06:59 +0000 (15:06 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jan 2010 15:06:59 +0000 (15:06 +0000)
bin/couchdb2mongodb.pl [new file with mode: 0755]

diff --git a/bin/couchdb2mongodb.pl b/bin/couchdb2mongodb.pl
new file mode 100755 (executable)
index 0000000..6d9420e
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+# http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
+
+use warnings;
+use strict;
+
+use IO::Socket::INET;
+use Storable qw();
+use JSON;
+use Data::Dump qw(dump);
+use Time::HiRes qw(time);
+use File::Path qw(make_path remove_tree);
+use MongoDB;
+
+my $name = 'pxelator';
+
+my $conn = MongoDB::Connection->new;
+my $db = $conn->get_database( $name );
+my $audit = $db->get_collection("audit");
+
+sub couchdb_socket {
+       IO::Socket::INET->new(
+               PeerAddr => '10.60.0.91',
+               PeerPort => 5984,
+               Proto => 'tcp',
+       ) || die $!;
+}
+
+sub get_chunk {
+       my $sock = shift;
+       my $chunk;
+       while(<$sock>) {
+               $chunk .= $_;
+               last if /^[\n\r]+$/;
+       }
+#      warn "# $sock\n$chunk\n";
+       return $chunk;
+}
+
+my $sock = couchdb_socket;
+
+print $sock "GET /$name/_all_docs?include_docs=true HTTP/1.0\r\n\r\n";
+
+get_chunk($sock);
+
+my $total = <$sock>;
+$total =~ s{^.*total_rows\D+(\d+).+$}{$1};
+warn "# total: $total\n";
+
+while(<$sock>) {
+       if ( /"id":"([^"]+)"/ ) {
+
+               s/,[\r\n]+$//; # cleanup JSON
+               my $json = from_json( $_ );
+               $audit->insert( $json->{doc} );
+               print STDERR ".";
+       } else {
+               warn "UNKNOWN: $_";
+       }
+}
+