hadoop 进阶实例

来源:互联网 发布:unity3d灯光教程 编辑:程序博客网 时间:2024/05/16 15:01

三个文件

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);




  




原创粉丝点击