MapReduce实验——统计拨打公共服务号码的电话信息
作者:互联网
统计拨打公共服务号码的电话信息
Map类
package PhoneCollect;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class MyMap extends Mapper<LongWritable,Text,Text,Text> {
protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] lineArr = line.split(" ");
String anum = lineArr[0];
String bnum = lineArr[1];
context.write(new Text(bnum),new Text(anum));
}
}
Reduce类
package PhoneCollect;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class MyReduce extends Reducer<Text, Text,Text,Text> {
protected void reduce(Text key,Iterable<Text> it,Context context) throws IOException, InterruptedException {
String valueString;
String out = "";
for(Text value:it){
valueString = value.toString();
out += valueString+"|";
}
context.write(new Text(key),new Text(out));
}
}
Job类
package PhoneCollect;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
public class TestJob {
public static void main(String[] args) throws IOException {
//
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//
job.setJarByClass(TestJob.class);
job.setMapperClass(MyMap.class);
job.setReducerClass(MyReduce.class);
//
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//
FileInputFormat.setInputPaths(job,new Path("file:///simple/source.txt"));
FileOutputFormat.setOutputPath(job,new Path("file:///simple/output"));
}
}
标签:拨打,org,hadoop,MapReduce,job,公共服务,Text,apache,import 来源: https://blog.csdn.net/weixin_45740510/article/details/122240909