Bigquery PHP – 创建一个新数据集

huangapple go评论71阅读模式
英文:

Bigquery PHP - creating a new Dataset

问题

我正在尝试在我的项目中插入一个新的数据集:

$client = new Google_Client();

$client->setClientId("");
$client->setClientSecret("");
$client->setRedirectUri("");
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setScopes(array(Google_Service_Bigquery::BIGQUERY, Google_Service_Bigquery::BIGQUERY_INSERTDATA, Google_Service_Oauth2::USERINFO_EMAIL));
$client->setState("offline");  

$service = new Google_Service_Bigquery($client);

$service->datasets->insert("mynewdataset", null);

我收到以下异常信息:

Uncaught TypeError: Argument 2 passed to Google\Service\Bigquery\Resource\Datasets::insert() must be an instance of Google\Service\Bigquery\Dataset

如何创建一个数据集对象并如何配置它?这是正确的 API 文档吗?https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/insert

英文:

I am trying to insert a new Dataset in my project:

$client = new Google_Client();

$client->setClientId("");
$client->setClientSecret("");
$client->setRedirectUri("");
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setScopes(array(Google_Service_Bigquery::BIGQUERY, Google_Service_Bigquery::BIGQUERY_INSERTDATA, Google_Service_Oauth2::USERINFO_EMAIL));
$client->setState("offline");  

$service = new Google_Service_Bigquery($client);

$service->datasets->insert("mynewdataset", null);

And I'm getting this exception:

Uncaught TypeError: Argument 2 passed to Google\Service\Bigquery\Resource\Datasets::insert() must be an instance of Google\Service\Bigquery\Dataset

How to create a Dataset object and how to configure it? is this the correct API doc? https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/insert

答案1

得分: 0

这个链接对我的Java版本有帮助:https://stackoverflow.com/questions/19037703/missing-parameters-when-creating-new-table-in-google-bigquery-through-python-api

所以我的工作Java版本如下:

Dataset dataset = new Dataset();
dataset.setId("newdatasetid");

DatasetReference datasetReference = new DatasetReference();
datasetReference.setProjectId("bigquery-project-id");
datasetReference.setDatasetId("newdatasetid");

dataset.setDatasetReference(datasetReference);

com.google.api.services.bigquery.Bigquery.Datasets.Insert insertDataset = mBigquery.datasets().insert("bigquery-project-id", dataset);
insertDataset.execute();

根据这个,PHP中的第二个参数需要是一个带有数据集和项目ID的Dataset。

$datasetReference = new \Google\Service\Bigquery\DatasetReference();
$datasetReference->setDatasetId("datasetid");
$datasetReference->setProjectId("projectid");

$dataset = new \Google\Service\Bigquery\Dataset();
$dataset->setDatasetReference($datasetReference);
$this->mService->datasets->insert("projectid", $dataset);

至于如何避免这些问题,我看到这个文档版本中一些字段被标记为“required”:https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/

英文:

This helped with my Java version: https://stackoverflow.com/questions/19037703/missing-parameters-when-creating-new-table-in-google-bigquery-through-python-api

So my working Java version is this:

Dataset dataset = new Dataset();
dataset.setId("newdatasetid");

DatasetReference datasetReference = new DatasetReference();
datasetReference.setProjectId("bigquery-project-id");
datasetReference.setDatasetId("newdatasetid");

dataset.setDatasetReference(datasetReference);

com.google.api.services.bigquery.Bigquery.Datasets.Insert insertDataset = mBigquery.datasets().insert("bigquery-project-id", dataset);
insertDataset.execute();

Based on this, the second argument in PHP needs to be a Dataset that has a DatasetReference with the dataset and project IDs.

$datasetReference = new \Google\Service\Bigquery\DatasetReference();
$datasetReference->setDatasetId("datasetid");
$datasetReference->setProjectId("projectid");

$dataset = new \Google\Service\Bigquery\Dataset();
$dataset->setDatasetReference($datasetReference);
$this->mService->datasets->insert("projectid", $dataset);

As to how you would avoid these issues, I saw this doc version where some fields are marked as "required": https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/

huangapple
  • 本文由 发表于 2023年7月17日 22:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定