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

问题描述 投票:0回答:1

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

  • 数据集名称 - userDetails
  • 表名 - userInfo

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

$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);

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

谢谢!

php google-cloud-platform google-bigquery google-cloud-datastore
1个回答
2
投票

在Bigquery中,此过程称为Streaming insert。

你有很多关于Github samples的例子

/**
 * 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]
© www.soinside.com 2019 - 2024. All rights reserved.