php - Bigquery query failure handling -
i trying make sure streaming queries inserted successfully, , seems though of these queries not being inserted correctly.
there no errors being thrown not using data returned insert request not know how errors structured.
here insertion code:
$rows = array(); $row = new google_service_bigquery_tabledatainsertallrequestrows; $row->setjson($data); $rows[0] = $row; $request = new google_service_bigquery_tabledatainsertallrequest; $request->setkind('bigquery#tabledatainsertallrequest'); $request->setrows($rows); return $this->service->tabledata->insertall($project, $dataset, $tableid, $request);
the above code excludes authentication/setting client & service
you can use method
/** * * @param google_client $client * @param type $project_id * @param type $dataset_id * @param type $rows * @return mixed * @throws google_service_exception */ public function bq_tabledata_insertall($client, $project_id, $dataset_id, $rows) { $success = true; $failed_lines = array(); $last_reason = ''; $ret = array( 'success' => &$success, 'last_reason' => &$last_reason, 'failed_lines' => &$failed_lines, ); $bq = new google_service_bigquery($client); $request = new google_service_bigquery_tabledatainsertallrequest(); $request->setrows($rows); try { $resp = new google_service_bigquery_tabledatainsertallresponse(); $resp = $bq->tabledata->insertall($project_id, $dataset_id, static::tableid(), $request); $errors = new google_service_bigquery_tabledatainsertallresponseinserterrors(); $errors = @$resp->getinserterrors(); if (!empty($errors)) { $error_msg = "\r\nrequest headers: \r\n" . json_encode($client->request->getrequestheaders()) . "\r\nresponse headers: \r\n" . json_encode($client->request->getresponseheaders()) . "\r\nrequest body:\r\n" . $client->request->getpostbody() . "\r\nresponse body:\r\n" . $client->request->getresponsebody() . "\r\n"; if (is_array($errors)) { foreach ($errors $ep) { $arr = $ep->geterrors(); $line = $ep->getindex(); if (is_array($arr)) { foreach ($arr $e) { switch ($e->getreason()) { case "stopped": break; case "timeout": $failed_lines[] = $line; $last_reason = $e->getreason(); $error_msg.= sprintf("timeout on line %s, reason: %s, msg: %s\r\n", $line, $e->getreason(), $e->getmessage()); break; default: $error_msg.= sprintf("error on line %s, reason: %s, msg: %s\r\n", $line, $e->getreason(), $e->getmessage()); break; } } } else { $error_msg.= json_encode($arr) . "\r\n"; } } $this->seterrormessage($error_msg); } else { $this->seterrormessage($errors); } //print_r($errors); //exit; $success = false; } return $ret; } catch (google_service_exception $e) { $this->seterrors($e->geterrors())->seterrormessage($e->getmessage()); throw $e; } }
Comments
Post a Comment