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

通过php在bigquery中执行插入操作,php,google-cloud-platform,google-bigquery,google-cloud-datastore,Php,Google Cloud Platform,Google Bigquery,Google Cloud Datastore,我正在我的谷歌云项目中集成一个bigQuery。我已经解决了集成大查询所需的所有需求。现在我想通过我的php文件执行插入操作。我在bigQuery中创建了一个数据集和表 数据集名称-用户详细信息 表名-用户信息 我想通过我的php文件在这个表中进行插入。在此之前,我正在云数据存储中保存用户详细信息,但现在我的需求已经改变,我想在bigQuery中保存这些详细信息。以下是我在云数据存储中插入值的代码: $datastore = new Google\Cloud\Datastore\Datast

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

  • 数据集名称-用户详细信息
  • 表名-用户信息
我想通过我的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);
类似地,我如何在大查询而不是数据存储中做到这一点


谢谢

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

你在这方面有很多例子

/**
*有关如何运行完整示例的说明:
*
*@见https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
*/
名称空间Google\Cloud\Samples\BigQuery;
//使用Composer包含Google云依赖项
需要一次目录//供应商/autoload.php';
如果(计数($argv)<4 |计数($argv)>5){
返回打印(“用法:php snippets/stream_row.php项目_ID DATASET_ID TABLE_ID[DATA]\n”);
}
列表($\$projectId,$datasetId,$tableId)=$argv;
$data=isset($argv[4])?json_解码($argv[4],true):[“field1”=>“value1”];
#[启动bigquery\u表\u插入\u行]
使用Google\Cloud\BigQuery\BigQueryClient;
/**在代码中取消注释并填充这些变量*/
//$projectId='谷歌项目ID';
//$datasetId='BigQuery数据集ID';
//$tableId='BigQuery表ID';
//$data=[
//“field1”=>“value1”,
//“field2”=>“value2”,
// ];
//实例化bigquery表服务
$bigQuery=新的BigQueryClient([
“projectId”=>$projectId,
]);
$dataset=$bigQuery->dataset($datasetId);
$table=$dataset->table($tableId);
$insertResponse=$table->insertRows([
['data'=>$data],
//其他行可以转到这里
]);
如果($insertResponse->issusccessful()){
打印('数据成功流入BigQuery'.PHP\u EOL);
}否则{
foreach($insertResponse->failedRows()作为$row){
foreach($row['errors']作为$error){
printf(“%s:%s”.PHP_EOL,$error['reason'],$error['message']);
}
}
}
#[结束bigquery\u表\u插入\u行]

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

你在这方面有很多例子

/**
*有关如何运行完整示例的说明:
*
*@见https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
*/
名称空间Google\Cloud\Samples\BigQuery;
//使用Composer包含Google云依赖项
需要一次目录//供应商/autoload.php';
如果(计数($argv)<4 |计数($argv)>5){
返回打印(“用法:php snippets/stream_row.php项目_ID DATASET_ID TABLE_ID[DATA]\n”);
}
列表($\$projectId,$datasetId,$tableId)=$argv;
$data=isset($argv[4])?json_解码($argv[4],true):[“field1”=>“value1”];
#[启动bigquery\u表\u插入\u行]
使用Google\Cloud\BigQuery\BigQueryClient;
/**在代码中取消注释并填充这些变量*/
//$projectId='谷歌项目ID';
//$datasetId='BigQuery数据集ID';
//$tableId='BigQuery表ID';
//$data=[
//“field1”=>“value1”,
//“field2”=>“value2”,
// ];
//实例化bigquery表服务
$bigQuery=新的BigQueryClient([
“projectId”=>$projectId,
]);
$dataset=$bigQuery->dataset($datasetId);
$table=$dataset->table($tableId);
$insertResponse=$table->insertRows([
['data'=>$data],
//其他行可以转到这里
]);
如果($insertResponse->issusccessful()){
打印('数据成功流入BigQuery'.PHP\u EOL);
}否则{
foreach($insertResponse->failedRows()作为$row){
foreach($row['errors']作为$error){
printf(“%s:%s”.PHP_EOL,$error['reason'],$error['message']);
}
}
}
#[结束bigquery\u表\u插入\u行]

Pentium感谢它的工作,但数据将在90分钟后显示在表中,如文档中所述。如何在插入操作后立即显示数据?ThanksIt最长为90分钟,通常为10-30秒。BigQuery是一个大数据工具,它不是为事务数据库设计的,所以永远不要期望即时的答案。为什么不使用runQuery方法插入值?这背后有什么原因吗?看看他们的名字,一个是插入,另一个是查询,不同的包装感谢它的工作,但数据将在90分钟后显示在表中,如文件中所述。如何在插入操作后立即显示数据?ThanksIt最长为90分钟,通常为10-30秒。BigQuery是一个大数据工具,它不是为事务数据库设计的,所以永远不要期望即时的答案。为什么不使用runQuery方法插入值?这背后有什么原因吗?只要看看他们的名字,一个是插入,另一个是查询,不同的包装你有一个低比率。重要信息:因此,您必须使用投票下方张贴答案左侧的勾号来标记已接受的答案。这将提高你的利率。通过访问此链接了解其工作原理:您的利率很低。重要信息:因此,您必须使用投票下方张贴答案左侧的勾号来标记已接受的答案。这将提高你的利率。通过访问此链接了解其工作原理:
/**
 * 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]