import rows into MongoDB
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Jan 2010 11:14:04 +0000 (12:14 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Jan 2010 11:14:04 +0000 (12:14 +0100)
dbi2mongo.pl [new file with mode: 0755]

diff --git a/dbi2mongo.pl b/dbi2mongo.pl
new file mode 100755 (executable)
index 0000000..378728e
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -w
+
+sub BEGIN {
+$ENV{DBI_AUTOPROXY}='dbi:Gofer:transport=stream;url=ssh:dpavlin@koha.ffzg.hr';
+}
+
+use strict;
+use DBI;
+use MongoDB;
+use Data::Dump qw/dump/;
+
+$|++;
+
+my $debug = @ARGV ? 1 : 0;
+
+our $dbi = "DBI:mysql:database=test";
+our ( $dbi, $user, $password );
+our ( $database, $collection ) = ( 'test', 'test' );
+
+our $sql = qq{
+       select
+               id as _id,
+               table.*
+       from table
+       where id > ?
+       order by id asc
+       limit 100000
+};
+
+require 'config.pl';
+
+warn "# $dbi $user -> $database $collection\n";
+
+my $conn = MongoDB::Connection->new;
+my $db   = $conn->get_database( $database );
+my $coll = $db->get_collection( $collection );
+my $dbh  = DBI->connect($dbi,$user,$password,{ RaiseError => 1 });
+
+$db->drop if $debug;
+
+# > db.items.find().sort({item_id:-1}).limit(1);
+my $last = $coll->query()->sort({ '_id' => -1 })->limit(1)->next;
+warn dump( $last );
+my $last_id = $last->{_id} || 0;
+
+print "Fetching items from $dbi _id > $last_id\n";
+
+my $sth = $dbh->prepare($sql);
+$sth->execute( $last_id );
+
+warn dump( $sth->{NAME} );
+
+print "found ",$sth->rows," items to process...\n";
+
+while (my $row = $sth->fetchrow_hashref() ) {
+
+       map { $row->{$_} * 1 } grep { defined $row->{$_} && $row->{$_} =~ /^\d+$/ } keys %$row;
+       $coll->insert( $row );
+}
+