提取某日访问次数最多的那个IP

来源:互联网 发布:网络商标骗局 编辑:程序博客网 时间:2024/06/06 13:19
海量数据日志中,提取出某日访问次数最多的那个IP。

        思路:对于海量数据的处理,主要采取的策略就是分而治之,即缩减问题的规模,将一个大的问题划分成若干等价的小问题。然后解决这些小问题,最后将获得的小问题解综合起来,得出原问题的解。用到比较多的技术主要有散列、位图、堆、trie树、mapreduce、K路归并(败者树)等。其中散列用的尤为多。

         对于本问题,假定某日访问的IP地址已经从数据日志中提取出来,存放在一个大的二进制文件中。下面的工作主要是找目标IP——文件中出现次数最多的那个IP。这个文件很大,内存无法完全放下,内排序的方法行不通。可以采取如下措施:

        (1)利用散列函数,将大文件中的IP地址散列到若干个文件中。相同的IP地址肯定在同一个文件中。

        (2)处理每个小文件,找到该文件中出现次数最多的那个IP,记录下IP地址和出现次数。可以用hash_map,IP地址为键值、出现次数为数值。

        (3)将第(2)步中找到的IP地址及出现次数综合起来,找到这些IP地址中出现次数最多的那个IP。

         简单实现:接下来给出一种简单的实现,效率比较低。测试中,从一个含4亿个IP地址的文件中提取目标IP,一共用了52分钟。其中大量的时间用于文件的读写,约为30分钟。另外有7分钟用于产生含4亿个随机数的文件。真正用于计算的时间为15分钟。由于C++标准STL中没有hash_map,因此该用map实现第(2)步,如果改用hash_map,应该能减少部分计算的时间。

        另外,如果设置读写缓冲区,经过测试,缓冲区为128字节时,读写文件的时间从原来的30分钟减为25分钟左右。进一步增大缓冲区大小,提升的速度比非常小,待求解。这里设置缓冲区不是指这种方式:

char buffer[1024]; 

streambuf * ptrbuf = outFile.rdbuf(); 

 ptrbuf-> pubsetbuf(buffer,1024); 

而是定义一个整形数组,每次读写时,读写一块数据而不是一个整数。

单个读写   outFile.write((char*)&x,sizeof(unsigned));

块读写       outFile.write((char *)buffer,BUFFER_SIZE*sizeof(unsigned));

VC6.0下编译运行通过

