首页 技术 正文
技术 2022年11月22日
0 收藏 498 点赞 2,807 浏览 2576 个字
package org.slp;import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;
import java.util.StringTokenizer;/**
* Created by sanglp on 2017/7/17.
*/
public class Test2Mapper extends Mapper<LongWritable ,Text,Text,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//super.map(key, value, context);
String line = value.toString();//一行数据代表一组好友关系
String[] ss = line.split("\t");
context.write(new Text(ss[0]),new Text(ss[1]));//主从分成两行输出
context.write(new Text(ss[1]),new Text(ss[0])); }
}
package org.slp;import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;/**
* Created by sanglp on 2017/7/17.
*/
public class Test2Reduce extends Reducer<Text,Text,Text,Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//super.reduce(key, values, context);
Set<String> set = new HashSet<String>();
for(Text t :values ){
set.add(t.toString());
}
if (set.size()>1){
for(Iterator j = set.iterator();j.hasNext();){
String name = (String)j.next();
for(Iterator k = set.iterator();k.hasNext();){
String other = (String)k.next();
if(!name.equals(other)){
context.write(new Text(name),new Text(other));
}
}
}
}
}
}
package org.slp;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;/**
* Created by sanglp on 2017/7/17.
*/
public class JobRun2 { public static void main(String[] args){
Configuration conf = new Configuration();
conf.set("mapred.job.tracker","node1:9001");
conf.set("mapred.job.tracker","node1:9001");
conf.set("mapred.jar","C:\\Users\\sanglp\\qq.jar");
try {
Job job = new Job(conf);
job.setJobName("qq");
job.setJarByClass(JobRun2.class);
job.setMapperClass(Test2Mapper.class);
job.setReducerClass(Test2Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setNumReduceTasks(1);//设置reduce任务的个数
//mapreduce输入数据所在目录或文件
FileInputFormat.addInputPath(job,new Path("/usr/input/qq"));
//mr执行之后的输出数据的目录
FileOutputFormat.setOutputPath(job,new Path("/usr/out/qq"));
try {
System.exit(job.waitForCompletion(true)?0:1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

文件内容例如:

小明  小李

小花  小白

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781