d89853ff92014c88c72d52ff3e0ad15569116f46
[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 = "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 => { precommit => [ { mod => 'riak_search_kv_hook', fun => 'precommit' } ] } });
26 }
27
28 riak_search_kv_hook '/koha.marcxml';
29
30 foreach my $table ( @tables ) {
31
32     my $sth = $dbh->prepare(qq{ select * from $table $limit}) || die $dbh->errstr();
33     $sth->execute || die $sth->errstr();
34     my @pk = $dbh->primary_key( undef, undef, $table );
35
36     print "import ", $sth->rows, " rows from $table pk:",dump( @pk ),"...\n";
37
38         riak_search_kv_hook "koha.$table";
39
40     while (my $row = $sth->fetchrow_hashref() ) {
41
42         my $key = join('_', map { $row->{$_} } @pk);
43
44         if ( my $marcxml = delete $row->{marcxml} ) {
45                         $riak->request( 'PUT' => "/koha.marcxml/$key", $marcxml, {
46                                 'Content-Type' => 'text/xml',
47                         } );
48         }
49
50 #               warn "## $key ",dump($row);
51
52                 my $headers;
53                 if ( my $biblionumber = $row->{biblionumber} ) {
54                         $headers->{Link} = qq|</riak/koha.biblio/$biblionumber>; riaktag="biblio"| unless $key =~ m/biblionumber/;
55                 }
56                 $riak->request( 'PUT' => "/koha.$table/$key", $row, $headers );
57     }
58
59 }