简体   繁体   English

PHP BigQuery 客户端错误代码:401 running long job

[英]PHP BigQuery Client Error Code: 401 running long job

I got a PHP BigQueryClient that I use to export big tables to csv from BQ, but after running for a while they throw and error with code 401:我得到了一个 PHP BigQueryClient,我用它从 BQ 将大表导出到 csv,但在运行一段时间后它们抛出错误代码 401:

 Google\Cloud\Core\Exception\ServiceException 

  {
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",     
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

it occurs when running a long while that goes over the rows of the table like this它发生在长时间运行时会像这样遍历表格的行

$bigQuery = new BigQueryClient([
            'keyFile' => $array[$key],
            'projectId' => $this->projectId
        ]);

        //This is to get total row count so we can loop over it
        $info = $bigQuery->dataset($this->datasetId)->table($tableName)->info();

        //Output row count, and status message
        $this->info($this->messagePrepend . "Rows: " . number_format($info['numRows']) . ", Chunk size: " . number_format($this->chunkSize) . " – Processing.." . PHP_EOL);

        $startTime = Carbon::now();

        //Open our output file
        $file = fopen(storage_path("tmp/{$tableName}.csv"), 'w');
        fputcsv($file, $this->tableHeaders);

        $orderBy = match ($keyword) {
            default => "id"
        };

        while ($info['numRows'] > $offset) {

            $config = $bigQuery->query("SELECT * FROM {$this->datasetId}.{$tableName} ORDER BY {$orderBy} ASC LIMIT {$this->chunkSize} OFFSET {$offset}");
            $job = $bigQuery->startQuery($config);
            $queryResults = collect($job->queryResults());

            $queryResults->map(function ($row) {
                $line = [FORM LINE]

                fputcsv($file, $line);
            });

            $offset += $this->chunkSize;
        }

I've tried looking for the causes for this, and it seems like the issue is that the client token expires after an hour.我已经尝试寻找造成这种情况的原因,问题似乎是客户端令牌在一小时后过期。

Despite that I've not found a way to refresh it, cause it doesn't seem to do so automatically, could anyone help me figure out how to do that?尽管如此,我还没有找到刷新它的方法,因为它似乎不会自动刷新,谁能帮我弄清楚该怎么做?

I've read cloud bigquery docs but they didn't provide me with much answers, and neither did google我读过云 bigquery 文档,但他们没有给我提供太多答案,谷歌也没有

Resolved it by updating the instance of BigQueryClient when an exception is thrown:通过在抛出异常时更新 BigQueryClient 的实例来解决它:

try {
    $job = $bigQuery->startQuery($config);
} catch (\Throwable $th) {
    $this->warn("Code: " . $th->getCode() . ": " . $th->getMessage(), 'vvv');
    $bigQuery = new BigQueryClient([
        'keyFile' => $array[$key],
        'projectId' => $this->projectId
    ]);

    $job = $bigQuery->startQuery($config);
}

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

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