MapReduce编程:自定义分区和自定义计数器

文章目录

MapReduce编程:自定义分区和自定义计数器

一、实验目标

  1. 熟练掌握Mapper类,Reducer类和main函数的编写方法
  2. 掌握用mapreduce计算框架中本地聚合的基本用法
  3. 掌握MapReduce编程的自定义分区操作
  4. 掌握用mapreduce计算框架中partitioner的基本用法和实现效果
  5. 掌握MapReduce编程的自定义计数器操作

二、实验要求及注意事项

  1. 给出每个实验的主要实验步骤、实现代码和测试效果截图。
  2. 对本次实验工作进行全面的总结分析。
  3. 所有程序需要本地测试和集群测试,给出相应截图。
  4. 建议工程名,类名或包名等做适当修改,显示个人学号或者姓名

三、实验内容及步骤

实验任务1:自定义分区,mapreduce计算框架中partitioner可以将需要归为一类的数据输出到同一个文件中。使用mapreduce编程,读取文本文件,对其中的单词进行计数,并设置2个分区,将单词首字母包含"h"的放入第一个分区,其余的放入第二个分区。实现效果参考图1:

主要实现步骤和运行效果图:

(1)进入虚拟机并启动Hadoop集群,完成文件上传。

(2)启动Eclipse客户端,新建一个java工程;在该工程中创建package,导入jar包,完成环境配置,依次创建包、Mapper类,Reducer类和主类等;

(3)完成代码编写。

PartitionMap

java 复制代码
package hadoop;

import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

import org.apache.hadoop.io.*;

public class WjwPartitionMap extends Mapper<Object, Text, Text, IntWritable>{
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
    	String arr[] = value.toString().split(" ");
    	for(String word:arr){
    		context.write(new Text(word), new IntWritable(1));
    	}
    }
}

PartitionReduce

java 复制代码
package hadoop;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class WjwPartitionReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
		int sum = 0;
		for(IntWritable val:values){
			sum += val.get();
		}
		context.write(key, new IntWritable(sum));
	}

}

Partitioner

java 复制代码
package hadoop;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;

public class WjwPartitioner extends Partitioner<Text, IntWritable>{
	public int getPartition(Text key, IntWritable value, int numPartition){
		String str = key.toString();
		if(str.startsWith("h")){
			return 0;
		}else{
			return 1;
		}
	}

}

PartitionMain

java 复制代码
package hadoop;

import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;

public class WjwPartitionMain {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
		if(args==null || args.length!=2){
			System.out.println("error");
		}
		Job job = Job.getInstance(new Configuration(), "WjwPartitionMain");
		job.setJarByClass(WjwPartitionMain.class);
		job.setMapperClass(WjwPartitionMap.class);
		job.setReducerClass(WjwPartitionReduce.class);
		job.setNumReduceTasks(2);
		job.setPartitionerClass(WjwPartitioner.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.waitForCompletion(true);
	}

}

(4)测试程序,并查看输出结果。

实验任务2:自定义计数器,使用mapreduce编程,并在编程中利用自定义计数器统计sogou.txt文件中符合指定条件的uid等于"88f8ec0baee999cb1f30ffecda27cd86"出现的次数,实现效果如图2所示。

主要实现步骤和运行效果图:

(1)进入虚拟机并启动Hadoop集群,完成文件上传。

(2)启动Eclipse客户端,新建一个java工程;在该工程中创建package,导入jar包,完成环境配置,依次创建包、Mapper类,Reducer类和主类等;

(3)完成代码编写。

CountMap

java 复制代码
package hadoop;

import org.apache.hadoop.mapreduce.*;

import java.io.*;

import org.apache.hadoop.io.*;


public class WjwCountMap extends Mapper<Object, Text, Text, IntWritable>{
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
    	String arr[] = value.toString().split("\t");
    	String uid = arr[1];
    	Counter mycounter = context.getCounter("zidingyi","uid-counter");
    	if("88f8ec0baee999cb1f30ffecda27cd86".equals(uid)){
    		mycounter.increment(1);
    		context.write(new Text(uid), new IntWritable(1));
    	}
    }
}

CountReduce

java 复制代码
package hadoop;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class WjwCountReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
	@SuppressWarnings("unused")
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
		int sum = 0;
		for(IntWritable val:values){
			sum += 1;
		}
		context.write(key, new IntWritable(sum));
	}

}

CountMain

java 复制代码
package hadoop;

import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;

public class WjwCountMain {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
		if(args==null || args.length!=2){
			System.out.println("error");
		}
		Job job = Job.getInstance(new Configuration(), "WjwCountMain");
		job.setJarByClass(WjwCountMain.class);
		job.setMapperClass(WjwCountMap.class);
		job.setReducerClass(WjwCountReduce.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.waitForCompletion(true);
	}

}

(4)测试程序,并查看输出结果。

附:系列文章

实验 文章目录 直达链接
实验01 Hadoop安装部署 https://want595.blog.csdn.net/article/details/132767284
实验02 HDFS常用shell命令 https://want595.blog.csdn.net/article/details/132863345
实验03 Hadoop读取文件 https://want595.blog.csdn.net/article/details/132912077
实验04 HDFS文件创建与写入 https://want595.blog.csdn.net/article/details/133168180
实验05 HDFS目录与文件的创建删除与查询操作 https://want595.blog.csdn.net/article/details/133168734
实验06 SequenceFile、元数据操作与MapReduce单词计数 https://want595.blog.csdn.net/article/details/133926246
实验07 MapReduce编程:数据过滤保存、UID 去重 https://want595.blog.csdn.net/article/details/133947981
实验08 MapReduce 编程:检索特定群体搜索记录和定义分片操作 https://want595.blog.csdn.net/article/details/133948849
实验09 MapReduce 编程:join操作和聚合操作 https://want595.blog.csdn.net/article/details/133949148
实验10 MapReduce编程:自定义分区和自定义计数器 https://want595.blog.csdn.net/article/details/133949522
相关推荐
唐可盐34 分钟前
【数据治理实践】第 10 期:数据时代的导航——数据目录
大数据·人工智能·数据治理·数字化转型·数据资产·数据资产入表
智链RFID1 小时前
当企业运营遇到瓶颈:RFID 为什么越来越被采用?
大数据·网络·人工智能·科技·rfid
Data-Miner1 小时前
50页可编辑PPT | 某大数据资产平台建设项目启动会材料
大数据
旺仔Sec2 小时前
2026江苏省职业院校技能大赛新一代信息技术赛道大数据应用开发项目样题
大数据·数据挖掘
wx_xkq12882 小时前
营销智脑V3企业级AI平台架构设计:优秘智能完成从单点工具到全链路生态布局
大数据·人工智能
2601_955363152 小时前
技术赋能B端拓客:号码核验行业的革新与价值重构,氪迹科技法人,股东号码筛选系统,阶梯式价格
大数据·人工智能·重构
大大大大晴天️2 小时前
大数据实时计算-反压机制剖析
大数据·flink·spark streaming
zxsz_com_cn2 小时前
设备预测性维护数据采集方案设计
大数据
尽兴-3 小时前
仿京东电商商品搜索服务实战:基于 Elasticsearch 的实现与落地
大数据·elasticsearch·jenkins·建模·dsl查询
AI扑社3 小时前
AI+GEO 驱动的全新数字营销解决方案
大数据·人工智能·geo·ai搜索