通过 php 在 bigquery 中执行插入操作



我正在我的谷歌云项目中集成一个bigQuery。我已经解决了集成大查询所需的所有要求。现在我想通过我的 php 文件执行插入操作。我在bigQuery中创建了一个数据集

  • 数据集名称 - 用户详细信息
  • 表名 - 用户信息

我想通过我的 php 文件插入此表。在此之前,我将用户详细信息保存在云数据存储中,但现在我的需求已更改,我想将这些详细信息保存在 bigQuery 中。这是我在云数据存储中插入值的代码:

$datastore = new GoogleCloudDatastoreDatastoreClient(['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);

同样,如何在大查询而不是数据存储中执行此操作?

谢谢!

在 Bigquery 中,此过程称为流式插入。

你在 Github 上有很多例子

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
*/
namespace GoogleCloudSamplesBigQuery;
// 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 GoogleCloudBigQueryBigQueryClient;
/** 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]

最新更新