项目作者: prologuetech

项目描述 :
Google BigQuery for Laravel
高级语言: PHP
项目地址: git://github.com/prologuetech/laravel-big.git
创建时间: 2017-06-26T20:01:46Z
项目社区:https://github.com/prologuetech/laravel-big

开源协议:MIT License

下载


Google BigQuery for Laravel

This package aims to wrap laravel functionality around Google’s BigQuery.

Install

Via Composer

  1. $ composer require prologuetech/big

Setup

Publish our config file into your application:

  1. php artisan vendor:publish --provider="Prologuetech\Big\BigServiceProvider"

You should have a config/prologue-big.php file to configure defaults.

Laravel 5.4.x

Older versions of Laravel require you to add our big service provider to your application providers array in config/app.php:

  1. Prologuetech\Big\BigServiceProvider::class,

You now have access to a familiar laravel experience, enjoy!

Google Authentication

The Google SDK supports Application Default Credentials (ADC) and thus this package does as well. You may leave your auth_file field inside of your config file null to use ADC. Credentials fetcher is not currently supported but may be added in the future.

For more information see the adc docs.

How to use

Configuration

By default we use the following global config options with BigQuery.

  1. $this->options = [
  2. 'useLegacySql' => false,
  3. 'useQueryCache' => false,
  4. ];

Tables

When creating tables in BQ we automatically flip a Eloquent model schema for you. Let’s cover an example of archiving data
from our events table into BQ using laravel’s chunk method.

  1. $datasetId = 'test';
  2. $tableId = 'events';
  3. // Create our BQ helper
  4. $big = new Big();
  5. // Create table, we will pass in a mocked model to mutate into BQ schema
  6. // Note: create table will only make a new table if it does not exist
  7. /** @var Google\Cloud\BigQuery\Table $table */
  8. $table = $big->createFromModel($datasetId, $tableId, new Event());
  9. // Let's stream our events into BQ in large chunks
  10. // Note: notArchived() is a simple scope, use whatever scopes you have on your model
  11. Event::notArchived()->chunk(1000, function ($events) use ($big, $table) {
  12. // Prepare our rows
  13. $rows = $big->prepareData($events);
  14. // Stream into BQ, you may also pass in any options with a 3rd param.
  15. // Note: By default we use: 'ignoreUnknownValues' => true
  16. $big->insert($table, $rows);
  17. // Get our current id's
  18. /** @var Illuminate\Support\Collection $events */
  19. $ids = $events->pluck('id')->toArray();
  20. // Update these event's as processed
  21. Event::whereIn('id', $ids)->update([
  22. 'system_processed' => 1
  23. ]);
  24. });

That’s it! You now have a replica of your events table in BigQuery, enjoy!

Queries

Instantiating Big will automatically setup a Google ServiceBuilder and give us direct access to BigQuery through
our internals via $big->query. However there are many helpers built into Big that make interacting with BigQuery a
piece of cake (or a tasty carrot if you’re into that kind of thing).

For example when running a query on BigQuery we must use the reload method in a loop to poll results. Big comes with a
useful method run so all you need to do is this:

  1. $query = 'SELECT count(id) FROM test.events';
  2. $big = new Big();
  3. $results = $big->run($query);

When using run we automatically poll BigQuery and return all results as a laravel collection object for you so you
can enjoy your results as a refreshing cup of Laravel.

Change log

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.