function insertRow($dataset, $tableid, $arr) { $rows = array(); $row = new Google_Service_Bigquery_TableDataInsertAllRequestRows(); $row->setJson((object) $arr); //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. $row->setInsertId(strtotime('now') . rand(0, 1000000)); $rows[0] = $row; $request = new Google_Service_Bigquery_TableDataInsertAllRequest(); $request->setKind('bigquery#tableDataInsertAllRequest'); $request->setRows($rows); $this->service->tabledata->insertAll($this->project_id, $dataset, $tableid, $request); }
/** * @param IBigQueryWriteable[][] $groupedRows The rows to insert grouped by table name * * @return string[]|\Google_Service_Exception[] An array of errors encountered during the insert, indexed * by table name */ public function writeBatched(array $groupedRows) { // Remove empty datasets foreach ($groupedRows as $tableName => $rows) { if (count($rows) < 1) { unset($groupedRows[$tableName]); } } // Bail out if there is nothing to write if (count($groupedRows) < 1) { return []; } $startTime = floor(microtime(true) * 1000); $client = $this->getClient(); $service = $this->getService(); $dataSet = $this->getDataSet(); $client->setUseBatch(true); $batch = new \Google_Http_Batch($client); try { $totalRows = $writtenRows = 0; $requests = []; foreach ($groupedRows as $tableName => $rows) { /** @var IBigQueryWriteable[] $rows */ $request = new \Google_Service_Bigquery_TableDataInsertAllRequest(); $request->setKind('bigquery#tableDataInsertAllRequest'); $numRows = count($rows); $totalRows += $numRows; $bqRows = []; foreach ($rows as $queuedRow) { $row = new \Google_Service_Bigquery_TableDataInsertAllRequestRows(); $row->setJson($this->_serializeForBigQuery($queuedRow)); $row->setInsertId($queuedRow->getBigQueryInsertId()); $bqRows[] = $row; } $request->setRows($bqRows); $options = []; /** @var \Google_Http_Request $insertReq */ $insertReq = $service->tabledata->insertAll($this->bigQueryProject(), $dataSet, $tableName, $request, $options); $requests[$tableName] = $insertReq; $batch->add($insertReq, $tableName); } $responses = $batch->execute(); } finally { $client->setUseBatch(false); } $errors = []; foreach (array_keys($groupedRows) as $tableName) { if (isset($responses['response-' . $tableName])) { $response = $responses['response-' . $tableName]; if ($response instanceof \Google_Service_Exception) { $errors[$tableName] = $response->getMessage(); } else { if ($response instanceof \Google_Service_Bigquery_TableDataInsertAllResponse) { $insertErrors = $response->getInsertErrors(); if (!empty($insertErrors)) { $msg = $this->_makeErrorsMsg($insertErrors); $this->_debug('Errors inserting into table ' . $dataSet . '.' . $tableName . ': ' . $msg); $errors[$tableName] = $msg; } else { $writtenRows += count($groupedRows[$tableName]); } } else { $errors[$tableName] = 'Unknown response type: ' . get_class($response); } } } else { $errors[$tableName] = 'No response from BigQuery'; } } $duration = floor(microtime(true) * 1000) - $startTime; $this->_debug('Wrote ' . $writtenRows . ' of ' . $totalRows . ' rows to ' . count($groupedRows) . ' table(s) in ' . $duration . ' ms with ' . count($errors) . ' error(s)'); return $errors; }