specify riak url and dsn as arguments
[NoSQL-toys.git] / koha / koha2riak-search.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use DBI;
5 use RiakSearch;
6 use Data::Dump qw/dump/;
7
8
9 my ( $riak_url, $dbi ) = @ARGV;
10
11 $riak_url ||= 'http://10.60.0.92:8098';
12 $dbi ||= 'DBI:mysql:dbname=koha;host=10.60.0.10;port=3306';
13 my @tables = qw(
14 biblioitems
15 biblio
16 );
17
18 my $limit;
19 $limit = $ENV{LIMIT} if $ENV{LIMIT};
20
21 my $dbh = DBI->connect($dbi,"","") || die $DBI::errstr;
22 my $riak = RiakSearch->new( $riak_url );
23
24 warn $riak->request( 'GET' => '/koha.marcxml' );
25
26 sub riak_search_kv_hook {
27         $riak->request( 'PUT' => shift, { props => {
28                 precommit => [ { mod => 'riak_search_kv_hook', fun => 'precommit' } ],
29 #               precommit => [],
30 #               last_write_wins => 'true',
31                 }
32         });
33 }
34
35 riak_search_kv_hook 'koha.marcxml';
36
37 foreach my $table ( @tables ) {
38
39     my $sth = $dbh->prepare(qq{ select * from $table $limit}) || die $dbh->errstr();
40     $sth->execute || die $sth->errstr();
41     my @pk = $dbh->primary_key( undef, undef, $table );
42
43     print "import ", $sth->rows, " rows from $table pk:",dump( @pk ),"...\n";
44
45         riak_search_kv_hook "koha.$table";
46
47     while (my $row = $sth->fetchrow_hashref() ) {
48
49         my $key = join('_', map { $row->{$_} } @pk);
50                 my $biblionumber = $row->{biblionumber};
51
52         if ( my $marcxml = delete $row->{marcxml} ) {
53                         $riak->request( 'PUT' => "/koha.marcxml/$key", $marcxml, {
54                                 'Content-Type' => 'text/xml',
55                                 'Link' => qq|</riak/koha.$table/$biblionumber>; riaktag="biblio"|,
56                         } );
57         }
58
59 #               warn "## $key ",dump($row);
60
61                 my $headers;
62                 $headers->{Link} = qq|</riak/koha.biblio/$biblionumber>; riaktag="biblio"|
63                 if $biblionumber && $key !~ m/biblionumber/;
64
65                 $riak->request( 'PUT' => "/koha.$table/$key", $row, $headers );
66     }
67
68 }