在上例中,使用Avro框架求出数据的最大值,本例使用Avro对数据排序,输入依然是之前的样本,输出使用文本(也可以输出Avro格式)。

1、在Avro的Schema中直接设置排序方向。

dataRecord.avsc,放入resources目录下:

{
"type":"record",
"name":"WeatherRecord",
"doc":"A weather reading",
"fields":[
{"name":"year","type":"int"},
{"name":"temperature","type":"int","order":"descending"}
]
}

原常量类:

public class AvroSchemas {
private Schema currentSchema; //本例中不使用常量,修改成资源中加载
public static final Schema SCHEMA = new Schema.Parser().parse("{\n" +
"\t\"type\":\"record\",\n" +
"\t\"name\":\"WeatherRecord\",\n" +
"\t\"doc\":\"A weather reading\",\n" +
"\t\"fields\":[\n" +
"\t\t{\"name\":\"year\",\"type\":\"int\"},\n" +
"\t\t{\"name\":\"temperature\",\"type\":\"int\",\"order\":\"descending\"}\n" +
"\t]\t\n" +
"}"); public AvroSchemas() throws IOException {
Schema.Parser parser = new Schema.Parser();
//采用从资源文件中读取Avro数据格式
this.currentSchema = parser.parse(getClass().getResourceAsStream("dataRecord.avsc"));
} public Schema getCurrentSchema() {
return currentSchema;
}
}

2、mapper

public class AvroMapper extends Mapper<LongWritable,Text,AvroKey<GenericRecord>,AvroValue<GenericRecord>> {
private RecordParser parser = new RecordParser();
// private GenericRecord record = new GenericData.Record(AvroSchemas.SCHEMA);
private AvroSchemas schema;
private GenericRecord record; public AvroMapper() throws IOException {
schema =new AvroSchemas();
record = new GenericData.Record(schema.getCurrentSchema());
} @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { parser.parse(value.toString());
if(parser.isValid()){
record.put("year",parser.getYear());
record.put("temperature",parser.getData());
context.write(new AvroKey<>(record),new AvroValue<>(record));
}
}
}

3、reducer

public class AvroReducer extends Reducer<AvroKey<GenericRecord>,AvroValue<GenericRecord>,IntPair,NullWritable> {
//多文件输出,本例中每年一个文件
private MultipleOutputs<IntPair,NullWritable> multipleOutputs; /**
* Called once at the start of the task.
*
* @param context
*/
@Override
protected void setup(Context context) throws IOException, InterruptedException {
multipleOutputs = new MultipleOutputs<>(context);
} @Override
protected void reduce(AvroKey<GenericRecord> key, Iterable<AvroValue<GenericRecord>> values, Context context) throws IOException, InterruptedException {
//在混洗阶段完成排序,reducer只需直接输出数据
for (AvroValue<GenericRecord> value : values){
GenericRecord record = value.datum();
//多文件输出,每年一个文件。
multipleOutputs.write(new IntPair((Integer) record.get("year"),(Integer)(record.get("temperature"))),NullWritable.get(),record.get("year").toString());
// context.write(new IntPair((Integer) record.get("year"),(Integer)(record.get("temperature"))),NullWritable.get());
}
} }

4、job

public class AvroSort extends Configured implements Tool {

    @Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
conf.set("mapreduce.job.ubertask.enable","true"); Job job = Job.getInstance(conf,"Avro sort");
job.setJarByClass(AvroSort.class); //通过AvroJob直接设置Avro key和value的输入和输出,而不是使用Job来设置
AvroJob.setMapOutputKeySchema(job, AvroSchemas.SCHEMA);
AvroJob.setMapOutputValueSchema(job,AvroSchemas.SCHEMA);
// AvroJob.setOutputKeySchema(job,AvroSchemas.SCHEMA); job.setMapperClass(AvroMapper.class);
job.setReducerClass(AvroReducer.class); job.setInputFormatClass(TextInputFormat.class);
// job.setOutputFormatClass(AvroKeyOutputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1])); Path outPath = new Path(args[1]);
FileSystem fileSystem = outPath.getFileSystem(conf);
//删除输出路径
if(fileSystem.exists(outPath))
{
fileSystem.delete(outPath,true);
} return job.waitForCompletion(true) ? 0:1;
} public static void main(String[] args) throws Exception{
int exitCode = ToolRunner.run(new AvroSort(),args);
System.exit(exitCode);
}
}

