次のコードを見てください。
wcmapper.php (Hadoop ストリーミング ジョブのマッパー)
#!/usr/bin/php
<?php
//sample mapper for hadoop streaming job
$word2count = array();
// input comes from STDIN (standard input)
while (($line = fgets(STDIN)) !== false) {
// remove leading and trailing whitespace and lowercase
$line = strtolower(trim($line));
// split the line into words while removing any empty string
$words = preg_split('/\W/', $line, 0, PREG_SPLIT_NO_EMPTY);
// increase counters
foreach ($words as $word) {
$word2count[$word] += 1;
}
}
// write the results to STDOUT (standard output)
foreach ($word2count as $word => $count) {
// tab-delimited
echo "$word\t$count\n";
}
?>
wceducer.php (サンプル Hadoop ジョブのリデューサー スクリプト)
#!/usr/bin/php
<?php
//reducer script for sample hadoop job
$word2count = array();
// input comes from STDIN
while (($line = fgets(STDIN)) !== false) {
// remove leading and trailing whitespace
$line = trim($line);
// parse the input we got from mapper.php
list($word, $count) = explode("\t", $line);
// convert count (currently a string) to int
$count = intval($count);
// sum counts
if ($count > 0) $word2count[$word] += $count;
}
ksort($word2count); // sort the words alphabetically
// write the results to STDOUT (standard output)
foreach ($word2count as $word => $count) {
echo "$word\t$count\n";
}
?>
このコードは、commoncrawl データセットで PHP を使用した Wordcount ストリーミング ジョブ用です。
ここでは、これらのコードは入力全体を読み取ります。これは私が必要としているものではありません。最初の 100 行を読み取って、テキスト ファイルに書き込む必要があります。私は Hadoop、CommonCrawl、および PHP の初心者です。それで、どうすればこれを行うことができますか?
助けてください。