Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery [PHP] InsertAll Error: No records present in table data append request

Everything seems to me working except for the last line of this function. But it seems that the json (rows) is the problem...

Any help is appreciated!

Error:

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.

Table Schema:

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

Code:

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);
}
like image 588
KernelCurry Avatar asked Feb 15 '14 09:02

KernelCurry


1 Answers

The class Google_Service_Bigquery_TableDataInsertAllRequestRows should be named _Row not _Rows because you have to make an array of _Rows objects to pass the request. Here is The code that finally worked.

Also, the json must be an object, not a json string. $data = the json_decode of the json string that was in the original question.

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);
}
like image 64
KernelCurry Avatar answered Sep 25 '22 13:09

KernelCurry