MapReduce练习 根据手机前三位获取运营商

采用本地模式

1.Map

package nuc.edu.ls.local;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
 * 
  199	1999704	吉林	松原	电信	138000	0438	220700
199	1999705	吉林	松原	电信	138000	0438	220700
199	1999706	吉林	松原	电信	138000	0438	220700
199	1999707	吉林	松原	电信	138000	0438	220700
199	1999708	吉林	松原	电信	138000	0438	220700
199	1999709	吉林	松原	电信	138000	0438	220700
199	1999710	吉林	长春	电信	130000	0431	220100
199	1999711	吉林	吉林	电信	132000	0432	220200
199	1999712	吉林	长春	电信	130000	0431	220100
 */
public class MapTask extends Mapper<LongWritable, Text, Text, Text> {

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		if(key.get()!=0) {
	        String[] split = value.toString().split("\\s+");
	        context.write(new Text(split[0]), new Text(split[4]));
		}
	}
}

2.Reduce

package nuc.edu.ls.local;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class ReduceTask extends Reducer<Text, Text, Text, Text> {
	@Override
	protected void reduce(Text arg0, Iterable<Text> arg1, Reducer<Text, Text, Text, Text>.Context arg2)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		arg2.write(new Text(arg0), new Text(arg1.iterator().next()));
	}
}

3.Driver

package nuc.edu.ls.local;

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;


public class Driver {
	public static void main(String[] args) throws Exception {
		System.setProperty("HADOOP_USER_NAME", "root");

		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "eclipseToCluster");

		job.setMapperClass(MapTask.class);
		job.setReducerClass(ReduceTask.class);
		 job.setJarByClass(Driver.class);
        //job.setJar("C:\\Users\\LENOVO\\Desktop\\WordCount.jar");
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
        
		FileInputFormat.addInputPath(job, new Path("d:/phone.txt"));
		FileOutputFormat.setOutputPath(job, new Path("d:/phone/"));

		boolean completion = job.waitForCompletion(true);
		System.out.println(completion ? 0 : 1);

	}
}

结果截图;

猜你喜欢

转载自blog.csdn.net/qq_39184715/article/details/81948996