简体   繁体   English

BigQuery [PHP] InsertAll错误:表数据追加请求中没有记录

[英]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... 但是似乎json(行)是问题所在...

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);
}

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. Google_Service_Bigquery_TableDataInsertAllRequestRows类应命名为_Row而不是_Rows,因为您必须创建_Rows对象数组才能传递请求。 Here is The code that finally worked. 这是最终起作用的代码。

Also, the json must be an object, not a json string. 另外,json必须是一个对象,而不是json字符串。 $data = the json_decode of the json string that was in the original question. $ 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);
}

Just wanted to share: I make a little change because the code above didn't worked for me: This line was a problem 只想分享:我做了一些更改,因为上面的代码对我不起作用:这行是个问题

 $this->service->tabledata->insertAll($this->project_id, 'log', 'v1', $request);

The problem is using the keyword $this->service. 问题是使用关键字$ this-> service。 That's because the code didn't run inside a class. 那是因为代码没有在类内运行。 Instead of $this, you should use the $service 代替$ this,您应该使用$ service

this is the full code of php google bigquery auth and php google bigquery insert that works for me: 这是对我有用的php google bigquery auth和php google bigquery插入的完整代码:

    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();
} }

This response mean that there is no errors. 此响应表示没有错误。 go to check in the bigQuery and see the values: 去检查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
        (
        )

)

Good luck all! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM