convert koha marcxml to riak bin koha.marcxml
[NoSQL-toys.git] / koha / koha2riak-search.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use DBI;
5 use Net::Riak;
6 use Data::Dump qw/dump/;
7
8 my $limit = "limit 3";
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 = Net::Riak->new(host => $riak_url );
18
19 #my $marcxml_bucket = $riak->bucket( 'koha.marcxml' );
20
21 foreach my $table ( @tables ) {
22
23     my $sth = $dbh->prepare(qq{ select * from $table $limit}) || die $dbh->errstr();
24     $sth->execute || die $sth->errstr();
25     my @pk = $dbh->primary_key( undef, undef, $table );
26
27     print "import ", $sth->rows, " rows from $table pk:",dump( @pk ),"...\n";
28
29     my $bucket = $riak->bucket( 'koha.' . $table );
30
31     while (my $row = $sth->fetchrow_hashref() ) {
32
33         my $key = join('_', map { $row->{$_} } @pk);
34
35         if ( my $marcxml = delete $row->{marcxml} ) {
36             my $request = $riak->client->new_request(
37                 'PUT', [ 'riak', "koha.marcxml/$key" ]
38             );
39             $request->header('Content-Type' => 'text/xml');
40             $request->content($marcxml);
41             my $response = $riak->client->send_request($request);
42
43             warn "$riak_url/riak/koha.marcxml/$key ", length($marcxml), " bytes\n";
44
45             unless ($response->is_success) {
46                 die "Error put marcxml:", dump( $response );
47             }
48         }
49
50         warn "# $key ",dump($row);
51     }
52
53 }