我正在将bigQuery集成到我的Google云项目中。我已经解决了集成大查询所需的所有要求。现在,我想通过我的php文件执行插入操作。我已经在bigQuery中创建了数据集和表。
我想通过我的php文件在此表中插入。在此之前,我将用户详细信息保存在云数据存储中,但现在我的要求已更改,我想将这些详细信息保存在bigQuery中。这是我在 cloud数据存储中插入值的代码:
$datastore = new Google\Cloud\Datastore\DatastoreClient(['projectId' => 'google_project_id']);
$key = $datastore->key($entity_kind);
$key->ancestor(parent_kind, key);
$entity = $datastore->entity($key);
/*------------- Set user entity properties --------------*/
$entity['name'] = $username;
$entity['date_of_birth'] = strtotime(date('Y-m-d H:i'));
$entity['religion'] = $religion;
$entity->setExcludeFromIndexes(['religion']);
$datastore->insert($entity);
类似地,我该如何在大型查询而非数据存储区中做到这一点?
谢谢!
答案 0 :(得分:2)
在Bigquery中,此过程称为流插入。
上有很多例子/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
*/
namespace Google\Cloud\Samples\BigQuery;
// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';
if (count($argv) < 4 || count($argv) > 5) {
return print("Usage: php snippets/stream_row.php PROJECT_ID DATASET_ID TABLE_ID [DATA]\n");
}
list($_, $projectId, $datasetId, $tableId) = $argv;
$data = isset($argv[4]) ? json_decode($argv[4], true) : ["field1" => "value1"];
# [START bigquery_table_insert_rows]
use Google\Cloud\BigQuery\BigQueryClient;
/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $datasetId = 'The BigQuery dataset ID';
// $tableId = 'The BigQuery table ID';
// $data = [
// "field1" => "value1",
// "field2" => "value2",
// ];
// instantiate the bigquery table service
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->table($tableId);
$insertResponse = $table->insertRows([
['data' => $data],
// additional rows can go here
]);
if ($insertResponse->isSuccessful()) {
print('Data streamed into BigQuery successfully' . PHP_EOL);
} else {
foreach ($insertResponse->failedRows() as $row) {
foreach ($row['errors'] as $error) {
printf('%s: %s' . PHP_EOL, $error['reason'], $error['message']);
}
}
}
# [END bigquery_table_insert_rows]