migrate data from CouchDB to MongoDB
[pxelator] / bin / couchdb2mongodb.pl
1 #!/usr/bin/perl
2
3 # http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
4
5 use warnings;
6 use strict;
7
8 use IO::Socket::INET;
9 use Storable qw();
10 use JSON;
11 use Data::Dump qw(dump);
12 use Time::HiRes qw(time);
13 use File::Path qw(make_path remove_tree);
14 use MongoDB;
15
16 my $name = 'pxelator';
17
18 my $conn = MongoDB::Connection->new;
19 my $db = $conn->get_database( $name );
20 my $audit = $db->get_collection("audit");
21
22 sub couchdb_socket {
23         IO::Socket::INET->new(
24                 PeerAddr => '10.60.0.91',
25                 PeerPort => 5984,
26                 Proto => 'tcp',
27         ) || die $!;
28 }
29
30 sub get_chunk {
31         my $sock = shift;
32         my $chunk;
33         while(<$sock>) {
34                 $chunk .= $_;
35                 last if /^[\n\r]+$/;
36         }
37 #       warn "# $sock\n$chunk\n";
38         return $chunk;
39 }
40
41 my $sock = couchdb_socket;
42
43 print $sock "GET /$name/_all_docs?include_docs=true HTTP/1.0\r\n\r\n";
44
45 get_chunk($sock);
46
47 my $total = <$sock>;
48 $total =~ s{^.*total_rows\D+(\d+).+$}{$1};
49 warn "# total: $total\n";
50
51 while(<$sock>) {
52         if ( /"id":"([^"]+)"/ ) {
53
54                 s/,[\r\n]+$//; # cleanup JSON
55                 my $json = from_json( $_ );
56                 $audit->insert( $json->{doc} );
57                 print STDERR ".";
58         } else {
59                 warn "UNKNOWN: $_";
60         }
61 }
62