ace3441f540aa00cc7f7db61398e906dc939ed16
[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 my $limit = "limit 5";
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 $dbh = DBI->connect($dbi,"","") || die $DBI::errstr;
17 my $riak = RiakSearch->new( $riak_url );
18
19 warn $riak->request( 'GET' => '/koha.marcxml' );
20
21 sub riak_search_kv_hook {
22         $riak->request( 'PUT' => shift, { props => { precommit => [ { mod => 'riak_search_kv_hook', fun => 'precommit' } ] } });
23 }
24
25 riak_search_kv_hook '/koha.marcxml';
26
27 foreach my $table ( @tables ) {
28
29     my $sth = $dbh->prepare(qq{ select * from $table $limit}) || die $dbh->errstr();
30     $sth->execute || die $sth->errstr();
31     my @pk = $dbh->primary_key( undef, undef, $table );
32
33     print "import ", $sth->rows, " rows from $table pk:",dump( @pk ),"...\n";
34
35         riak_search_kv_hook "koha.$table";
36
37     while (my $row = $sth->fetchrow_hashref() ) {
38
39         my $key = join('_', map { $row->{$_} } @pk);
40
41         if ( my $marcxml = delete $row->{marcxml} ) {
42                         $riak->request( 'PUT' => "/koha.marcxml/$key", $marcxml, {
43                                 'Content-Type' => 'text/xml',
44                         } );
45         }
46
47         warn "# $key ",dump($row);
48
49                 my $headers;
50                 if ( exists $row->{biblionumber} && $key !~ m/biblionumber/ ) {
51                         $headers->{Links} = '/koha.biblio/' . $row->{biblionumber};
52                 }
53                 $riak->request( 'PUT' => "/koha.$table/$key", $row, $headers );
54     }
55
56 }