Batch upload data to Google bigquery with Symfony

Installs: 131

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 5

Forks: 0

Open Issues: 1


1.0.0-beta 2019-05-03 15:13 UTC

This package is auto-updated.

Last update: 2020-06-29 13:38:58 UTC


This bundle offers a simple method to batch upload data to Google bigquery.


3 concepts are useful to work with this bundle:


An entity is any object implementing \CCMBenchmark\BigQueryBundle\BigQuery\Entity\RowInterface. This interface extends JsonSerializable to handle the export to bigquery. An entity is coupled to a metadata object.


Metadata are responsible to store the schema related to your entity and other information used to store data into bigquery. A metadata is a class implementing \CCMBenchmark\BigQueryBundle\BigQuery\MetadataInterface.


The UnitOfWork is provided by the bundle. It's responsible to store the data and then to upload it to bigquery.

Full example

class MyMetadata implements CCMBenchmark\BigQueryBundle\BigQuery\MetadataInterface {
    public function getEntityClass(): string {
        return MyEntity::class;

    public function getDatasetId(): string {
        return 'mydataset';
    public function getProjectId(): string {
        return 'myproject';
    public function getTableId(): string {
        return 'mytable';
    public function getSchema(): array {
        return [
            [ "mode"=> "NULLABLE", "name"=> "sessions", "type"=> "INTEGER" ]

class MyEntity implements CCMBenchmark\BigQueryBundle\BigQuery\Entity\RowInterface {
    use CCMBenchmark\BigQueryBundle\BigQuery\Entity\RowTrait;

    private $sessions;
    public function __construct(int $sessions) {
        $this->sessions = $sessions;

    public function jsonSerialize()
        return [
            'sessions' => $this->sessions,
            'created_at' => (new \Datetime())->format('Y-m-d H:i:s'),

// @var $unitOfWork \CCMBenchmark\BigQueryBundle\BigQuery\UnitOfWork
    new MyEntity(

// Your data will be uploaded when calling this method

Getting started

Install this package

  1. Require the package composer require ccmbenchmark/bigquery-bundle
  2. Add it to your kernel:

Symfony 4+:

return [
    \CCMBenchmark\BigQueryBundle\BigQueryBundle::class => ['all' => true]

Symfony 3.4:

$bundles = array(
    new \CCMBenchmark\BigQueryBundle\BigQueryBundle(),

Setup your project on google cloud storage and google bigquery

To upload data to google big query using google cloud storage, you need:

  1. A valid google cloud project with the following apis: BigQuery API, Cloud Storage
  2. A valid service account with the json api identifier
  3. To setup the billing on your account

Note: Usage of google cloud storage and google big query can be charged by Google. So using this bundle can produce charges on your account. You are responsible of that.

Setup the bundle

        bucket: [Name of your bucket in google cloud storage]
        application_name: "My application"
        credentials_file: "[Path to your credentials in json format]"
    proxy: ## Remove this section if you don't have any proxy or set the values to "~"
        host: ""
        port: "%proxy.port"

Create and declare your metadata

To create a metadata, create a new class implementing MetadataInterface.

To automatically register your metadata into the UnitOfWork, this bundle provides a tag to declare on this service.

<service id="AppBundle\MyMetadata">
    <tag name="big_query.metadata" />

At this point your metadata are declared into the UnitOfWork, thanks to a CompilerPass. You are ready to upload data.

Upload data to google bigquery

To upload data, you need to use the service CCMBenchmark\BigQueryBundle\BigQuery\UnitOfWork. This service offers a simple API. Call addData to store a new Entity to upload. When all you're entities are in the UnitOfWork, call flush to upload it.


If there is no exception thrown by the code but you cannot find your data in bigquery, you should follow this steps:

  1. Check google cloud storage. A file named "reporting-[YYYY-mm-dd]-[uniqId].json" should be here. If there is no such file, check permissions on GCP.
  2. Check google big query. A new job should have been submitted. Be sure to check in the project history. If the job is errored, try to submit it again using the UI, bigquery will display the errors.

Cleaning google cloud storage

This is out of scope of this bundle, but to save storage you can define a lifecycle in your bucket.