文件
hadoop hello
hdfs worldtom cat
cat dog
hello world
hello hdfs
输出
tom dog
dog tom
hello world
world hello
hdfs world
hdfs hadoop
world hdfs
world hadoop
hadoop hdfs
hadoop world
hello hdfs
hdfs hello
public class QQMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] ss = StringUtils.split(line, "\\s+");
context.write(new Text(ss[0]), new Text(ss[1]));
context.write(new Text(ss[1]), new Text(ss[0]));
}
}
public class QQReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Set<String> set = new HashSet<String>();
for (Text text : values) {
set.add(text.toString());
}
if (set.size() > 1) {
for (Iterator j = set.iterator(); j.hasNext();) {
String name = j.next();
for (Iterator i = set.iterator(); i.hasNext();) {
String other = i.next();
if (!name.equals(other)) {
context.write(new Text(name), new Text(other));
}
}
}
}
}
}