BigQuery[PHP]InsertAll错误:表数据追加请求中不存在记录
在我看来,除了这个函数的最后一行之外,所有的东西都在工作。但json(行)似乎是问题所在 感谢您的帮助 错误:BigQuery[PHP]InsertAll错误:表数据追加请求中不存在记录,php,google-bigquery,Php,Google Bigquery,在我看来,除了这个函数的最后一行之外,所有的东西都在工作。但json(行)似乎是问题所在 感谢您的帮助 错误: Google_Service_Exception Error calling POST https://www.googleapis.com/bigquery/v2/projects/mtg/datasets/log/tables/v1/insertAll: (400) No records present in table data append request. 表架构: raw
Google_Service_Exception
Error calling POST https://www.googleapis.com/bigquery/v2/projects/mtg/datasets/log/tables/v1/insertAll: (400) No records present in table data append request.
表架构:
raw_url STRING NULLABLE
endpoint STRING NULLABLE
parameter STRING NULLABLE
client_ip STRING NULLABLE
account INTEGER NULLABLE
response_code INTEGER NULLABLE
response_time INTEGER NULLABLE
datetime TIMESTAMP NULLABLE
代码:
类Google_Service_Bigquery_TableDataInsertAllRequestRows应命名为_Row而非_Rows,因为您必须创建一个_Rows对象数组才能传递请求。下面是最终有效的代码 此外,json必须是对象,而不是json字符串$data=原始问题中json字符串的json_解码
public function insertLog($data)
{
$rows = array();
$row = new Google_Service_Bigquery_TableDataInsertAllRequestRows;
$row->setJson($data);
$row->setInsertId( strtotime('now') );
$rows[0] = $row;
$request = new Google_Service_Bigquery_TableDataInsertAllRequest;
$request->setKind('bigquery#tableDataInsertAllRequest');
$request->setRows($rows);
$this->service->tabledata->insertAll($this->project_id, 'log', 'v1', $request);
}
只是想分享:
我做了一些更改,因为上面的代码对我不起作用:
这条线是个问题
$this->service->tabledata->insertAll($this->project_id, 'log', 'v1', $request);
问题是使用关键字$this->service。这是因为代码没有在类中运行。您应该使用$service而不是$this 这是适用于我的php google bigquery auth和php google bigquery insert的完整代码:
define("CLIENT_ID", 'my client id');
define('KEY_FILE','key.p12');
define('SERVICE_ACCOUNT_NAME', 'its the email address as appear on the credential page');
define('PROJECT_ID', 'my project');
define('DATASET_ID', 'my data set');
define('TABLE_ID', 'my table');
$client_id = CLIENT_ID;
$service_account_name = SERVICE_ACCOUNT_NAME; //Email Address
$key_file_location = KEY_FILE;
// Cashing the client inside a session.
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
if (isset($_SESSION['service_token']))
{
$client->setAccessToken($_SESSION['service_token']);
}
else
{
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/bigquery'),
$key
);
$client->setAssertionCredentials($cred);
$client->setClientId(CLIENT_ID);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
}
$service = new Google_Service_Bigquery($client);
$data = array( 'fields1' => '1', 'sample' => '2','maker' => '2');
try {
$rows = array();
$row = new Google_Service_Bigquery_TableDataInsertAllRequestRows;
$row->setJson($data);
$row->setInsertId( strtotime('now') );
$rows[0] = $row;
$request = new Google_Service_Bigquery_TableDataInsertAllRequest;
$request->setKind('bigquery#tableDataInsertAllRequest');
$request->setRows($rows);
$response = $service->tabledata->insertAll(PROJECT_ID, DATASET_ID , TABLE_ID, $request);
print_r($response);
} catch (Exception $ex) {
echo $ex->getMessage();
} }
此响应意味着没有错误。进入bigQuery并查看值:
Google_Service_Bigquery_TableDataInsertAllResponse Object
(
[collection_key:protected] => insertErrors
[internal_gapi_mappings:protected] => Array
(
)
[insertErrorsType:protected] => Google_Service_Bigquery_TableDataInsertAllResponseInsertErrors
[insertErrorsDataType:protected] => array
[kind] => bigquery#tableDataInsertAllResponse
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
祝大家好运 问题是使用关键字$this->service。这是因为代码没有在类中运行。您应该使用$serviceThank,而不是$this!节省了很多时间!我有一个回购的例子,你愿意贡献这个代码吗?我可以写它。。。如果你想/有时间,你也可以。当然有正确的归因。
Google_Service_Bigquery_TableDataInsertAllResponse Object
(
[collection_key:protected] => insertErrors
[internal_gapi_mappings:protected] => Array
(
)
[insertErrorsType:protected] => Google_Service_Bigquery_TableDataInsertAllResponseInsertErrors
[insertErrorsDataType:protected] => array
[kind] => bigquery#tableDataInsertAllResponse
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)