protected function writeTable(CsvFile $csv, $outputTable, $incremental, $primaryKey) { try { $tableNameArr = explode('.', $outputTable); $bucketId = $tableNameArr[0] . "." . $tableNameArr[1]; $tableName = $tableNameArr[2]; } catch (ContextErrorException $e) { throw new UserException("Wrong output table name.", $e); } if (!count($csv->getHeader())) { throw new ApplicationException("Trying to upload an empty file"); } try { if (!$this->storageApi->bucketExists($bucketId)) { $bucketArr = explode('.', $bucketId); $this->storageApi->createBucket(str_replace('c-', '', $bucketArr[1]), SapiClient::STAGE_IN, 'DB Extractor data bucket'); } if (!$this->storageApi->tableExists($outputTable)) { $this->storageApi->createTableAsync($bucketId, $tableName, $csv, array('primaryKey' => $primaryKey)); } else { // handle unexpected temporary errors like "unable to fork()" $success = false; $exception = null; for ($i = 0; $i < 2 && !$success; $i++) { try { $this->storageApi->writeTableAsync($outputTable, $csv, array('incremental' => $incremental)); $success = true; } catch (\Exception $e) { $exception = $e; $this->logger->warning("Error writing to SAPI", ['exception' => $exception]); } sleep(1); } if (!$success) { throw $exception; } } } catch (ClientException $e) { if ($e->getCode() < 500) { throw new UserException($e->getMessage(), $e); } else { throw new ApplicationException($e->getMessage(), $e); } } $this->logger->info("Table " . $tableName . " imported to Storage API"); }
/** * * Upload CSV file to storage API * * @param $queryNumber * @param $table * @return bool * @throws \Exception * @throws \Keboola\StorageApi\ClientException */ private function _uploadCsvFile($queryNumber, $table) { if (!isset($this->_csvFiles[$queryNumber])) { return false; } $csvHandle = $this->_csvFiles[$queryNumber]["handle"]; $tableId = $this->storageApiBucket . '.' . $table; if (!isset($this->_sapiTableCache[$tableId]) && $this->storageApi->tableExists($tableId)) { $this->_sapiTableCache[$tableId] = $this->storageApi->getTable($tableId); } try { if (isset($this->_sapiTableCache[$tableId])) { $this->storageApi->writeTableAsync($tableId, $csvHandle, array("incremental" => true)); } else { $this->storageApi->createTableAsync($this->storageApiBucket, $table, $csvHandle, array("primaryKey" => "ex__primary")); $this->_sapiTableCache[$tableId] = $this->storageApi->getTable($tableId); } } catch (Exception $e) { throw new UserException($e->getMessage(), $e); } return true; }