- 論壇徽章:
- 0
|
本帖最后由 hu_zhuang163 于 2013-07-25 16:56 編輯
Perl 使用 Archive::Tar讀取 1G 的 Log文件 out of memory
系統(tǒng)環(huán)境: win7 使用的是activePerl 5.16.2
代碼如下:-
-
- my $logFile = "E:\\a\\";
- my @zipFiles = glob $logFile . "log_*.tar.gz"or die "can't find/read tar.gz $logFile ";
- foreach (@zipFiles) {
- my $fileList = $_;
- my $tar = Archive::Tar->new;
- $tar->read($fileList);
- my @files = $tar->list_files;
- foreach my $logName (@files) {
- if ( $logName =~ m/tanx_*/ ) {
- $tar->extract_file( $logName, "tanxTemp.out" );
-
- open( DATA, "<", "tanxTemp.out" ) || die "cannot open the file: $!\n";
- while ( my $line = <DATA> ) {
- my @lineAry = split /,/, $line;
- my $ip = $lineAry[8];
- $tanxIPHash{$ip}="1";
- }
- close(DATA);
- }
-
- }
- }
復(fù)制代碼 我大致知道原因 按照模塊說(shuō)明, 使用Archive::Tar這個(gè)模塊 read 方法 是把所有的內(nèi)容都讀取到內(nèi)存中在處理 , 可能就是這個(gè)導(dǎo)致的
然后我使用這樣-
- my $next = Archive::Tar->iter( "example.tar.gz", 1, {filter => qr/\.pm$/} );
- while( my $f = $next->() ) {
- print $f->name, "\n";
- $f->extract or warn "Extraction failed";
- # ....
- }
復(fù)制代碼 好像還是不行 ,哪位高手有處理大文件的經(jīng)驗(yàn)的 ,求鏈接 |
|