view plain
  1. #pragma warning(disable:4786) //VC6.0中 忽略警告  
  2. #include <fstream>  
  3. #include <iostream>  
  4. #include <map>  
  5. #include <string>  
  6. #include <ctime>  
  7. using namespace std;  
  8.   
  9. const unsigned N=400000000;      //随机产生的IP地址数  
  10. const unsigned FILE_NUM=16;      //产生的小文件个数  
  11. const unsigned HASH_SHIFT=28;    //散列值的位移量  
  12.   
  13. inline unsigned HashInt(unsigned value); //将整数散列到0到FILE_NUM之间  
  14. bool ProduceIP(string fileName);                //随机产生IP地址,看成是32位无符号数  
  15. bool DecomposeFile(string fileName);     //分而治之,将大文件分为若干个小文件  
  16. bool FindTargetIP(unsigned result[2]);      //找到出现次数最多的IP  
  17.   
  18. int main()  
  19. {  
  20.     unsigned start,end;      //记录总的运行时间  
  21.     unsigned start1,end1;  //产生大文件的时间  
  22.     unsigned start2,end2;  //分解大文件的时间  
  23.     unsigned start3,end3;  //找出现IP次数最多的时间  
  24.   
  25.     string name="IP.bin";       //大文件  
  26.     unsigned result[2]={0,0};   //保存结果  
  27.   
  28.     start=clock();  
  29.     start1=clock();  
  30.     //随机产生大量IP  
  31.     if(ProduceIP(name)==false)  
  32.         return 1;  
  33.     end1=clock();  
  34.   
  35.     start2=clock();  
  36.     //分而治之  
  37.     if(DecomposeFile(name)==false)  
  38.         return 1;  
  39.     end2=clock();  
  40.   
  41.     start3=clock();  
  42.     //找到出现次数最多的IP  
  43.     if(FindTargetIP(result)==false)  
  44.         return 1;  
  45.     end3=clock();  
  46.     end=clock();  
  47.   
  48.     //打印结果  
  49.     cout<<"total run time : "<<(end-start)/1000.0<<endl;  
  50.     cout<<"ProduceIP() run time : "<<(end1-start1)/1000.0<<endl;  
  51.     cout<<"DecomposeFile() run time : "<<(end2-start2)/1000.0<<endl;  
  52.     cout<<"FindTargetIP() run time : "<<(end3-start3)/1000.0<<endl;  
  53.     cout<<"IP : "<<(result[0]>>24)<<'.'<<((result[0]&0x00ff0000)>>16)<<'.';  
  54.     cout<<((result[0]&0x0000ff00)>>8)<<'.'<<((result[0]&0x000000ff))<<endl;  
  55.     cout<<"appear time : "<<result[1]<<endl;  
  56.     return 0;  
  57. }  
  58. //将整数散列到0到FILE_NUM之间  
  59. inline unsigned HashInt(unsigned value)  
  60. {  
  61.     //斐波那契(Fibonacci)散列法 hash_key=(value * M) >> S;  
  62.     //value是16位整数,M = 40503   
  63.     //value是32位整数,M = 2654435769   
  64.     //value是64位整数,M = 11400714819323198485  
  65.     //S与桶的个数有数,如果桶的个数为16,那么S为28  
  66.     //对于32位整数,S=32-log2(桶的个数)  
  67.     return (value*2654435769)>>HASH_SHIFT;   
  68. }  
  69. //随机产生IP地址 看成是32位无符号数  
  70. bool ProduceIP(string fileName)  
  71. {  
  72.     ofstream outFile(fileName.c_str(),ios::binary);  
  73.     if(!outFile)  
  74.     {  
  75.         cerr<<"error: unable to open output file : "<<fileName<<endl;  
  76.         return false;  
  77.     }  
  78.     srand(time(0));  
  79.     for(unsigned i=0;i<N;i++)  
  80.     {  
  81.         //产生一个大整数用来模拟IP地址  
  82.         unsigned x=((rand()%256)<<24)|((rand()%256)<<16)|((rand()%256)<<8)|(rand()%256);  
  83.         outFile.write((char*)&x,sizeof(unsigned));  
  84.     }  
  85.     return true;  
  86. }  
  87. //分而治之 将大文件分为若干个小文件  
  88. bool DecomposeFile(string fileName)  
  89. {  
  90.     ofstream outFiles[FILE_NUM];  
  91.     int i;  
  92.     for(i=0;i<FILE_NUM;i++)  
  93.     {  
  94.         //小文件的名称  
  95.         char buffer[10];  
  96.         string name="tmp";  
  97.         itoa(i,buffer,10);  
  98.         name=name+buffer+".bin";  
  99.         //打开小文件  
  100.         outFiles[i].open(name.c_str(),ios::binary);  
  101.         if(!outFiles[i])  
  102.         {  
  103.             cerr<<"error: unable to open output file :"<<name<<endl;  
  104.             return false;  
  105.         }  
  106.     }  
  107.     ifstream inFile(fileName.c_str(),ios::binary);  
  108.     while(inFile.good())   
  109.     {  
  110.         //散列到各个小文件中  
  111.         unsigned int value=0;  
  112.         if(inFile.read((char*)&value,sizeof(unsigned)))  
  113.         {  
  114.             outFiles[HashInt(value)].write((char*)&value,sizeof(unsigned));  
  115.         }  
  116.     }  
  117.     //关闭文件  
  118.     inFile.close();  
  119.     for(i=0;i<FILE_NUM;i++)  
  120.         outFiles[i].close();  
  121.     return true;  
  122. }  
  123. //找到出现次数最多的IP  
  124. bool FindTargetIP(unsigned result[2])  
  125. {  
  126.     result[0]=0;  
  127.     result[1]=0;  
  128.     for(int i=0;i<FILE_NUM;i++)  
  129.     {  
  130.         char buffer[10];  
  131.         string name="tmp";  
  132.         itoa(i,buffer,10);  
  133.         name=name+buffer+".bin";  
  134.         //处理每个小文件  
  135.         ifstream inFile;  
  136.         inFile.open(name.c_str(),ios::binary);  
  137.         if(!inFile)  
  138.         {  
  139.             cerr<<"error: unable to open input file :"<<name<<endl;  
  140.             return false;  
  141.         }  
  142.         //核心代码,由于STL中没有hash_map,用map来代替  
  143.         map<unsigned,unsigned> ip_count;  
  144.         while(inFile.good())  
  145.         {  
  146.             unsigned key=0;  
  147.             if(inFile.read((char*)&key,sizeof(unsigned)))  
  148.             {  
  149.                 ip_count[key]++;  
  150.             }  
  151.         }  
  152.         map<unsigned,unsigned>::iterator it=ip_count.begin();  
  153.         for(;it!=ip_count.end();it++)  
  154.         {  
  155.             if(it->second>result[1])  
  156.             {  
  157.                 result[0]=it->first;  
  158.                 result[1]=it->second;  
  159.             }  
  160.         }  
  161.         inFile.close();  
  162.     }  
  163.     return true;  
  164. }  

