PHP来操作Hadoop

2 篇文章 0 订阅

还是按上次安装的Hadoop

cd /home/hadoop
mkdir test

vi user.txt:
1,张三,23,beijing,10086,
2,李四,34,shanghai,10000,
3,王五,20,beijing,10010,

vi mapper.php:
#!/usr/bin/php
<?php
$count = 0;
while($line = fgets(STDIN)) {
    $line = trim($line);
    $user = explode(',', $line);
    echo $user[3]." 1\n";
}

vi reducer.php:
#!/usr/bin/php
<?php
$result = array();
while($line = fgets(STDIN)) {
    list($city, $count) = explode(' ', $line);
    if(!isset($result[$city])) $result[$city] = 0;
    $result[$city] += $count;
}
foreach($result as $key=>$value){
    echo "$key $value\n";
}

chmod +x mapper.php
chmod +x reducer.php
把这个test同步到每个节点同样的位置上
调试:
cat user.txt|./mapper.php

cat user.txt | ./mapper.php | ./reducer.php

执行:
/usr/local/hadoop/bin/hdfs dfs -mkdir /user
/usr/local/hadoop/bin/hdfs dfs -mkdir /user/hadoop
/usr/local/hadoop/bin/hdfs dfs -mkdir /user/hadoop/input
/usr/local/hadoop/bin/hdfs dfs -put /home/hadoop/test/user.txt /user/hadoop/input

结果:
/usr/local/hadoop/bin/hadoop jar /usr/local/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.6.2.jar -input input/user.txt -output output2 -mapper /home/hadoop/test/mapper.php -reducer /home/hadoop/test/reducer.php

查看结果:
/usr/local/hadoop/bin/hdfs dfs -cat output2/*

如果发现文件夹已存在:
/usr/local/hadoop/bin/hdfs dfs -rm -r -f output2

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值