Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP处理大文件_Php_Sorting_Large Files_File Handling - Fatal编程技术网

用PHP处理大文件

用PHP处理大文件,php,sorting,large-files,file-handling,Php,Sorting,Large Files,File Handling,我有一个大约10GB或更大的文件。该文件每行仅包含1到10之间的数字,而不包含其他数字。现在的任务是从文件中读取数据[数字],然后按升序或降序对数字进行排序,并使用排序后的数字创建一个新文件 有谁能帮我回答这个问题吗?我以前也遇到过类似的问题。试图操纵如此大的文件最终导致巨大的资源消耗,无法应对。我得到的最简单的解决方案是尝试使用名为LOAD data infle的快速数据转储函数将其导入MySQL数据库 一旦进入,您应该能够操作数据 或者,您可以逐行读取文件,同时将结果逐行输出到另一个文件中

我有一个大约10GB或更大的文件。该文件每行仅包含1到10之间的数字,而不包含其他数字。现在的任务是从文件中读取数据[数字],然后按升序或降序对数字进行排序,并使用排序后的数字创建一个新文件


有谁能帮我回答这个问题吗?

我以前也遇到过类似的问题。试图操纵如此大的文件最终导致巨大的资源消耗,无法应对。我得到的最简单的解决方案是尝试使用名为
LOAD data infle的快速数据转储函数将其导入MySQL数据库

一旦进入,您应该能够操作数据

或者,您可以逐行读取文件,同时将结果逐行输出到另一个文件中,并使用已排序的数字。不过,我不太确定这会有多好


您以前是否尝试过使用PHP,或者只是想找到一种可能的方法?

如果您不需要PHP(如果您手头有Linux maschine):

编辑:好的,这是您的PHP解决方案(如果您手头有Linux maschine):



:)

我假设这是一项家庭作业,目标是对RAM中存储的数据进行排序,而不是对内存中存储的数据进行排序

因为你只有数字1-10,这不是那么复杂的任务。只要打开你的输入文件,数一数你拥有的每一个特定数字的数量。之后,您可以构造简单循环并将值写入另一个文件。下面的例子是非常自我解释的

$inFile = '/path/to/input/file';
$outFile = '/path/to/output/file';
$input = fopen($inFile, 'r');
if ($input === false) {
    throw new Exception('Unable to open: ' . $inFile);
}
//$map will be array with size of 10, filled with 0-s
$map = array_fill(1, 10, 0);
//Read file line by line and count how many of each specific number you have
while (!feof($input)) {
    $int = (int) fgets($input);
    $map[$int]++;
}
fclose($input);
$output = fopen($outFile, 'w');
if ($output === false) {
    throw new Exception('Unable to open: ' . $outFile);
}
/*
 * Reverse array if you need to change direction between
 * ascending and descending order
 */
//$map = array_reverse($map);
//Write values into your output file
foreach ($map AS $number => $count) {
    $string = ((string) $number) . PHP_EOL;
    for ($i = 0; $i < $count; $i++) {
        fwrite($output, $string);
    }
}
fclose($output);
$infle='/path/to/input/file';
$outFile='/path/to/output/file';
$input=fopen($infle,'r');
如果($input==false){
引发新异常('无法打开:'。$infle);
}
//$map将是大小为10的数组,用0-s填充
$map=数组填充(1,10,0);
//逐行读取文件,并计算每个特定数字的数量
而(!feof($input)){
$int=(int)fgets($input);
$map[$int]++;
}
fclose(投入);
$output=fopen($outFile,'w');
如果($output==false){
抛出新异常('无法打开:'。$outFile);
}
/*
*如果需要在两个方向之间更改方向,请反转阵列
*升序和降序
*/
//$map=数组\反向($map);
//将值写入输出文件
foreach($map AS$number=>$count){
$string=((string)$number);
对于($i=0;$i<$count;$i++){
fwrite($output,$string);
}
}
fclose(产出);

考虑到您处理的是大文件这一事实,您还应该检查PHP环境的脚本执行时间限制,下面的示例对于10GB以上大小的文件将花费很长时间,但由于我在您的问题中没有看到任何关于执行时间和性能的限制,所以我假设它可以。

excel文件?csv文件?10GB的原始文本文件?甚至操作系统也不愿意打开这种大小的应用程序:|您有没有考虑过不使用PHP来实现类似的功能?PHP不是为这样的东西而设计的。
sort
命令使用
/tmp
dir来存储文件,因此
/tmp
空间不足和排序将失败。您可以使用
-T
开关指定要使用的任意临时目录。我想要一个使用PHP的解决方案。
<?php

// Sort ascending
`sort -n file > file_sorted-asc`;

// Sort descending
`sort -nr file > file_sorted-desc`;

?>
$inFile = '/path/to/input/file';
$outFile = '/path/to/output/file';
$input = fopen($inFile, 'r');
if ($input === false) {
    throw new Exception('Unable to open: ' . $inFile);
}
//$map will be array with size of 10, filled with 0-s
$map = array_fill(1, 10, 0);
//Read file line by line and count how many of each specific number you have
while (!feof($input)) {
    $int = (int) fgets($input);
    $map[$int]++;
}
fclose($input);
$output = fopen($outFile, 'w');
if ($output === false) {
    throw new Exception('Unable to open: ' . $outFile);
}
/*
 * Reverse array if you need to change direction between
 * ascending and descending order
 */
//$map = array_reverse($map);
//Write values into your output file
foreach ($map AS $number => $count) {
    $string = ((string) $number) . PHP_EOL;
    for ($i = 0; $i < $count; $i++) {
        fwrite($output, $string);
    }
}
fclose($output);