PHP code example of chocofamilyme / bigquery

1. Go to this page and download the library: Download chocofamilyme/bigquery library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chocofamilyme / bigquery example snippets


...
return new \Phalcon\Config([
    'analytics' => [
        'dataset'       => 'holding',
        'queueName'     => 'analytics',
        'exchangeType'  => 'direct',

        'undeliveredDataModel' => \Helper\Analytics\Models\UndeliveredDataMock::class,
        'connection' => [
            'keyFilePath' => __DIR__.'/_data/keys/key.json',
            //'keyFile'     => {},
        ],
        'mappers' => [
            'tableName' => \Chocofamily\Analytics\NullMapper::class,
        ],

        'repeater'    => [
            'attempt' => 5,
            'exclude' => [
                \InvalidArgumentException::class,
                \Google\Cloud\Core\Exception\NotFoundException::class,
            ],
        ],

        'pathStorage' => __DIR__.'/storage',
    ],
]);
...

$table = $this->table('undelivered_data');
$table->addColumn('table_name', 'string', ['null' => false]);
$table->addColumn('data', 'text', ['null' => false]);
$table->addColumn('status', 'integer', ['default' => 0, 'limit' => 1]);
$table->addTimestamps()->create();

$bufferSize = 50;
$validator = new SenderValidator();
$streamer    = new StreamerWrapper($validator, $bufferSize)

$mapperClass = $this->config['mappers']->get($body['table_name'], NullMapper::class);
$mapper = new $mapperClass;

$streamer->setMapper($mapper);
$streamer->validator->setClientData($data);
$streamer->send();

$validator = new SenderValidator();
$runner    = new RunnerWrapper($validator)

$mapperClass = $this->config['mappers']->get($body['table_name'], NullMapper::class);
$mapper = new $mapperClass;

$runner->setMapper($mapper);
$runner->validator->setClientData($data);
$runner->send();

$limit = 100;

$analytics = $this->getDI()->getShared('config')->analytics->toArray();
$provider  = new BigQuery($analytics);

do {
    $undeliveredDataService = new UndeliveredData($analytics->undeliveredDataModel);
    $undeliveredDataSet = $undeliveredDataService->findAllUndelivered($limit);

    foreach ($undeliveredDataSet as $undeliveredData) {
        $data = \json_decode($undeliveredData->data, true);
        $bigQuery->setTable($undeliveredData->table_name);
        if ($provider->insert($data)) {
            $undeliveredData->delete();
        }
    }
} while ($undeliveredDataSet->count() >= $limit);

$query = "SELECT * FROM holding.chocolife_test WHERE created_at = \"2018-11-20\" LIMIT 100"
$provider = new BigQuery($this->getDI()->getShared('config')->analytics->toArray());
$result = $provider->runQuery($query);