MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序

文件如下:


这个案例主要考察我们对排序的理解,我们可以这样做:

代码如下(由于水平有限,不保证完全正确,如果发现错误欢迎指正):

①建一个TestBean

package com;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class TestBean implements WritableComparable<TestBean>{
private long a;
private long b; public TestBean() {
super();
}
public TestBean(long a, long b) {
super();
this.a = a;
this.b = b;
}
public long getA() {
return a;
}
public void setA(long a) {
this.a = a;
}
public long getB() {
return b;
}
public void setB(long b) {
this.b = b;
}
@Override
public String toString() {
return a + " " + b;
}
@Override
public void readFields(DataInput in) throws IOException {
this.a=in.readLong();
this.b=in.readLong();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(a);
out.writeLong(b); }

//排序主要代码
@Override
public int compareTo(TestBean o) {
long i=this.a-o.getA();
if(i==){
long j=this.b-o.getB();
return (int) j;
}else{
return (int)i;
}
}
}

②测试代码:

package com;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class TestCount {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.0.100:9000");
config.set("yarn.resourcemanager.hostname", "192.168.0.100"); FileSystem fs = FileSystem.get(config); Job job = Job.getInstance(config); job.setJarByClass(TestCount.class); //设置所用到的map类
job.setMapperClass(myMapper.class);
job.setMapOutputKeyClass(TestBean.class);
job.setMapOutputValueClass(NullWritable.class); //设置用到的reducer类
job.setReducerClass(myReducer.class);
job.setOutputKeyClass(TestBean.class);
job.setOutputValueClass(NullWritable.class); //设置输出地址
FileInputFormat.addInputPath(job, new Path("/input/order.txt")); Path path = new Path("/output/"); if(fs.exists(path)){
fs.delete(path, true);
} //指定文件的输出地址
FileOutputFormat.setOutputPath(job, path); //启动处理任务job
boolean completion = job.waitForCompletion(true);
if(completion){
System.out.println("Job Success!");
} } public static class myMapper extends Mapper<LongWritable, Text,TestBean, NullWritable>{ @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String values=value.toString();
String words[]=values.split(" ");
TestBean bean = new TestBean(Long.parseLong(words[]),Long.parseLong(words[]));
context.write(bean, NullWritable.get());
}
} public static class myReducer extends Reducer<TestBean, NullWritable, LongWritable, LongWritable>{ @Override
protected void reduce(TestBean key, Iterable<NullWritable> values,Context context)throws IOException, InterruptedException {
context.write(new LongWritable(key.getA()), new LongWritable(key.getB()));
}
}
}

这样就能得到最终结果:

如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击下方的【好文要顶】按钮【精神支持】,因为这两种支持都是使我继续写作、分享的最大动力!

MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序的更多相关文章

  1. nginx的conf文件,两种配置方式,第一种无ssl证书,第二种有ssl证书。

    以下为无ssl证书配置的请求转发 server { listen 80; server_name api.******.com; location ~* /union { client_max_bod ...

  2. 编程算法 - 和为s的两个数字 代码(C)

    和为s的两个数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个递增排序的数组和一个数字s, 在数组中查找两个数, 使得它们的和正好是 ...

  3. 【面试题041】和为s的两个数字VS和为s的连续正数序列

    [面试题041]和为s的两个数字VS和为s的连续正数序列 题目一:     输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. ...

  4. 和为 s 的两个数字(和为 s 的连续正数序列)

    题目 输入一个递增排序的数组和一个数字 s,在数组中查找两个数,得它们的和正好是 s.如果有多对数字的和等于 s,输出任意一对即可 思路 我们先在数组中选择两个数字,如果它们的和等于输入的 s,我们就 ...

  5. 10.排序数组中和为给定值的两个数字[Find2NumbersWithGivenSum]

    [题目] 输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字.要求时间复杂度是O(n).如果有多对数字的和等于输入的数字,输出任意一对即可. 例如输入数组1 ...

  6. 剑指Offer - 九度1352 - 和为S的两个数字

    剑指Offer - 九度1352 - 和为S的两个数字2014-02-05 18:15 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于 ...

  7. JavaScript求两个数字之间所有数字的和

    这是在fcc上的中级算法中的第一题,拉出来的原因并不是因为有什么好说的,而是我刚看时以为是求两个数字的和, 很显然错了.我感觉自己的文字理解能力被严重鄙视了- -.故拉出来折腾折腾. 要求: 给你一个 ...

  8. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  9. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. C语言数据类型大小

    数据类型大小是由操作系统和编译器共同决定的,但必须满足: short和int至少为16bit:long至少为32bit: short不能超过int,int不能超过long. 在主流编译器中,32位机和 ...

  2. git base commond

    打开Git Bash 命令:先写 git status, 它会告诉你怎么做 1. git pull  (把git库中代码拉下来)      2. $ git status (查看状态) 3. $ gi ...

  3. Java版接口自动化--初稿

    一.接口参数的获取: 1.参数通过Excel读取,并将结果写入Excel中 package org.fanqi.operateExcel; import java.io.FileInputStream ...

  4. devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 163840 free data blocks

    问题: 制作镜像的时候报错 devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 1 ...

  5. CMDB初步了解

    本节内容 浅谈ITIL CMDB介绍 Django自定义用户认证 Restful 规范 资产管理功能开发 浅谈ITIL TIL即IT基础架构库(Information Technology Infra ...

  6. jquery获取浏览器类型和版本号的方法

    $(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="Mi ...

  7. django_视图层/2.0路由层/虚拟环境

  8. 多线程threading.local的作用及原理?

    1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...

  9. Android 屏幕切换动画

    public void overridePendingTransition (int enterAnim, int exitAnim) Call immediately after one of th ...

  10. (转)基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN

    object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.object detection要解决的问题就是物体在哪里,是什么这整个流程的问题.然而,这个问题 ...