- 论坛徽章:
- 0
|
本帖最后由 zhanminga 于 2011-05-11 22:50 编辑
问题:80GB的文件5亿条记录与2000条记录的列表进行匹配,运行的速度很慢,求大虾指点
问题进展:20110506 貌似跑完20W用时39分钟。
20110509 一小时跑完100W数据。
20110510 22点 的 137分钟跑完87GB的数据,结果数据大小11.58GB(逻辑无问题)。
期待能并行或者其他的方法能进一步将时间缩短(在逻辑无问题的情况下)
-------------------------------------------------------------------------------------------------
环境: perl v5.8.2 + AIX 文件服务器上配置为 lcpu=8 mem=15360MB
文件wap_source (此文件大小为82GB,记录数为5亿条)内容如:
"8613488877513","NokiaE71","20110301235000","20110301235000","http://dh.uc.cn/?id=51394"
"8615116906023","MOT-A3000_CMCC/1.0 Release/09.09.2008 Profile/MIDP","20110301234959","20110301235000","http://3g.sina.com.cn/iask/ring/list.php?p=0&serial=436493081.365&vt=1&wm=4007&q=%E7%AD%89%E4%BD%A0%E7%88%B1%E6%88%91&p=dsymp3&w=dsymp31"
需进行匹配的文件vgop_wap_list(此文件记录数为2000条)内容如:
570,wap.baidu.com,ssid=0
120,mc60.z.qq.com,mc
580,dh.uc.cn,uc_common_param
580,dh.uc.cn
570,3g.sina.com.cn,iask
650,3g.sina.com.cn
匹配的最终结果内容如:
13488877513,20110301,580
15116906023,20110301,650
15116906023,20110301,570
#####################################################
代码判断:
中间判断为wap_source文件的网址及二级栏目与 文件列表vgop_wap_list中的网址及二级栏目进行匹配,
判断为:
1: wap_source中只有网址,那么直接跟vgop_wap_list中的网址进行匹配
2: wap_source中有网址并且有二级栏目,那么分2种情况与列表vgop_wap_list进行匹配
a) 两者的网址匹配成功并且二级栏目也匹配成功则print
b)wap_source中的网址与列表vgop_wap_list网址匹配成功(前提:列表中只有网址项,无二级栏目), 则print
#逗号分割
my @wap_r = split(/,/);
#对逗号分割后的最后一个元素,进行二次分割
#如有的url为http://3g.qq.com/news/?id=htpp:// 这类的数据进行,而且可以去掉前面的htpp:// 只取其网址及随后的二级栏目
my @my_url_d = split(/^http:\/\//,$wap_r[4]);
#对取出来的网址及二级栏目进行分割,并在下面可进行比较
my @url = split(/\//,$my_url_d[1]);
#####################################################
代码逻辑符合需要, 另外vgop_wap_list文件中的有一些大家提到的
许多形如“240,wap.baoruan.com,/download/datalist/list/class/signtoplist/" 这样的数据
这类数据我会在之后进行丢弃处理,先不予考虑,随后会把新的列表文件传上来。
解说一下:
此87GB的A文件分为5个接口而来(5个文件),所以考虑是否直接用5个接口文件来进行。另:是否能考虑并行 或多线程等方式奴
(每个接口的文件为10分钟下载一次,一个接口一天有6*24个文件,最后按接口合并为一个总的文件)
#####################################################
另附代码如下:
用iamlimeng 的程序 (20110510晚22点修改的代码为)
跑完整个87GB的单个文件 大概需要137分钟跑完,(感谢iamlimeng 抽出宝贵的时间)
(20110510晚22点修改的代码为)(代码测试通过)
用现有的vgop_wap_list列表文件,处理87GB的文件,处理完得结果文件为11.58GB 跟预估的11GB左右差不多,
耗时:8251 wallclock secs (7335.76 usr + 179.76 sys = 7515.52 CPU)- #!/usr/bin/perl
- use strict;
- my %list;
- open(IN,"vgop_wap_list")||die("wap list file not find!");
- while (<IN>) {
- chomp;
- my @temp = split /,/;
- if ($#temp == 2) { push(@{$list{2}{$temp[1]}{$temp[2]}},$temp[0]); }
- elsif ($#temp == 1) { push(@{$list{1}{$temp[1]}},$temp[0]); }
- }
- close IN;
- open(FH,">result.txt");
- open(FILEIN,"wap_source")||die("wap data file not find!");
- while (<FILEIN>) {
- if (/(.*?),(.*?),(.*?),(.*?),(.*)/) {
- my @my_url_d = split(/http:\/\//,$5);
- my @url = split(/\//,$my_url_d[1]);
- my $tel_no = substr($1,-12,11);
- my $start_time = substr($3,1,8);
- if ($#url > 0 && $list{2}{$url[0]}{$url[1]}) {
- print FH "$tel_no,$start_time,$_\n" foreach (@{$list{2}{$url[0]}{$url[1]}});
- }
- if ($list{1}{$url[0]}) {
- print FH "$tel_no,$start_time,$_\n" foreach (@{$list{1}{$url[0]}});
- }
- }
- }
- close FILEIN;
- close FH;
复制代码 (修改前)(代码测试通过)- #! perl
- #-----------------------------------
- # zhanming 20110506
- #-----------------------------------
- open(FILEIN,"wap_source")||die("wap data file not find!");
- while (<FILEIN>) {
- chomp;
- s/"//g;
- @wap_r = split(/,/);
- # print "一级分割".$#wap_r.",";
- if ($#wap_r eq 4) {
- @my_url_d = split(/^http:\/\//,$wap_r[4]);
- #二级分割
- @url = split(/\//,$my_url_d[1]);
- $tel_no = substr($wap_r[0],-11,11);
- $start_time = substr($wap_r[2],0,8);
- #
- open(FILEIN_LIST,"vgop_wap_list")||die("wap list file not find!");
- while (<FILEIN_LIST>) {
- chomp;
- @wap_list = split(/,/);
- if ($#url gt 0){
- #列表进行比对
- if ($#wap_list eq 2 ){
- if (($wap_list[1] eq $url[0]) && ($wap_list[2] eq $url[1]) ) {
- print $tel_no.",".$start_time.",".$wap_list[0]."\n";
- }
- }else{
- if ($wap_list[1] eq $url[0]) {
- print $tel_no.",".$start_time.",".$wap_list[0]."\n";
- }
- }
- #
- }else{
- if ($#wap_list eq 1 ){
- if ($wap_list[1] eq $url[0]) {
- print $tel_no.",".$start_time.",".$wap_list[0]."\n";
- }
- }
- }
- }
- close FILEIN_LIST;
- #
- }
- }
- close FILEIN;
复制代码 感谢iamlimeng 的帮助!
20110509修改后,但是速度还是很慢,一个小时跑完100W记录的匹配 为:- #!/usr/bin/perl
- use strict;
- use warnings;
- open(IN,"vgop_wap_list")||die("wap list file not find!");
- my @vgop_wap_list = <IN>;
- close IN;
- chomp @vgop_wap_list;
- my %vgop;
- foreach (@vgop_wap_list) {
- @{$vgop{$_}} = split /,/;
- }
- open(FILEIN,"wap_source_100w")||die("wap data file not find!");
- while (<FILEIN>) {
- if (/.*?,.*?,.*?,.*?,.*/) {
- chomp;
- s/"//g;
- my @wap_r = split(/,/);
- my @my_url_d = split(/^http:\/\//,$wap_r[4]);
- my @url = split(/\//,$my_url_d[1]);
- my $tel_no = substr($wap_r[0],-11,11);
- my $start_time = substr($wap_r[2],0,8);
- foreach (@vgop_wap_list) {
- if ($#url gt 0){
- if ($#{$vgop{$_}} eq 2) {
- if (${$vgop{$_}}[1] eq $url[0] && ${$vgop{$_}}[2] eq $url[1]){
- print $tel_no.",".$start_time.",".${$vgop{$_}}[0]."\n";
- }
- }
- else {
- if (${$vgop{$_}}[1] eq $url[0]) {
- print $tel_no.",".$start_time.",".${$vgop{$_}}[0]."\n";
- }
- }
- }
- else {
- if ($#{$vgop{$_}} eq 1 && ${$vgop{$_}}[1] eq $url[0]){
- print $tel_no.",".$start_time.",".${$vgop{$_}}[0]."\n";
- }
- }
- }
- }
- }
- close FILEIN;
复制代码 |
|