设置缓冲区后的代码。问题描述见 “ 解题笔记(9)——提取某日访问次数最多的那个IP ”

view plain
  1. #pragma warning(disable:4786) //VC6.0中 忽略警告  
  2. #include <fstream>  
  3. #include <iostream>  
  4. #include <map>  
  5. #include <string>  
  6. #include <ctime>  
  7. using namespace std;  
  8.   
  9. const unsigned N=400000000;        //随机产生的IP地址数  
  10. const unsigned FILE_NUM=16;       //产生的小文件个数  
  11. const unsigned HASH_SHIFT=28;    //散列值的位移量  
  12. const unsigned BUFFER_SIZE=32;  
  13.   
  14. inline unsigned HashInt(unsigned value); //将整数散列到0到FILE_NUM之间  
  15. bool ProduceIP(string fileName);         //随机产生IP地址,看成是32位无符号数  
  16. bool DecomposeFile(string fileName);     //分而治之,将大文件分为若干个小文件  
  17. bool FindTargetIP(unsigned result[2]);   //找到出现次数最多的IP  
  18.   
  19. int main()  
  20. {  
  21.     unsigned start,end;    //记录总的运行时间  
  22.     unsigned start1,end1;  //产生大文件的时间  
  23.     unsigned start2,end2;  //分解大文件的时间  
  24.     unsigned start3,end3;  //找出现IP次数最多的时间  
  25.   
  26.     string name="IP.bin";       //大文件  
  27.     unsigned result[2]={0,0};   //保存结果  
  28.   
  29.     start=clock();  
  30.     start1=clock();  
  31.     //随机产生大量IP  
  32.     if(ProduceIP(name)==false)  
  33.         return 1;  
  34.     end1=clock();  
  35.   
  36.     start2=clock();  
  37.     //分而治之  
  38.     if(DecomposeFile(name)==false)  
  39.         return 1;  
  40.     end2=clock();  
  41.   
  42.     start3=clock();  
  43.     //找到出现次数最多的IP  
  44.     if(FindTargetIP(result)==false)  
  45.         return 1;  
  46.     end3=clock();  
  47.     end=clock();  
  48.   
  49.     //打印结果  
  50.     cout<<"total run time : "<<(end-start)/1000.0<<endl;  
  51.     cout<<"ProduceIP() run time : "<<(end1-start1)/1000.0<<endl;  
  52.     cout<<"DecomposeFile() run time : "<<(end2-start2)/1000.0<<endl;  
  53.     cout<<"FindTargetIP() run time : "<<(end3-start3)/1000.0<<endl;  
  54.     cout<<"IP : "<<(result[0]>>24)<<'.'<<((result[0]&0x00ff0000)>>16)<<'.';  
  55.     cout<<((result[0]&0x0000ff00)>>8)<<'.'<<((result[0]&0x000000ff))<<endl;  
  56.     cout<<"appear time : "<<result[1]<<endl;  
  57.     return 0;  
  58. }  
  59.   
  60. //将整数散列到0到FILE_NUM之间  
  61. inline unsigned HashInt(unsigned value)  
  62. {  
  63.     //斐波那契(Fibonacci)散列法 hash_key=(value * M) >> S;  
  64.     //value是16位整数,M = 40503   
  65.     //value是32位整数,M = 2654435769   
  66.     //value是64位整数,M = 11400714819323198485  
  67.     //S与桶的个数有数,如果桶的个数为16,那么S为28  
  68.     //对于32位整数,S=32-log2(桶的个数)  
  69.     return (value*2654435769)>>HASH_SHIFT;   
  70. }  
  71. //随机产生IP地址 看成是32位无符号数  
  72. bool ProduceIP(string fileName)  
  73. {  
  74.     ofstream outFile(fileName.c_str(),ios::binary);  
  75.     if(!outFile)  
  76.     {  
  77.         cerr<<"error: unable to open output file : "<<fileName<<endl;  
  78.         return false;  
  79.     }  
  80.     srand(time(0));  
  81.   
  82.     unsigned i,j=0;  
  83.     unsigned buffer[BUFFER_SIZE];  
  84.     for(i=0;i<N;i++)  
  85.     {  
  86.         //产生一个大整数用来模拟IP地址  
  87.         unsigned x=((rand()%256)<<24)|((rand()%256)<<16)|((rand()%256)<<8)|(rand()%256);  
  88.         buffer[j++]=x;  
  89.         if(BUFFER_SIZE==j)  
  90.         {  
  91.             outFile.write((char *)buffer,BUFFER_SIZE*sizeof(unsigned));  
  92.             j=0;  
  93.         }  
  94.     }  
  95.     outFile.write((char *)buffer,j*sizeof(unsigned));  
  96.     return true;  
  97. }  
  98. //分而治之 将大文件分为若干个小文件  
  99. bool DecomposeFile(string fileName)  
  100. {  
  101.     ofstream outFiles[FILE_NUM];  
  102.     int i;  
  103.     for(i=0;i<FILE_NUM;i++)  
  104.     {  
  105.         //小文件的名称  
  106.         char str[10];  
  107.         string name="tmp";  
  108.         itoa(i,str,10);  
  109.         name=name+str+".bin";  
  110.         //打开小文件  
  111.         outFiles[i].open(name.c_str(),ios::binary);  
  112.         if(!outFiles[i])  
  113.         {  
  114.             cerr<<"error: unable to open output file :"<<name<<endl;  
  115.             return false;  
  116.         }  
  117.     }  
  118.     ifstream inFile(fileName.c_str(),ios::binary);  
  119.   
  120.     unsigned buffer[FILE_NUM][BUFFER_SIZE];   
  121.     unsigned j[FILE_NUM]={0};  
  122.     while(inFile.good())  
  123.     {  
  124.         unsigned value;  
  125.         if(inFile.read((char*)&value,sizeof(unsigned)))  
  126.         {  
  127.             unsigned h=HashInt(value);  
  128.             buffer[h][j[h]++]=value;  
  129.             if(BUFFER_SIZE==j[h])  
  130.             {  
  131.                 outFiles[h].write((char *)buffer[h],BUFFER_SIZE*sizeof(unsigned));  
  132.                 j[h]=0;  
  133.             }  
  134.         }  
  135.     }  
  136.     for(i=0;i<FILE_NUM;i++)  
  137.         outFiles[i].write((char *)buffer[i],j[i]*sizeof(unsigned));  
  138.     //关闭文件  
  139.     inFile.close();  
  140.     for(i=0;i<FILE_NUM;i++)  
  141.         outFiles[i].close();  
  142.     return true;  
  143. }  
  144. //找到出现次数最多的IP  
  145. bool FindTargetIP(unsigned result[2])  
  146. {  
  147.     result[0]=0;  
  148.     result[1]=0;  
  149.     for(int i=0;i<FILE_NUM;i++)  
  150.     {  
  151.         char str[10];  
  152.         string name="tmp";  
  153.         itoa(i,str,10);  
  154.         name=name+str+".bin";  
  155.         //处理每个小文件  
  156.         ifstream inFile;  
  157.         inFile.open(name.c_str(),ios::binary);  
  158.         if(!inFile)  
  159.         {  
  160.             cerr<<"error: unable to open input file :"<<name<<endl;  
  161.             return false;  
  162.         }  
  163.         //核心代码,由于STL中没有hash_map,用map来代替  
  164.         map<unsigned,unsigned> ip_count;  
  165.         while(inFile.good())  
  166.         {  
  167.             unsigned buffer[BUFFER_SIZE];  
  168.             int readNum=0;  
  169.             inFile.read((char*)buffer,BUFFER_SIZE*sizeof(unsigned));  
  170.             readNum=inFile.gcount()>>2;  
  171.             for(int j=readNum;j>0;j--)  
  172.             {  
  173.                 ip_count[buffer[j-1]]++;  
  174.             }  
  175.         }  
  176.         map<unsigned,unsigned>::iterator it=ip_count.begin();  
  177.         for(;it!=ip_count.end();it++)  
  178.         {  
  179.             if(it->second>result[1])  
  180.             {  
  181.                 result[0]=it->first;  
  182.                 result[1]=it->second;  
  183.             }  
  184.         }  
  185.         inFile.close();  
  186.     }  
  187.     return true;  
  188. }  
原创粉丝点击