关于reduce输出write方法

Hadoop与Storm输出定制
本文探讨了Hadoop中自定义OutputFormat的方法,包括RecordWriter的实现与数据库存储过程,以及Storm中的数据过滤、分区聚合和键拼接技巧。通过具体代码示例,介绍了如何在MapReduce作业中使用TableMapReduceUtil进行HBase表读取前的过滤操作。
关于hadoop一些自定义输出
code>OutputFormat</code> describes the output-specification for a 
 * Map-Reduce job.
首先继承outputFormat<key,value>这个抽象类 Map-Reduce job的输出规范
实现他的方法:
RecordWriter<KeyBaseDimension, BaseStatsValueWritable> getRecordWriter 在方法内可以进行数据库连接操作
这里需要一个返回一个RecordWriter 
继承这个RecordWriter类
 实现里面的write方法 进行数据库jdbc存储即可
 
 
 关于reduce端输出时会调用的write方法
 实现类为:TaskInputOutputContextImpl
  private RecordWriter<KEYOUT,VALUEOUT> output;
  public void write(KEYOUT key, VALUEOUT value
                    ) throws IOException, InterruptedException {
    output.write(key, value);
  }
  最终是调用了RecordWriter的write方法,


map端读取hbase一个mr工具类
*在提交TableMap作业之前使用此选项。它将被适当地设置
*工作。
TableMapReduceUtil 这个类很重要,在提交读取hbase表job之前可以对其进行一系列过滤操作
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);

        filterList.addFilter(
                new SingleColumnValueFilter(EventLogConstants.EVENT_LOGS_FAMILY_NAME_BYTES,
                        Bytes.toBytes(EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME),
                        CompareOp.EQUAL, Bytes.toBytes(EventLogConstants.EventEnum.BC_SX.alias)));

public static void initTableMapperJob(List<Scan> scans,
      Class<? extends TableMapper> mapper,
      Class<?> outputKeyClass,
      Class<?> outputValueClass, Job job,
      boolean addDependencyJars,
      boolean initCredentials) throws IOException {
 scan之前进行过滤器数据
List<Scan> scanList = new ArrayList<Scan>(); try { conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); String tableName = EventLogConstants.HBASE_NAME_AUDIT_SX + GlobalConstants.UNDERLINE + statDate.replaceAll(GlobalConstants.KEY_SEPARATOR, ""); if (admin.tableExists(TableName.valueOf(tableName))) { Scan scan = new Scan();

scan读取多个表的设置
// If an application wants to use multiple scans over different tables each scan must // define this attribute with the appropriate table name by calling // scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName)) // static public final String SCAN_ATTRIBUTES_TABLE_NAME = "scan.attributes.table.name"; scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName)); scan.setFilter(filterList); scanList.add(scan);
}
最后将job 与scanlist都设置进去

TableMapReduceUtil.initTableMapperJob(scanList, AuditorSXMapper.class,

AuditorDimensionKey.class, Text.class, job, false);

 

 


strom一些笔记知识

 storm echo(File(),fun,File())
 filter:实现filter接口 iskeep方法
 partitionAggregate函数:分区内聚合,实现aggregate<保存聚合状态的类> 的aggregate实现聚合逻辑 ,complete方法 ridentCollector collector.emit(Value(聚合后的值))
 一般的key拼接函数:实现function接口的execute方法
 HBaseMapState.Options optsWait = new HBaseMapState.Options();
 
     TridentState amtOfWaitState = partStream.project(new Fields("waitingTotalOfPartDay","dayAndContType"))
                .groupBy(new Fields("dayAndContType"))
                .persistentAggregate(
                        factoryWait,
                        new Fields("waitingTotalOfPartDay"),new Sum(),
                        new Fields("waitingGlobalOfDay")
                );
 
 persistentAggregate 持久化保存函数 进行全区的sum求和,输入各区,输出为总和

 

 

 

转载于:https://www.cnblogs.com/hejunhong/p/10423281.html

实现Reduce输出结果的全排序,需要在Reducer中进行一定的修改。以下是一个基本的MapReduce全排序的代码示例: Mapper函数: ``` public static class SortMapper extends Mapper<Object, Text, IntWritable, IntWritable>{ private IntWritable num = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); num.set(Integer.parseInt(line)); context.write(num, new IntWritable(1)); } } ``` Reducer函数: ``` public static class SortReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> { public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for (IntWritable value : values) { context.write(key, new IntWritable(1)); } } } ``` 驱动函数: ``` public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "sort"); job.setJarByClass(Sort.class); job.setMapperClass(SortMapper.class); job.setReducerClass(SortReducer.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); job.setMapOutputKeyClass(IntWritable.class); job.setMapOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setNumReduceTasks(1); // 设置Reduce任务数为1,确保全局排序 System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 在这个示例中,Mapper函数与之前的示例相同。在Reducer函数中,我们直接输出键值对,因为它们已经按照键进行了排序。在驱动函数中,我们设置了Reduce任务数为1,以确保全局排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值