Guest User

Untitled

a guest
Feb 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.73 KB | None | 0 0
  1. my $cursor = do {
  2.     $result = $elastic->client->search(
  3.         index => 'log',
  4.         type  => 'log',
  5.         query => $query,
  6.         size  => $bulk_size,
  7.         scroll => $scroll,
  8.         search_type => 'scan',
  9.     );
  10.  
  11.     sub {
  12.         my $rs = $elastic->client->scroll(
  13.             scroll_id => $result->{_scroll_id},
  14.             scroll    => $scroll,
  15.         );
  16.         $rs = 0 unless defined $rs->{hits}->{hits}->[0];
  17.         $rs;
  18.     };
  19. };
  20.  
  21. while( $result = $cursor->() ) {
  22.     for( @{ $result->{hits}->{hits} } ) {
  23.        # dumper le document en json et l'écrire sur une ligne dans un fichier
  24.     }
  25. }
  26.  
  27.  
  28. -----
  29.  
  30. my $index = sub {
  31.     my $bulk = shift;
  32.     $elastic->client->bulk( $bulk, refresh => 0 );
  33. };
  34.  
  35. my $i = 0;
  36. my $bulk = [];
  37. foreach my $file ( @files ) {
  38.     open( my $in, '<', $file ) or die "can't open file '$file' : $!\n";
  39.     while( my $line = <$in> ) {
  40.        try {
  41.             my $tweet = $json->decode( $line );
  42.            
  43.             push( @$bulk, {
  44.                 index => {
  45.                     index => 'log',
  46.                     type => 'log',
  47.                     id => "".delete($tweet->{id}),
  48.                     data => $tweet
  49.                 }
  50.             } );
  51.         } catch {
  52.             print STDERR (defined $_? $_ : '')." - line : ".$line;
  53.         };
  54.         print STDERR $eta->tick;
  55.  
  56.         if( ++$i > $bulk_size ) {
  57.             try {
  58.                 $index->( $bulk );
  59.             } catch {
  60.                 print STDERR $_;
  61.             };
  62.             $bulk = [];
  63.             $i = 0;
  64.         }
  65.     }
  66.     close $file;
  67. }
  68.  
  69. if( $i ) {
  70.     try { $index->( $bulk ); } catch { print STDERR $_; };
  71. }
  72. $elastic->client->refresh_index( index => $elastic->index );
Add Comment
Please, Sign In to add comment