除了这个函数的最后一行,我觉得一切都在工作。但似乎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_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
代码:
public function insertLog()
{
$rows = new Google_Service_Bigquery_TableDataInsertAllRequestRows;
$rows->setJson('{"raw_url":"test","endpoint":"card.id","parameter":"1","client_ip":"127.0.0.1","account":1,"response_code":200,"response_time":1000,"created_at":"2014-02-14 19:16:21"}');
$rows->setInsertId("21");
$request = new Google_Service_Bigquery_TableDataInsertAllRequest;
$request->setKind('bigquery#tableDataInsertAllRequest');
$request->setRows($rows);
$this->service->tabledata->insertAll($this->project_id, 'log', 'v1', $request);
}
答案 0 :(得分:4)
类Google_Service_Bigquery_TableDataInsertAllRequestRows应该命名为_Row not _Rows,因为您必须创建一个_Rows对象数组来传递请求。这是最终有效的代码。
此外,json必须是一个对象,而不是一个json字符串。 $ data =原始问题中json字符串的json_decode。
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);
}
答案 1 :(得分:1)
只想分享: 我做了一点改动,因为上面的代码对我不起作用: 这条线是个问题
$this->service->tabledata->insertAll($this->project_id, 'log', 'v1', $request);
问题在于使用关键字$ this-> service。那是因为代码没有在类中运行。而不是$ this,你应该使用$ service
这是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
(
)
)
祝你好运!