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