view release on metacpan or search on metacpan
t/11_load_errors.t
t/12_insert_errors.t
t/13_select_errors.t
t/14_null.t
t/15_options.t
t/16_gs.t
t/17_extract.t
t/create_json.pl
t/delete_sample_datasets.pl
t/google-tutorial.pl
t/invalid_private_key_file.p12
t/invalid_private_key_file.txt
t/synopsis.pl
META.yml
MANIFEST
# NAME
Google::BigQuery - Google BigQuery Client Library for Perl
# SYNOPSIS
use Google::BigQuery;
my $client_email = <YOUR CLIENT EMAIL ADDRESS>;
my $private_key_file = <YOUR PRIVATE KEY FILE>;
my $project_id = <YOUR PROJECT ID>;
# create a instance
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
);
# create a dataset
my $dataset_id = <YOUR DATASET ID>;
$bq->create_dataset(
dataset_id => $dataset_id
);
$bq->use_dataset($dataset_id);
# METHODS
See details of option at https://cloud.google.com/bigquery/docs/reference/v2/.
- create
Create a instance.
my $bq = Google::BigQuery::create(
client_email => $client_email, # required
private_key_file => $private_key_file, # required
project_id => $project_id, # optional
dataset_id => $dataset_id, # optional
scope => \@scope, # optional (default is 'https://www.googleapis.com/auth/bigquery')
version => $version, # optional (only 'v2')
);
- use\_project
Set a default project.
lib/Google/BigQuery.pm view on Meta::CPAN
return $class->new(%args);
} else {
die "Can't load class: $class";
}
}
sub new {
my ($class, %args) = @_;
die "undefined client_eamil" if !defined $args{client_email};
die "undefined private_key_file" if !defined $args{private_key_file};
die "not found private_key_file" if !-f $args{private_key_file};
my $self = bless { %args }, $class;
$self->{GOOGLE_API_TOKEN_URI} = 'https://accounts.google.com/o/oauth2/token';
$self->{GOOGLE_API_GRANT_TYPE} = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
if ($self->{private_key_file} =~ /\.json$/) {
open my $in, "<", $self->{private_key_file} or die "can't open $self->{private_key_file} : $!";
my $private_key_json = decode_json(join('', <$in>));
close $in;
$self->{private_key} = $private_key_json->{private_key};
} elsif ($self->{private_key_file} =~ /\.p12$/) {
my $password = "notasecret";
my $pkcs12 = Crypt::OpenSSL::PKCS12->new_from_file($self->{private_key_file});
$self->{private_key} = $pkcs12->private_key($password);
} else {
die "invalid private_key_file format";
}
$self->_auth;
$self->_set_rest_description;
return $self;
}
sub DESTROY {
}
lib/Google/BigQuery.pm view on Meta::CPAN
$self->{ua} = LWP::UserAgent->new;
my $claim = {
iss => $self->{client_email},
scope => join(" ", @{$self->{scope}}),
aud => $self->{GOOGLE_API_TOKEN_URI},
exp => $self->{exp},
iat => $self->{iat},
};
my $jwt = JSON::WebToken::encode_jwt($claim, $self->{private_key}, 'RS256', { type => 'JWT' });
my $response = $self->{ua}->post(
$self->{GOOGLE_API_TOKEN_URI},
{ grant_type => $self->{GOOGLE_API_GRANT_TYPE}, assertion => $jwt }
);
if ($response->is_success) {
$self->{access_token} = decode_json($response->decoded_content);
} else {
my $error = decode_json($response->decoded_content);
lib/Google/BigQuery.pm view on Meta::CPAN
=head1 NAME
Google::BigQuery - Google BigQuery Client Library for Perl
=head1 SYNOPSIS
use Google::BigQuery;
my $client_email = <YOUR CLIENT EMAIL ADDRESS>;
my $private_key_file = <YOUR PRIVATE KEY FILE>;
my $project_id = <YOUR PROJECT ID>;
# create a instance
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
);
# create a dataset
my $dataset_id = <YOUR DATASET ID>;
$bq->create_dataset(
dataset_id => $dataset_id
);
$bq->use_dataset($dataset_id);
lib/Google/BigQuery.pm view on Meta::CPAN
See details of option at https://cloud.google.com/bigquery/docs/reference/v2/.
=over 4
=item * create
Create a instance.
my $bq = Google::BigQuery::create(
client_email => $client_email, # required
private_key_file => $private_key_file, # required
project_id => $project_id, # optional
dataset_id => $dataset_id, # optional
scope => \@scope, # optional (default is 'https://www.googleapis.com/auth/bigquery')
version => $version, # optional (only 'v2')
);
=item * use_project
Set a default project.
t/01_create.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Google::BigQuery;
my $bigquery = Google::BigQuery::create(
client_email => $ENV{CLIENT_EMAIL},
private_key_file => $ENV{PRIVATE_KEY_FILE}
);
isnt($bigquery, undef, 'constructor');
isnt($bigquery->{access_token}, undef, 'access_token');
done_testing;
t/02_projects.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file
);
my $response = $bigquery->request(method => 'list', resource => 'projects');
ok(@{$response->{projects}}, "list");
done_testing;
t/03_datasets.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id
);
my $response;
# insert
$response = $bigquery->request(
method => 'insert',
resource => 'datasets',
dataset_id => $dataset_id,
t/04_tables.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id
);
my $response;
# create dataset
$response = $bigquery->request(
method => 'insert',
resource => 'datasets',
t/05_jobs.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id
);
my $response;
# create dataset
$response = $bigquery->request(
method => 'insert',
resource => 'datasets',
t/06_tabledata.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id,
);
my $response;
my $table_id = 'sample_table_' . time;
# create dataset
$response = $bigquery->request(
method => 'insert',
t/07_db_methods.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use FindBin '$Bin';
use JSON qw(decode_json encode_json);
use Data::Dumper;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $ret;
my $bigquery = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file
);
# set default project
$bigquery->use_project($project_id);
is($bigquery->{project_id}, $project_id, "use_project");
# set default dataset
$bigquery->use_dataset($dataset_id);
is($bigquery->{dataset_id}, $dataset_id, "use_dataset");
t/08_auth_error.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $dataset_id = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $ret;
my $bq;
# auth
throws_ok
{ Google::BigQuery::create() }
'/undefined client_eamil/',
'undefined client_email';
throws_ok
{ Google::BigQuery::create(client_email=>$client_email) }
'/undefined private_key_file/',
'undefined private_key_file';
throws_ok
{ Google::BigQuery::create(client_email=>"${client_email}x", private_key_file=>$private_key_file); }
'/invalid_grant/',
'invalid_grant';
throws_ok
{ Google::BigQuery::create(client_email=>"${client_email}", private_key_file=>"x${private_key_file}") }
'/not found private_key_file/',
'not found private_key_file';
my $invalid_private_key_file="$Bin/invalid_private_key_file.txt";
throws_ok
{ Google::BigQuery::create(client_email=>"${client_email}", private_key_file=>"$invalid_private_key_file") }
'/invalid private_key_file format/',
'invalid private_key_file format';
my $bq = Google::BigQuery::create(client_email=>$client_email, private_key_file=>$private_key_file);
done_testing;
t/09_datasets_errors.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $ret;
my $bq = Google::BigQuery::create(client_email=>$client_email, private_key_file=>$private_key_file);
# create_dataset
is($bq->create_dataset(), 0, 'create_dataset: no project');
is($bq->create_dataset(project_id=>$project_id), 0, 'create_dataset: no dataset');
is($bq->create_dataset(project_id=>"${project_id}x",dataset_id=>$dataset_id), 0, 'create_dataset: not found project');
is($bq->create_dataset(project_id=>"${project_id}", dataset_id=>"${dataset_id}"), 1, 'create_datset: ok');
is($bq->create_dataset(project_id=>"${project_id}", dataset_id=>"${dataset_id}"), 0, 'create_dataset: already exists dataset');
# drop_dataset
is($bq->drop_dataset(), 0, 'create_dataset: no project');
t/10_table_errors.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $ret;
my $bq = Google::BigQuery::create(client_email=>$client_email, private_key_file=>$private_key_file);
is($bq->create_dataset(project_id=>$project_id,dataset_id=>$dataset_id),1,'create_dataset: ok');
# create table
is($bq->create_table(),0,'create_table: no project');
is($bq->create_table(project_id=>$project_id),0,'create_table: no dataset');
is($bq->create_table(project_id=>$project_id,dataset_id=>$dataset_id),0,'create_table: no table');
is($bq->create_table(project_id=>$project_id,dataset_id=>$dataset_id,table_id=>$table_id),1,'create_table: ok');
is($bq->create_table(project_id=>$project_id,dataset_id=>$dataset_id,table_id=>$table_id),0,'create_table: already exists');
t/11_load_errors.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
{ name => 'address', type => 'STRING', mode => 'NULLABLE' },
{ name => 'skills', type => 'STRING', mode => 'REPEATED' },
{ name => 'jobs', type => 'RECORD', mode => 'REPEATED', fields => [
{ name => 'year', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'company', type => 'STRING', mode => 'REQUIRED' },
{ name => 'role', type => 'STRING', mode => 'NULLABLE' },
]}
];
my $ret;
my $bq = Google::BigQuery::create(
client_email=>$client_email,
private_key_file=>$private_key_file,
project_id=>$project_id,
dataset_id=>$dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
# no requried field error
if (1) {
my $data = "data.json";
t/12_insert_errors.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
];
my $ret;
my $bq = Google::BigQuery::create(
client_email=>$client_email,
private_key_file=>$private_key_file,
project_id=>$project_id,
dataset_id=>$dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
if (1) {
my $values = [
{ id => 1, name => 'foo' },
t/13_select_errors.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
];
my $ret;
my $bq = Google::BigQuery::create(
client_email=>$client_email,
private_key_file=>$private_key_file,
project_id=>$project_id,
dataset_id=>$dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
my $values = [
{ id => 1, name => 'foo' },
{ id => 2, name => 'baz' },
{ id => 3, name => 'bar' },
t/14_null.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
];
my $ret;
my $bq = Google::BigQuery::create(
client_email=>$client_email,
private_key_file=>$private_key_file,
project_id=>$project_id,
dataset_id=>$dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
if (1) {
my $data = "data.tsv";
open my $out, ">", $data;
t/15_options.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
{ name => 'address', type => 'STRING', mode => 'NULLABLE' },
{ name => 'phone', type => 'INTEGER', mode => 'NULLABLE' },
];
my $ret;
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id,
scope => [qw(https://www.googleapis.com/auth/bigquery https://www.googleapis.com/auth/devstorage.full_control)],
version => 'v2',
);
isnt($bq, undef, 'create');
# create_dataset
if (1) {
my $access = [];
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
{ name => 'address', type => 'STRING', mode => 'NULLABLE' },
{ name => 'phone', type => 'INTEGER', mode => 'NULLABLE' },
];
my $bucket = $ENV{GS_BUCKET};
my $ret;
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
$bq->load(
table_id=>$table_id,
data=>[("gs://$bucket/data-100000.json.gz", "gs://$bucket/data-10000.json.gz")],
);
my ($count) = $bq->selectrow_array(query => "SELECT COUNT(*) FROM $table_id");
t/17_extract.t view on Meta::CPAN
use strict;
use Test::More 0.98;
use Test::Exception;
use JSON qw(decode_json encode_json);
use Data::Dumper;
use FindBin '$Bin';
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my ${dataset_id} = 'sample_dataset_' . time;
my $table_id = 'sample_table_' . time;
my $schema = [
{ name => 'id', type => 'INTEGER', mode => 'REQUIRED' },
{ name => 'name', type => 'STRING', mode => 'REQUIRED' },
{ name => 'address', type => 'STRING', mode => 'NULLABLE' },
{ name => 'phone', type => 'INTEGER', mode => 'NULLABLE' },
];
my $bucket = $ENV{GS_BUCKET};
my $ret;
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
dataset_id => $dataset_id,
);
$bq->create_dataset;
$bq->create_table(table_id=>$table_id,schema=>$schema);
$bq->load(
table_id=>$table_id,
data=>[("gs://$bucket/data-100000.json.gz", "gs://$bucket/data-10000.json.gz")],
);
t/delete_sample_datasets.pl view on Meta::CPAN
#!/usr/bin/env perl
use strict;
use warnings;
use Google::BigQuery;
use Data::Dumper;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id
);
my @datasets = grep /^sample_dataset_/, $bq->show_datasets;
foreach my $dataset (@datasets) {
print "drop dataset: $dataset\n";
$bq->drop_dataset(dataset_id => $dataset, deleteContents => 1);
}
t/google-tutorial.pl view on Meta::CPAN
#!/usr/bin/env perl
use strict;
use warnings;
use Google::BigQuery;
use Data::Dumper;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
);
{
print "[Heaviest 10 children]\n";
my $aref = $bq->selectall_arrayref(
query => "SELECT TOP(title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0"
);
foreach my $ref (@$aref) {
print join("\t", @$ref), "\n";
t/synopsis.pl view on Meta::CPAN
#!/usr/bin/env perl
use strict;
use warnings;
use Google::BigQuery;
my $client_email = $ENV{CLIENT_EMAIL};
my $private_key_file = $ENV{PRIVATE_KEY_FILE};
my $project_id = $ENV{PROJECT_ID};
# create a instance
my $bq = Google::BigQuery::create(
client_email => $client_email,
private_key_file => $private_key_file,
project_id => $project_id,
);
# create a dataset
my $dataset_id = "sample_dataset_" . time;
$bq->create_dataset(
dataset_id => $dataset_id
);
$bq->use_dataset($dataset_id);