CY1223
CY1223

Reputation: 41

Using PHP with Google BigQuery

I am pretty new to Google Cloud Platform and for learning purposes I have created a simple restaurant finder using the Zomato API and hosted it on Google Cloud Platform.

Currently I have created a dataset with a CSV file I found online on the Google BigQuery platform and I ran a few simple queries like

SELECT restaurant_name FROM "restaurants" WHERE rating > 4.0

on the platform itself and the results are shown in a table format.

My question is there anyway to run simple queries using PHP to the dataset I created on BigQuery and display the results to my website hosted on the Google Cloud Platform ?

Upvotes: 1

Views: 3585

Answers (1)

Pentium10
Pentium10

Reputation: 207830

You would start something like this

 composer require google/cloud-bigquery

and a code sample is

require 'vendor/autoload.php';

use Google\Cloud\BigQuery\BigQueryClient;

$bigQuery = new BigQueryClient([
'keyFilePath' => '/path/to/keyfile.json'
]);

// Get an instance of a previously created table.
$dataset = $bigQuery->dataset('my_dataset');
$table = $dataset->table('my_table');

// Begin a job to import data from a CSV file into the table.
$loadJobConfig = $table->load(
    fopen('/data/my_data.csv', 'r')
);
$job = $table->runJob($loadJobConfig);

// Run a query and inspect the results.
$queryJobConfig = $bigQuery->query(
    'SELECT * FROM `my_project.my_dataset.my_table`'
);
$queryResults = $bigQuery->runQuery($queryJobConfig);

foreach ($queryResults as $row) {
    print_r($row);
}

More example on: https://github.com/googleapis/google-cloud-php

Upvotes: 3

Related Questions