ynizon / big
Google BigQuery for Laravel
Requires
- php: ~7.0
- google/cloud: >0.60
- illuminate/cache: ^5.4
- illuminate/database: ^5.4
- illuminate/support: ^5.4
README
此包旨在将Laravel功能包装在Google的BigQuery周围。
来源: https://github.com/prologuetech/laravel-big
更新:添加将Big Query模型转换为Eloquent和运行绑定SQL查询的功能。更新Google云依赖项。
安装
通过Composer
$ composer require ynizon/big
设置
将我们的配置文件发布到您的应用程序中
php artisan vendor:publish --provider="Prologuetech\Big\BigServiceProvider"
您应该有一个 config/prologue-big.php 文件来配置默认值。
Laravel 5.4.x
旧版本的Laravel要求您将我们的big服务提供者添加到您的应用程序提供者数组中的 config/app.php
Prologuetech\Big\BigServiceProvider::class,
您现在可以访问熟悉Laravel体验了,享受吧!
Google身份验证
Google SDK支持应用程序默认凭证(ADC),因此此包也是如此。您可以将配置文件中的 auth_file 字段留空以使用ADC。当前不支持凭证获取器,但将来可能会添加。
有关更多信息,请参阅 adc文档。
如何使用
配置
默认情况下,我们使用以下全局配置选项与BigQuery一起使用。
$this->options = [ 'useLegacySql' => false, 'useQueryCache' => false, ];
表
当在BQ中创建表时,我们会自动为您翻转Eloquent模型模式。让我们通过使用Laravel的chunk方法将数据从我们的events表存档到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中有events表的副本了,享受吧!
查询
实例化 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);
从Big Query查询获取具有分页的Eloquent模型的示例。
$products = Product::whereIn("account_id",$accounts_id); $arrLikeFields[] = "title"; $products = $products->where("title","LIKE","%".$q."%"); $sql = $big->bindCountQuery($query, array("products"), $arrLikeFields); //Get count $totalProducts = $big->run($sql); $totalProducts = $totalProducts[0]["nb"]; //Get products $sql = $big->bindQuery($query->orderBy("updated_at"), array("products"),15, $currentPage, $arrLikeFields); $products = Big::unflipModel(new Product(), $big->run($sql)); $url = "/"; if (isset($_SERVER["REDIRECT_URL"])){ $url = $_SERVER["REDIRECT_URL"]; } $products = new LengthAwarePaginator($products, $totalProducts, 15, $currentPage, ['path'=>url($url)]);
当我们使用 run 时,我们将自动轮询BigQuery并将所有结果作为Laravel集合对象返回,这样您就可以像享用一杯清新的Laravel一样享受结果。
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
许可证
MIT许可证(MIT)。请参阅 许可证文件 以获取更多信息。