我有以下的輸入大的TSV文件:MapReduce的減速機的輸出錯誤
Site1 Tag1
Site1 Tag34
Site1 Tag8
Site2 Tag75
Site2 Tag54
Site2 Tag8
Site3 Tag24
Site3 Tag34
Site3 Tag1
...
我想找到,用Hadoop MapReduce的,在輸入和類似標籤的數量在所有這些站點之間的類似網站對幫助每一雙。
輸出的部分輸入提出:
Site1 Site2 1 // Site1 is similar to Site2 with 1 tag (Tag8)
Site1 Site3 2 // Site1 is similar to Site3 with 2 tag (Tag1 and Tag34)
Site2 Site1 1
Site3 Site1 2
我要輸出每每個站點只有10個最相似的網站。
每個站點有3個標籤
我想用2個MapReduce作業:
- 要映射標籤(密鑰)和網站,並通過標籤減少,在減少階段採取的所有站點特定的標籤並寫出輸出'標籤SiteX SiteY'
- 第二個MapReduce作業將接受第一個輸入並執行GROUP BY SiteX,SiteY對以獲得一對類似網站中類似標籤的數量。
我試圖實現第一個MAPRED,但我得到的只是「標記,站點」輸出。
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class RawToSimilarTagMapper {
public static class TagToSiteMapper extends Mapper<Object, Text, Text, Text> {
private Text site = new Text();
private Text tag = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String [] siteTag = value.toString().split("\t");
site.set(siteTag[0]);
tag.set(siteTag[1]);
context.write(tag, site);
System.out.println();
}
}
public static class SimilarSiteReducer extends Reducer<Text, Text, Text, Text> {
private Text value = new Text();
public void reduce(Text key, Iterable<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException, InterruptedException {
for (Text text : values) {
for (Text text2 : values) {
if (!text.equals(text2)) {
value.set(text.toString() + "\t" + text2.toString());
output.collect(key, value);
}
}
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "raw-to-similar");
job.setJarByClass(RawToSimilarTagMapper.class);
job.setMapperClass(TagToSiteMapper.class);
job.setCombinerClass(SimilarSiteReducer.class);
job.setReducerClass(SimilarSiteReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
FileSystem fs = null;
Path dstFilePath = new Path(args[2]);
try {
fs = dstFilePath.getFileSystem(conf);
if (fs.exists(dstFilePath))
fs.delete(dstFilePath, true);
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
我在做什麼錯在這裏?
同樣對於下一個階段,我怎麼能得到每個網站只有前10名最相似的網站?
對於第二/第三工作你也可以用豬腳本 –