17,reducejoin

需求:将对应商品表的id换成对应的名字

数据:
和上一节的order pd 一样

Mapper:

public class JoinMapper extends Mapper<LongWritable, Text, Text,OrBeen >{

	@Override
	protected void map(LongWritable key,Text value, Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
	
		OrBeen or = new OrBeen();
		Text k = new Text();
		FileSplit inputSplit = (FileSplit)context.getInputSplit();
		//获得读取的路径名
		String path = inputSplit.getPath().getName();
		String line = value.toString();
		String[] fields = line.split("\t");
		if(path.equals("order.txt")) {
			or.setP_time(fields[0]);
			or.setP_id(fields[1]);
			or.setP_name("");
			or.setP_num(Integer.parseInt(fields[2]));
			or.setP_flag(0);
			//0:订单表
			k.set(fields[1]);
		}else {
			or.setP_time("");
			or.setP_id(fields[0]);
			or.setP_name(fields[1]);
			or.setP_num(0);
			or.setP_flag(1);
			k.set(fields[0]);
		}
		context.write(k, or);
	}
	

}

在Map端,我们通过自己构造一个类型,然后包括商品表pd.txt的字段,通过flag来判断是哪个表
在传递给Reduce,我们通过k来分别指定一组数据,例如商品id为01的为一组

OrBeen:

public class OrBeen implements Writable{

	@Override
	public String toString() {
		return p_time + "\t" + p_name +"\t" +p_num;
	}
	private String p_time ;
	private String p_id ;
	private int p_num;
	private String p_name;
	private int p_flag;
	public OrBeen(String p_time, String p_id, int p_num, String p_name, int p_flag) {
		super();
		this.p_time = p_time;
		this.p_id = p_id;
		this.p_num = p_num;
		this.p_name = p_name;
		this.p_flag = p_flag;
	}
	public OrBeen() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		p_time = in.readUTF();
		p_id = in.readUTF();
		p_num = in.readInt();
		p_name = in.readUTF();
		p_flag = in.readInt();
	}
	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeUTF(p_time);
		out.writeUTF(p_id);
		out.writeInt(p_num);
		out.writeUTF(p_name);
		out.writeInt(p_flag);
		
	}
	public String getP_time() {
		return p_time;
	}
	public void setP_time(String p_time) {
		this.p_time = p_time;
	}
	public String getP_id() {
		return p_id;
	}
	public void setP_id(String i) {
		this.p_id = i;
	}
	public int getP_num() {
		return p_num;
	}
	public void setP_num(int p_num) {
		this.p_num = p_num;
	}
	public String getP_name() {
		return p_name;
	}
	public void setP_name(String p_name) {
		this.p_name = p_name;
	}
	public int getP_flag() {
		return p_flag;
	}
	public void setP_flag(int p_flag) {
		this.p_flag = p_flag;
	}
	

}

Reduce:

public class JoinReducer extends Reducer<Text, OrBeen, OrBeen, NullWritable>{

	@Override
	protected void reduce(Text key, Iterable<OrBeen> value, Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		//存放订单表
		ArrayList<OrBeen>  or = new ArrayList<OrBeen>();
		//存放商品表
		OrBeen o = new OrBeen();
		
		for(OrBeen orbeen:value) {
			if(orbeen.getP_flag()==0) {
				OrBeen A = new OrBeen(); 
				try {
					//注意 BeanUtils,copyProperties只能copy同类型的,将orbeen先拷贝到A里面,然后通过or.add添加到集合中去
					BeanUtils.copyProperties(A, orbeen);
				} catch (IllegalAccessException | InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				or.add(A);
			}else {
				try {
					BeanUtils.copyProperties(o, orbeen);
				} catch (IllegalAccessException | InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		//将订单表对应的名字互换
		for(OrBeen k :or) {
			k.setP_name(o.getP_name());	
			context.write(k, NullWritable.get());
		
		}
		
	
		
		}
	
}

Job:

public class JoinJobDrive {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(JoinJobDrive.class);
		
		job.setMapperClass(JoinMapper.class);
		job.setReducerClass(JoinReducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(OrBeen.class);
		
		job.setOutputKeyClass(OrBeen.class);
		job.setOutputValueClass(NullWritable.class);
		
		FileInputFormat.setInputPaths(job, new Path("B:/笔记/数据素材/in"));
		FileOutputFormat.setOutputPath(job, new Path("B:/笔记/数据素材/out"));
		
		boolean b = job.waitForCompletion(true);
		System.out.println(b);
	}

}

以上就是reducejoin的案例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
city_info.txt文件 1 北京 华北 2 上海 华东 3 深圳 华南 4 广州 华南 5 武汉 华中 6 南京 华东 7 天津 华北 8 成都 西南 9 哈尔滨 东北 10 大连 东北 11 沈阳 东北 12 西安 西北 13 长沙 华中 14 重庆 西南 15 济南 华东 16 石家庄 华北 17 银川 西北 18 杭州 华东 19 保定 华北 20 福州 华南 21 贵阳 西南 22 青岛 华东 23 苏州 华东 24 郑州 华北 25 无锡 华东 26 厦门 华南 product_info.txt文件 1 商品_1 自营 2 商品_2 自营 3 商品_3 自营 4 商品_4 自营 5 商品_5 自营 6 商品_6 自营 7 商品_7 自营 8 商品_8 自营 9 商品_9 自营 10 商品_10 自营 11 商品_11 自营 12 商品_12 第三方 13 商品_13 第三方 user_visit_action.txt文件 2019-07-17 95 26070e87-1ad7-49a3-8fb3-cc741facaddf 37 2019-07-17 00:00:02 手机 -1 -1 \N \N \N \N 3 2019-07-17 95 26070e87-1ad7-49a3-8fb3-cc741facaddf 48 2019-07-17 00:00:10 \N 16 98 \N \N \N \N 19 2019-07-17 95 26070e87-1ad7-49a3-8fb3-cc741facaddf 6 2019-07-17 00:00:17 \N 19 85 \N \N \N \N 7 2019-07-17 38 6502cdc9-cf95-4b08-8854-f03a25baa917 29 2019-07-17 00:00:19 \N 12 36 \N \N \N \N 5 注意:txt文件都是没有表头的!!! 1.项目需求 1)统计最受欢迎的品类,先排序点击-再是订单-最后是支付 2)统计页面跳转率 3)不同区域内的热门商品Top3 4)自定义需求(哪一时间段访问人数最多,按小时统计/按日期统计/按四季统计) 给出完整scala代码使用
最新发布
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值