function loadFileToBigQuery($uri_array, $schema_array, $dataset, $table_name) { $loadConfig = new Google_Service_Bigquery_JobConfigurationLoad(); $loadConfig->setSchema($this->createSchema($schema_array)); $loadConfig->setSourceUris($uri_array); $loadConfig->setDestinationTable($this->createDestinationTable($dataset, $table_name)); $loadConfig->setCreateDisposition("CREATE_IF_NEEDED"); $loadConfig->setWriteDisposition("WRITE_APPEND"); $loadConfig->sourceFormat = 'CSV'; $config = new Google_Service_Bigquery_JobConfiguration(); $config->setDryRun(false); $config->setLoad($loadConfig); $job = new Google_Service_Bigquery_Job(); $job->setConfiguration($config); // this may throw exception: Google_Service_Exception $job = $this->service->jobs->insert($this->project_id, $job); return $job; }
/** * @param $project_id * @param $dataSet * @param $tableId * @param $csvPath * @param $skipLeadingRows * @return array|\Exception */ public function bigQueryLoadDataExistingTable($project_id, $dataSet, $tableId, $csvPath, $skipLeadingRows = 1) { // Information about the destination table $destination_table = new \Google_Service_Bigquery_TableReference(); $destination_table->setProjectId($project_id); $destination_table->setDatasetId($dataSet); $destination_table->setTableId($tableId); // Set the load configuration, including source file(s) and schema $load_configuration = new \Google_Service_Bigquery_JobConfigurationLoad(); //'gs://YOUR_GOOGLE_CLOUD_STORAGE_BUCKET/file.csv' $load_configuration->setSourceUris(array($csvPath)); $load_configuration->setDestinationTable($destination_table); $load_configuration->skipLeadingRows = $skipLeadingRows; $load_configuration->sourceFormat = 'CSV'; $load_configuration->setwriteDisposition('WRITE_APPEND'); $job_configuration = new \Google_Service_Bigquery_JobConfiguration(); $job_configuration->setLoad($load_configuration); $job = new \Google_Service_Bigquery_Job(); $job->setKind('load'); $job->setConfiguration($job_configuration); $jobs = $this->bigqueryService->jobs; $response = $jobs->insert($project_id, $job); $jobStatus = new \Google_Service_Bigquery_JobStatus(); $status = $response->getStatus(); if ($jobStatus->count() != 0) { $err_res = $jobStatus->getErrorResult(); return $err_res->getMessage(); } $jr = $response->getJobReference(); $jobId = $jr['jobId']; $state = $status['state']; return array('JOBID' => $jobId, 'STATUS' => $state); }