三个文件
one.txt 格式如下
号码 标签 数量
two.txt 格式如下
号码 标签 数量
number.txt
133658207
需求是找出第三个文件中的号码,出现在前两个文件中的 号码 标签 数量
输出格式
号码 标签 数量
mapper.php
#!/usr/bin/php
<?php
error_reporting(0);
$in = fopen("php://stdin", "r");
$results = array();
while ( $line = fgets($in, 4096) )
{
if(empty($line)) {
continue;
}
$line = trim($line, PHP_EOL);
$temp = explode("\t", trim($line));
$key = $temp[0];
$value = $line;
print "$key\t$line\n"; //关键要加key ,reduce时按key排序
}
fclose($in);
reducer.php
#!/usr/bin/php
<?php
error_reporting(0);
ini_set('memory_limit','100M');
$in = fopen("php://stdin", "r");
$last_number = '';
$last_tag = '';
$last_amount = 0;
while ( $line = fgets($in, 4096) )
{
$temp = explode("\t", trim($line));
if(empty($line) || !isset($temp[1])) {
continue;
}
$number = $temp[1];
if($number == $last_number) {
if($last_tag) {
echo $last_number . ' ' . $last_tag . ' ' . $last_amount . "\n";
}else {
echo $temp[1] . ' ' . $temp[2] . ' ' . $temp[3] . "\n";
}
$last_number = '';
$last_amount = 0;
$last_tag = '';
} else {
$last_number = $temp[1];
if(isset($temp[2])) {
$last_tag = $temp[2];
$last_amount = $temp[3];
}else {
$last_tag = '';
$last_amount = 0;
}
}
}
fclose($in);