This article will introduce you to a simple PHPShell big text data statistics and sorting implementation program. You can refer to it. This article will introduce you to a simple program for data statistics and sorting of PHP/Shell large files.
Script ec (2); script
Many large Internet companies have such a problem during interviews. There is a 4G file, how to use a machine with only 1 GB of memory to calculate the number of times the file appears to be multiple (assume that one row is an array, such as a QQ number ). If the file is only 4B or dozens of megabytes, the easiest way is to read the file and analyze the statistics. But this is a 4G file, of course, it may also be a file of dozens or even hundreds of GB, which is not solved by direct reading.
Similarly, for such a large file, it is certainly not feasible to simply use PHP. My idea is that no matter how large the file is, we must first cut it into small files that can be tolerated by multiple applications, then, analyze small files in batches or in sequence, summarize the overall results, and generate the final results that meet the requirements. Similar to the popular MapReduce model, the core idea is "Map" and "Reduce", coupled with Distributed File Processing, of course, all I can understand and use is after Reduce.
Assume that there is a 1 billion-row file with a QQ number ranging from 6 to 10 characters in each row, so what I need to solve is to calculate the 1 billion QQ numbers, the first 10 numbers with the most duplicates are generated using the following PHP script. It is very likely that this random number will not be repeated, but we suppose there will be repeated numbers here.
| The Code is as follows: |
|
|
$ Fp = fopen('qq.txt ', 'W + ');
For ($ I = 0; I I <1000000000; $ I ++ ){
$ Str = mt_rand (random, 9999999999). "n ";
Fwrite ($ fp, $ str );
}
Fclose ($ fp ); |
The world of file generation is relatively long. Using php-client to run php files in Linux will save time. Of course, you can also use other methods to generate files. The generated file is about 11 GB.
Then, use Linux Split to cut the file. The cut standard is one file for every 1 million rows of data.
| The Code is as follows: |
|
| Split-l 1000000-a 3 qq.txt qqfile |
Qq.txt is divided into 1000 files named qqfileaaa to qqfilebml, each of which is 11 Mb in size. Then, it is easy to use any processing method. I still use PHP for analysis and statistics:
| The Code is as follows: |
|
|
$ Results = array ();
Foreach (glob ('/tmp/qq/*') as $ file ){
$ Fp = fopen ($ file, 'R ');
$ Arr = array ();
While ($ qq = fgets ($ fp )){
$ Qq = trim ($ qq );
Isset ($ arr [$ qq])? $ Arr [$ qq] ++: $ arr [$ qq] = 1;
}
Arsort ($ arr );
// The following processing method has a problem
Do {
$ I = 0;
Foreach ($ arr as $ qq => $ times ){
If ($ I> 10 ){
Isset ($ results [$ qq])? $ Results [$ qq] + = $ times: $ results [$ qq] = $ times;
$ I ++;
} Else {
Break;
}
}
} While (false );
Fclose ($ fp );
}
If ($ results ){
Arsort ($ results );
Do {
$ I = 0;
Foreach ($ results as $ qq => $ times ){
If ($ I> 10 ){
Echo $ qq. "t". $ times. "n ";
$ I ++;
} Else {
Break;
}
}
} While (false );
} |
In this way, the first 10 samples are taken and finally put together for analysis and statistics. It is not ruled out that the number ranks 11th in each sample, but the total number is definitely the first 10, therefore, the statistical calculation algorithm must be improved later.
Some people may say that the awk and sort commands in Linux can be used to complete sorting, but I tried to implement it if it was a small file, but the 11g files cannot be used in both memory and time. The following is an awk + sort script I modified. Maybe there is something wrong with the writing method. Please kindly advise.
| The Code is as follows: |
|
|
Awk-F' \ @ ''{name [$1] ++} END {for (count in name) print name [count], count} 'qq.txt | sort-n> 123.txt |
With the growth of the Internet at a geometric level, there will be a huge demand for both big file processing and big data.