hadoop 使用Avro排序的更多相关文章

  1. hadoop 使用Avro求最大值

    在上例中:hadoop MapReduce辅助排序解析,为了求每年的最大数据使用了mapreduce辅助排序的方法. 本例中介绍利用Avro这个序列化框架的mapreduce功能来实现求取最大值.Av ...

  2. 2 weekend110的hadoop的自定义排序实现 + mr程序中自定义分组的实现

    我想得到按流量来排序,而且还是倒序,怎么达到实现呢? 达到下面这种效果, 默认是根据key来排, 我想根据value里的某个排, 解决思路:将value里的某个,放到key里去,然后来排 下面,开始w ...

  3. Hadoop之WritableComprale 排序

    Hadoop之WritableComprale 排序 Hadoop只对key进行排序 排序是 MapReduce 框架中最重要的操作之一.Map Task 和 Reduce Task 均会对数据(按照 ...

  4. Hadoop日记Day18---MapReduce排序分组

    本节所用到的数据下载地址为:http://pan.baidu.com/s/1bnfELmZ MapReduce的排序分组任务与要求 我们知道排序分组是MapReduce中Mapper端的第四步,其中分 ...

  5. Hadoop shuffle与排序

    Mapreduce为了确保每个reducer的输入都按键排序.系统执行排序的过程-----将map的输出作为输入传给reducer 称为shuffle.学习shuffle是如何工作的有助于我们理解ma ...

  6. 三种方法实现Hadoop(MapReduce)全局排序(1)

    我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...

  7. hadoop streaming字段排序介绍

    我们在使用hadoop streaming的时候默认streaming的map和reduce的separator不指定的话,map和reduce会根据它们默认的分隔符来进行排序 map.reduce: ...

  8. 一起学Hadoop——二次排序算法的实现

    二次排序,从字面上可以理解为在对key排序的基础上对key所对应的值value排序,也叫辅助排序.一般情况下,MapReduce框架只对key排序,而不对key所对应的值排序,因此value的排序经常 ...

  9. Hadoop mapreduce自定义排序WritableComparable

    本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...

随机推荐

  1. Android:Gradle报错——No resource found that matches the given name (at 'dialogCornerRadius' with value '?android:attr/dialogCornerRadius')

    今天在使用科大讯飞语音识别SDK进行语音识别功能实现时,莫名的引入了这个错误.不得不吐槽Android Studio再引入别的包时太容易出现冲突,然后导致无法找到R文件,项目无法执行. 1. 具体报错 ...

  2. 第六模块:WEB框架开发 第1章·Django框架开发1~50

    01-Django基础介绍 02-Web应用程序1 03-Web应用程序2 04-http请求协议1 05-http请求协议2 06-http协议之响应协议 07-wsgire模块1 08-wsgir ...

  3. Python简要标准库(1)

    sys sys这个模块让你能够访问与Python解释器联系紧密的变量和函数 其中的一些在下表 F argv 命令行参数,包括脚本名称 exit([arg]) 退出当前的程序,可选参数为给定的返回值或者 ...

  4. [SHELL]退出脚本

    一,退出状态码 1,范围:0~255 2,查看退出状态码:必须在命令执行之后立即执行 ,显示的是脚本最后一条命令的退出状态码 echo $? 若f返回值为0,则表示正常 有异常为正值 二,exit 脚 ...

  5. 深入理解 Vuejs 动画效果

    本文主要归纳在 Vuejs 学习过程中对于 Vuejs 动画效果的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 V ...

  6. some Commands OF CONSOLE

    不可避免地使用console,一旦与电脑打交道:入口就是help,而很多行就直接过掉了,却不能看到需要的地方,在那里停下来,实际是需要使用more  less grep等 在windows中,使用di ...

  7. html+css基础 - 个人备忘录

    //======================html部分===================// 表现内容<meta http-equiv="Content-Type" ...

  8. Mysql 工作原理

    刚开始接触一个新的事物的时候,我觉得很有必要从其工作原理入手,弄清楚这个东西的来龙去脉,为接下来的继续深入学习做好铺垫,掌握好其原理有助于我们从整体上来把握这个东西,并且帮助我们在排错过程中理清思路. ...

  9. TCP系列39—拥塞控制—2、拥塞相关算法及基础知识

    一.拥塞控制的相关算法 早期的TCP协议只有基于窗口的流控(flow control)机制而没有拥塞控制机制,因而易导致网络拥塞.1988年Jacobson针对TCP在网络拥塞控制方面的不足,提出了& ...

  10. 利用JavaScriptSerializer类 进行Json对象的序列化和反序列化和过滤

    项目下载:JavaScriptSerializer_对JSON对象序列化与反序列化及过滤器 利用<JavascriptSerializer类> 进行Json对象的序列化和反序列化 1. 首 ...