Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 /**
  * @param $project_id
  * @param $dataSet
  * @param $tableId
  * @param array $payload
  * @return \Exception|\Google_Service_Bigquery_TableDataInsertAllResponse
  */
 public function bigQueryStreaming($project_id, $dataSet, $tableId, $payload = array())
 {
     //check this instructions
     //http://blog.shinetech.com/2014/08/25/put-on-your-streaming-shoes/
     $rows = array();
     foreach ($payload as $k => $v) {
         $row = new \Google_Service_Bigquery_TableDataInsertAllRequestRows();
         $row->setJson($payload);
         //$row->setInsertId(null);
         //$row->setInsertId( strtotime('now') );
         $rows[] = $row;
     }
     $request = new \Google_Service_Bigquery_TableDataInsertAllRequest();
     $request->setKind('bigquery#tableDataInsertAllRequest');
     $request->setRows($rows);
     $response = $this->bigqueryService->tabledata->insertAll($project_id, $dataSet, $tableId, $request);
     $result = $response->getInsertErrors();
     if (!empty($result)) {
         $errors = array();
         $count = 0;
         foreach ($result as $error) {
             $vars = $this->get_object_vars_all($error);
             $errors['error_' . $count]['index'] = $error->index;
             $errors['error_' . $count]['error'] = $vars['modelData']['errors'];
             $count++;
         }
         $result = $errors;
     } else {
         $result = 'data was inserted';
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * @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;
 }