google/cloud-bigquery-connection
Installation:
composer require google/cloud-bigquery-connection
Ensure your Laravel project has PHP 8.1+ (recommended: 8.4+ for full compatibility).
Authentication:
Configure credentials via environment variables (.env):
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
Or use ADC (Application Default Credentials) if running in GCP environments.
First Use Case: Fetch a BigQuery connection (e.g., for a dataset):
use Google\Cloud\BigQuery\Connection\V1\Client\ConnectionServiceClient;
use Google\Cloud\BigQuery\Connection\V1\GetConnectionRequest;
$client = new ConnectionServiceClient();
$request = (new GetConnectionRequest())
->setName('projects/YOUR_PROJECT/datasets/YOUR_DATASET/connections/YOUR_CONNECTION');
$response = $client->getConnection($request);
return $response->serializeToJsonString();
ConnectionServiceClient (primary interface).GetConnectionRequest, ListConnectionsRequest, etc.serializeToJsonString() for debugging or getConnection() for direct object access.$listRequest = (new ListConnectionsRequest())
->setParent('projects/YOUR_PROJECT/datasets/YOUR_DATASET');
$connections = $client->listConnections($listRequest);
foreach ($connections->getConnections() as $connection) {
// Process each connection
}
CreateConnectionRequest with a Connection object populated via:
$connection = (new Connection())
->setDisplayName('My BigQuery Connection')
->setConnectionType('BIGQUERY_CONNECTION_TYPE_UNSPECIFIED');
AppServiceProvider:
public function register()
{
$this->app->singleton(ConnectionServiceClient::class, function ($app) {
return new ConnectionServiceClient();
});
}
// app/Facades/BigQueryConnection.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class BigQueryConnection extends Facade {
protected static function getFacadeAccessor() { return 'bigquery.connection'; }
}
Register in AppServiceProvider:
$this->app->bind('bigquery.connection', function ($app) {
return $app->make(ConnectionServiceClient::class);
});
Usage:
$connection = BigQueryConnection::getConnection($request);
Leverage gRPC for streaming (e.g., large result sets):
$client = new ConnectionServiceClient(['grpc' => true]);
$stream = $client->listConnections($listRequest);
foreach ($stream as $connection) {
// Process streamed responses
}
While this package focuses on connection management, pair it with google/cloud-bigquery for queries:
use Google\Cloud\BigQuery\BigQueryClient;
$bigQuery = new BigQueryClient();
$queryJob = $bigQuery->query('SELECT * FROM `project.dataset.table`');
Authentication:
Deprecations:
credentials client option is deprecated. Use ADC or service account files instead.serviceAddress configurations are obsolete. Use the new connection resource names (e.g., projects/{project}/datasets/{dataset}/connections/{connection}).gRPC vs REST:
$client = new ConnectionServiceClient(['grpc' => true]);
PHP Version Quirks:
declare(strict_types=1) to catch type issues early.grpc/grpc and google/protobuf dependencies. Run composer update if issues arise.Logging:
Enable debug logs via the logger client option:
$client = new ConnectionServiceClient([
'logger' => new \Monolog\Logger('debug', [new \Monolog\Handler\StreamHandler('php://stderr')])
]);
Or use Laravel’s logging:
$client = new ConnectionServiceClient([
'logger' => \Log::getMonolog()
]);
Error Handling:
Catch Google\ApiCore\ApiException for API errors and Google\ApiCore\Retry\RetrySettings for retry logic:
try {
$response = $client->getConnection($request);
} catch (ApiException $e) {
\Log::error('BigQuery Connection Error', ['message' => $e->getMessage()]);
throw new \RuntimeException('Failed to fetch connection', 0, $e);
}
Connection Names:
projects/{project}/datasets/{dataset}/connections/{connection}.if (!preg_match('/^projects\/\w+(\/\w+){2,}\/connections\/\w+$/', $connectionName)) {
throw new \InvalidArgumentException('Invalid connection name format');
}
Custom Requests:
Extend the GetConnectionRequest or ListConnectionsRequest classes to add custom fields:
class CustomGetConnectionRequest extends GetConnectionRequest {
public function setCustomField(string $value): self {
$this->customField = $value;
return $this;
}
}
Response Transformers: Create a transformer to map raw responses to Laravel models:
$connection = (new ConnectionTransformer())->transform($client->getConnection($request));
Event Listeners:
Trigger events for connection lifecycle (e.g., connection.created):
event(new BigQueryConnectionEvent($connection));
Testing:
Use the Google\Cloud\Testing\MockConnectionServiceClient for unit tests:
$mockClient = new MockConnectionServiceClient();
$mockClient->method('getConnection')->willReturn($mockResponse);
$listRequest = (new ListConnectionsRequest())
->setParent('projects/YOUR_PROJECT/datasets/YOUR_DATASET')
->setPageSize(100);
$connections = $client->listConnections($listRequest);
$client = new ConnectionServiceClient([
'retry' => [
'maxAttempts' => 3,
'initialBackoff' => 100, // ms
]
]);
$connection = Cache::remember("bigquery.connection.{$connectionName}", now()->addHours(1), function () use ($client, $request) {
return $client->getConnection($request);
});
dispatch(new ListBigQueryConnections($client, $datasetId));
How can I help you explore Laravel packages today?