2014-09-24 5 views
1

IRON IO 대기열을 사용하는 flysystem을 사용하고 있으며, 약 1,800 만 건의 레코드와 약 5,000 건의 레코드를 가져 오는 DB 쿼리를 실행하려고합니다.phpleague flysystem 서버의 대용량 파일을 읽고 쓰십시오.

여기
PHP Fatal error: Allowed memory size of ########## bytes exhausted 

내가 싶습니다 단계는 다음과 같습니다 :

1) 데이터

2 가져가)를 돌려 여기에 내가 50 + MB의 파일 크기와 수신하고있는 오류 메시지는 를 CSV 적절한 문자열 (예 : implode(',', $dataArray) . "\r\n")

3)이 경우 (S3를 서버에서 파일을 가져 오기)

4) 파일 '내용을 읽어 보시기 바랍니다이 새로운 strin를 추가로 내가 믿고있어

public function fire($job, $data) 
{ 
    // First set the headers and write the initial file to server 
    $this->filesystem->write($this->filename, implode(',', $this->setHeaders($parameters)) . "\r\n", [ 
     'visibility' => 'public', 
     'mimetype' => 'text/csv', 
    ]); 

    // Loop to get new sets of data 
    $offset = 0; 

    while ($this->exportResult) { 
     $this->exportResult = $this->getData($parameters, $offset); 

     if ($this->exportResult) { 
      $this->writeToFile($this->exportResult); 

      $offset += 5000; 
     } 
    } 
} 

private function writeToFile($contentToBeAdded = '') 
{ 
    $content = $this->filesystem->read($this->filename); 

    // Append new data 
    $content .= $contentToBeAdded; 

    $this->filesystem->update($this->filename, $content, [ 
     'visibility' => 'public' 
    ]); 
} 

이 가장 효율적이지 않다 : 그것과에 g S3 파일 여기

간단한 실행이 내가 가지고있는 코드 아래입니다 해당 내용을 다시 쓰기? 나는이 문서들에서 벗어날 것이다. PHPLeague Flysystem

누군가가 더 적절한 방향으로 나를 가리킬 수 있다면, 그것은 굉장 할 것이다!

+0

이 Flysystem를 사용하여 파일에 추가 할 수있는 방법이 있는가 확인하시기 바랍니다? 그렇지 않으면 나는 당신이 어떻게 많은 양의 메모리를 사용하는 것을 피할 수 있는지 보지 못한다. – andy

+0

@andy 아니 거기에있는 것처럼 보이지 않습니다. 그들은 스트림 기능을 가지고 있지만 그 내용을 업데이 트하기 위해 파일을 읽어야하는 것과 같은 방식으로 작동한다고 생각합니다. –

답변

1

S3로 작업하는 경우이 문제를 해결하기 위해 PHP 용 AWS SDK를 직접 사용합니다. 파일에 추가하는 작업은 실제로 SDK의 S3 스트림 래퍼를 사용하는 것이 매우 쉽습니다. 전체 파일을 메모리로 읽도록 강요하지는 않습니다.

$s3 = \Aws\S3\S3Client::factory($clientConfig); 
$s3->registerStreamWrapper(); 

$appendHandle = fopen("s3://{$bucket}/{$key}", 'a'); 
fwrite($appendHandle, $data); 
fclose($appendHandle); 
0

Flysystem/쓰기/업데이트 읽기 지원 스트림

최신 API https://flysystem.thephpleague.com/api/

$stream = fopen('/path/to/database.backup', 'r+'); 
$filesystem->writeStream('backups/'.strftime('%G-%m-%d').'.backup', $stream); 

// Using write you can also directly set the visibility 
$filesystem->writeStream('backups/'.strftime('%G-%m-%d').'.backup', $stream, [ 
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE 
]); 

if (is_resource($stream)) { 
    fclose($stream); 
} 

// Or update a file with stream contents 
$filesystem->updateStream('backups/'.strftime('%G-%m-%d').'.backup', $stream); 

// Retrieve a read-stream 
$stream = $filesystem->readStream('something/is/here.ext'); 
$contents = stream_get_contents($stream); 
fclose($stream); 

// Create or overwrite using a stream. 
$putStream = tmpfile(); 
fwrite($putStream, $contents); 
rewind($putStream); 
$filesystem->putStream('somewhere/here.txt', $putStream); 

if (is_resource($putStream)) { 
    fclose($putStream); 
} 
관련 문제