MapReduce练习(一)

MapReduce练习(一)

路由日志:

如下:

Apr 23 11:49:54 hostapd: wlan0: STA14:7d:c5:9e:fd:84

Apr 23 11:49:52 hostapd: wlan0: STA74:e5:0b:04:28:f2

Apr 23 11:49:50 hostapd: wlan0: STAcc:af:78:cc:d5:5d

Apr 23 11:49:44 hostapd: wlan0: STAcc:af:78:cc:d5:5d

Apr 23 11:49:52 hostapd: wlan0: STA74:e5:0b:04:28:f2

Apr 23 11:49:54 hostapd: wlan0: STA14:7d:c5:9e:fd:84

 

 

 

要求:

提取时间和MAC地址,删除其它部分。

 

代码解析:

 

package hadoop;
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 Luyou extends Configured implements Tool{
	
	//Counter即是一个计数器,可以记录这个程序的一些数据用于统计
	enum Counter{
		
		LINESKIP,      //出错的行	
	}
		
	public static class Map extends Mapper<LongWritable,Text,NullWritable,Text>{
		public void map(LongWritable key,Text value,Context context) throws IOException{		
			//读取源文件,line得到的就是输入文件的一行数据
			String line=value.toString();
			try{	
				//对源数据进行分割和重组			
				String [] lineSplit=line.split(" ");				
				String month =lineSplit[0];				
				String time=lineSplit[1];				
				String mac=lineSplit[6];				
				Text out=new Text(month+' '+time+' '+mac);					
				//把两个参数分别作为KEY和VALUE输出
				context.write(NullWritable.get(), out);			
			}catch(Exception e){	
				//如果发生异常,则指定计数器中的LINESKIP自增			
				context.getCounter(Counter.LINESKIP).increment(1);				
				return;			
			}		
		}	
	}
	
	public int run(String[] args) throws Exception {
		Configuration conf=getConf();	
		//任务名			
		Job job=new Job(conf,"Luyou");	
		//指定Class,必须是当前所在的Class名	
		job.setJarByClass(Luyou.class);	
		//输入路径			
		FileInputFormat.addInputPath(job,new Path(args[0]));	
		//输出路径	
		FileOutputFormat.setOutputPath(job, new Path(args[1]));	
		//调用上面Map类作为Map任务代码			
		job.setMapperClass(Map.class);		
		job.setOutputFormatClass(TextOutputFormat.class);	
		//指定输出KEY格式	
		job.setOutputKeyClass(NullWritable.class);	
		//指定输出VALUE格式	
		job.setOutputValueClass(Text.class);		
		job.waitForCompletion(true);	
		//输出任务完成情况	
		
		System.out.println("任务名称"+job.getJobName());		
		System.out.println("任务成功"+(job.isSuccessful()?"是":"否"));		
		System.out.println("跳过的行"+job.getCounters().findCounter(Counter.LINESKIP).getValue());
		
		return job.isSuccessful()?0:1;
	}
	
	public static void main(String args[]) throws Exception{
		//在main函数调用run方法,启动一个mr任务		
		int res=ToolRunner.run(new Configuration(), new Luyou(),args);		
		System.exit(res);	
	}

}

 

启动hadoop:

 

路由日志上传到hdfs:

 

 

 

 

导入的包:

 

Map函数:

 

Run方法:

 

主函数:

 

类名和记录出错的小函数:

 

设置hdfs输入输出路径:

 

运行结果:

如下:

如下:

 

 

结果:

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java API方式的MapReduce是Hadoop中最常用的方式之一,MapReduce是一种分布式计算模型,可用于对大规模数据进行处理和分析。下面是一个使用Java API方式的MapReduce练习: 假设我们有一个文本文件,其中包含一些单词和它们出现的次数,我们需要编写一个程序来统计每个单词出现的次数。我们可以使用MapReduce来完成这个任务。 1. 创建一个Java项目,并添加Hadoop依赖。 2. 创建一个Java类来实现Mapper接口。在这个类中,我们需要实现map()方法,这个方法将会接收到输入键值对,即每行文本内容和行号。我们需要将文本内容切分成单个单词,并将每个单词作为键,将值设置为1,表示出现次数为1。 ```java public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } ``` 3. 创建一个Java类来实现Reducer接口。在这个类中,我们需要实现reduce()方法,这个方法将会接收到键值对列表,每个键值对中的键为单词,值为出现次数。我们需要将每个单词的出现次数加起来,并将结果作为值输出。 ```java public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } ``` 4. 在主方法中,我们需要设置Job信息,并将Mapper和Reducer类设置为Job的输入和输出。还需要设置输入文件和输出文件的路径信息。 ```java public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 5. 在命令行中运行程序,并指定输入文件和输出文件的路径信息。 ```bash hadoop jar WordCount.jar /input /output ``` 以上就是使用Java API方式的MapReduce练习,通过这个练习,我们可以更深入地了解MapReduce的实现原理和使用方法,同时也能更好地掌握Hadoop的使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值