Google BigQuery for Laravel

v1.0.1 2018-08-16 18:27 UTC

This package is not auto-updated.

Last update: 2024-09-15 00:58:01 UTC


README

本软件包旨在将Laravel的功能封装在Google的BigQuery周围。

安装

通过Composer

$ composer require prologuetech/big

设置

将我们的配置文件发布到您的应用程序中

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

您应该有一个config/prologue-big.php文件来配置默认值。

Laravel 5.4.x

Laravel的旧版本需要您在config/app.php中将我们的big服务提供者添加到您的应用程序提供者数组中

Prologuetech\Big\BigServiceProvider::class,

现在您可以使用熟悉的Laravel体验了,享受吧!

Google身份验证

Google SDK支持应用程序默认凭据(ADC),因此本软件包也支持。您可以将配置文件中的auth_file字段留为null以使用ADC。当前不支持凭据获取器,但将来可能会添加。

有关更多信息,请参阅adc文档

如何使用

配置

默认情况下,我们使用以下全局配置选项与BigQuery一起使用。

$this->options = [
    'useLegacySql' => false,
    'useQueryCache' => false,
];

在BQ中创建表时,我们将自动为您切换Eloquent模型模式。让我们通过使用Laravel的chunk方法将数据从我们的事件表存档到BQ的示例来了解一下。

$datasetId = 'test';
$tableId = 'events';

// Create our BQ helper
$big = new Big();

// Create table, we will pass in a mocked model to mutate into BQ schema
// Note: create table will only make a new table if it does not exist

/** @var Google\Cloud\BigQuery\Table $table */
$table = $big->createFromModel($datasetId, $tableId, new Event());

// Let's stream our events into BQ in large chunks
// Note: notArchived() is a simple scope, use whatever scopes you have on your model
Event::notArchived()->chunk(1000, function ($events) use ($big, $table) {
    // Prepare our rows
    $rows = $big->prepareData($events);

    // Stream into BQ, you may also pass in any options with a 3rd param.
    // Note: By default we use: 'ignoreUnknownValues' => true
    $big->insert($table, $rows);

    // Get our current id's
    /** @var Illuminate\Support\Collection $events */
    $ids = $events->pluck('id')->toArray();

    // Update these event's as processed
    Event::whereIn('id', $ids)->update([
        'system_processed' => 1
    ]);
});

就是这样!现在您在BigQuery中有了事件表的副本,享受吧!

查询

实例化Big将自动设置Google ServiceBuilder并直接通过内部$big->query提供对BigQuery的访问。然而,Big中内置了许多辅助器,使与BigQuery交互变得轻而易举(或者如果你喜欢那样的话,一个美味的胡萝卜)。

例如,当在BigQuery上运行查询时,我们必须使用循环中的reload方法轮询结果。Big提供了一个有用的方法run,所以你只需要这样做

$query = 'SELECT count(id) FROM test.events';

$big = new Big();
$results = $big->run($query);

使用run时,我们将自动轮询BigQuery,并将所有结果作为Laravel集合对象返回给你,这样你就可以像享受一杯清爽的Laravel咖啡一样享受你的结果。

变更日志

有关最近更改的更多信息,请参阅变更日志

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件