App-PipeFilter

 view release on metacpan or  search on metacpan

lib/App/PipeFilter/MysqlToJson.pm  view on Meta::CPAN

package App::PipeFilter::MysqlToJson;
{
  $App::PipeFilter::MysqlToJson::VERSION = '0.005';
}

use Moose;

extends 'App::PipeFilter::Generic';
with qw(
  App::PipeFilter::Role::Reader::LineByLine
  App::PipeFilter::Role::Output::Json
  App::PipeFilter::Role::Transform::None
);

use JSON::XS;

has _fields => (
  is  => 'rw',
  isa => 'ArrayRef',
);

before filter_file => sub {
  my ($self, $ifh, $ofh) = @_;

  # First line of MySQL bulk output (-B) is the field headers.
  $self->_fields( [ (scalar(<$ifh>) =~ m/(\S+)/g) ] );
};

sub decode_input {
  my ($self, $input_ref) = @_;
  chomp($$input_ref);

  my %row;
  @row{@{$self->_fields()}} = split /\t/, $$input_ref;
  return \%row;
}

1;

__END__

=pod

=head1 NAME

App::PipeFilter::MysqlToJson - translate mysql batch output to JSON

=head1 VERSION

version 0.005

=head1 SYNOPSIS

Here is the mysql2json(1) pipeline filter.

  #!/usr/bin/perl
  use App::PipeFilter::MysqlToJson;
  exit App::PipeFilter::MysqlToJson->new_with_options()->run();

=head1 DESCRIPTION

App::PipeFilter::MysqlToJson implements the mysql2json(1) pipeline
filter.  Please see mysql2json(1) for usage instructions.

This module reads mysql(1) batch output (via the -B option) and
produces a single JSON object per MySQL row.

Mysql batch output is produced by the mysql(1) utility's -B flag.

  mysql -B -u user -password -h 10.0.0.5 database \
    -e 'select crontab_id, task_id from crontab' | \
		mysql2json | jsort -k task_id -rn | head -5

Output may look like this:



( run in 1.078 second using v1.01-cache-2.11-cpan-6aa56a78535 )