Mapreduce 小例子

对应与map,reduce函数操作列举个小例子,操作平台eclipse与hadoop。输入文件要求:可以是一行对应多个字段,且每个字段需要使用空格分割。

Test_1:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Test_1 extends Configured implements Tool{

	/**
	 * @Author XD 2014-8-15 
	 */
	enum Counter{
		LINESKIP,	//对应于出错行的计数
	}
	public static class Map extends Mapper<LongWritable,Text,NullWritable,Text>{
		public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
			//读取源文件,line得到的是输入一行的数据
			String line = value.toString(); 
			try{
				//对文件进行分割
				String[] lineSplit = line.split(" ");
				String name = lineSplit[0];
				String id = lineSplit[2];
				String school = lineSplit[3];
				Text out = new Text(name+' '+id+' '+school+' ');
				
				context.write( NullWritable.get(),out);//要与上面的Mapper接口中的输入输出类型匹配
			
			}catch(java.lang.ArrayIndexOutOfBoundsException e){
				context.getCounter(Counter.LINESKIP).increment(1);
				return;
			}
		}
		
	}
	public int run(String[] args)  throws  Exception{
		//配置,初始化作业
		Configuration conf = getConf();
		Job job = new Job(conf,"Test_1");
		job.setJarByClass(Test_1.class);
		
		//设置输入输出路径
		FileInputFormat.addInputPath(job,new Path(args[0]));
		FileOutputFormat.setOutputPath(job,new Path(args[1]));
		
		//设置处理map的处理类
		job.setMapperClass(Map.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		
		//设置map输出类型
		job.setOutputKeyClass(NullWritable.class);
		job.setOutputValueClass(Text.class);
		
		job.waitForCompletion(true);
		
		System.out.println("任务名称: "+job.getJobName());
		System.out.println("任务成功: "+(job.isSuccessful()?"Yes":"No"));
		System.out.println("跳过行数:"+job.getCounters().findCounter(Counter.LINESKIP).getValue());
			
		return job.isSuccessful()? 0:1;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	 try {
		int result = ToolRunner.run(new Configuration(),new Test_1(),args);
		System.out.println(result);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

}
此例并不具备reduce函数,只是一个简单的map处理。

Test_2:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Test_2 extends Configured implements Tool{

	/**
	 * @Author XD 2014-8-15 
	 */
	enum Counter{
		LINESKIP,
	}
	public static class Map extends Mapper<LongWritable,Text,Text,Text>{
		public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
			//读取源文件,line得到的是输入一行的数据
			String line = value.toString(); 
			try{
				String[] lineSplit = line.split(" ");
				String anum = lineSplit[0];
				String bnum = lineSplit[1];
				context.write(new Text(bnum),new Text(anum));
				
			}catch(java.lang.ArrayIndexOutOfBoundsException e){
				context.getCounter(Counter.LINESKIP).increment(1);
				return;
			}
		}
		
	}
	public static class Reduce extends Reducer<Text,Text,Text,Text>{
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
			String valueString;
			String out = "";
			//对于map产生的数据进行reduce 处理
			for(Text value : values){
				valueString = value.toString();
				out += valueString+"|";	
			}
			//reduce 输出
				context.write(key,new Text(out));
		}
		
	}
	public int run(String[] args)  throws  Exception{
		//作业初始化
		Configuration conf = getConf();
		Job job = new Job(conf,"Test_2");
		job.setJarByClass(Test_2.class);
		
		//输入输出路径
		FileInputFormat.addInputPath(job,new Path(args[0]));
		FileOutputFormat.setOutputPath(job,new Path(args[1]));
		
		//处理map,reduce的类
		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		
		//reduce的输出类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		job.waitForCompletion(true);
		
		System.out.println("任务名称: "+job.getJobName());
		System.out.println("任务成功: "+(job.isSuccessful()?"Yes":"No"));
		System.out.println("跳过行数:"+job.getCounters().findCounter(Counter.LINESKIP).getValue());
			
		return job.isSuccessful()? 0:1;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	 try {
		int result = ToolRunner.run(new Configuration(),new Test_2(),args);
		System.out.println(result);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

}
此例对应与map,reduce操作,只是个简单的小例子,只是为了实验运行环境,并不代表什么。书写程序的工作,可以在eclipse下面进行,这样易于修改和调试。但是运行建议在终端输入命令运行...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spark是一个基于内存计算的大数据处理框架,而MapReduceHadoop的分布式计算框架。虽然它们都用于处理大规模数据,但在很多方面存在差异。 首先,Spark采用了基于内存的计算模型,这使得它在处理大规模数据时拥有更高的速度和性能。相比之下,MapReduce通常需要将中间结果写入磁盘,这增加了IO开销,并且在迭代计算上性能较低。 其次,Spark提供了更丰富的API,使得开发人员可以更灵活地进行数据处理。它支持多种编程语言,如Java、Scala和Python,并提供了各种高级函数和算法库,如SQL、机器学习和图计算等。相比之下,MapReduce只提供了基本的Map和Reduce函数,并且需要开发人员编写复杂的代码来实现更复杂的数据处理逻辑。 此外,Spark还具备更好的容错性和扩展性。它使用了弹性分布式数据集(RDD)作为基本的数据抽象,并且提供了lineage机制来实现容错处理。另外,Spark还支持在集群上并行执行任务,可以更好地利用集群资源,提高处理效率。 最后,Spark还提供了更灵活的调度和资源管理机制。它可以与各种集群管理器(如YARN、Mesos和Standalone)集成,可以根据需求动态分配资源,并且可以将任务调度到离数据最近的节点,减少数据传输开销。相比之下,MapReduce通常需要手动配置作业和任务,并且不支持动态资源分配。 综上所述,Spark相对于MapReduce具有更高的性能、更丰富的API、更好的容错性和扩展性以及更灵活的调度和资源管理机制。Spark在大规模数据处理方面具有广泛的应用场景,并且正在成为大数据处理的主流框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值