001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.mapreduce; 019 020import java.util.TreeSet; 021import org.apache.hadoop.hbase.Cell; 022import org.apache.hadoop.hbase.CellComparator; 023import org.apache.hadoop.hbase.ExtendedCell; 024import org.apache.hadoop.hbase.PrivateCellUtil; 025import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 026import org.apache.hadoop.hbase.util.MapReduceExtendedCell; 027import org.apache.hadoop.mapreduce.Reducer; 028import org.apache.yetus.audience.InterfaceAudience; 029 030/** 031 * Emits sorted Cells. Reads in all Cells from passed Iterator, sorts them, then emits Cells in 032 * sorted order. If lots of columns per row, it will use lots of memory sorting. 033 * @see HFileOutputFormat2 034 */ 035@InterfaceAudience.Public 036public class CellSortReducer 037 extends Reducer<ImmutableBytesWritable, Cell, ImmutableBytesWritable, Cell> { 038 protected void reduce(ImmutableBytesWritable row, Iterable<Cell> kvs, 039 Reducer<ImmutableBytesWritable, Cell, ImmutableBytesWritable, Cell>.Context context) 040 throws java.io.IOException, InterruptedException { 041 TreeSet<ExtendedCell> set = new TreeSet<>(CellComparator.getInstance()); 042 for (Cell kv : kvs) { 043 set.add(PrivateCellUtil.ensureExtendedCell(kv)); 044 } 045 context.setStatus("Read " + set.getClass()); 046 int index = 0; 047 for (ExtendedCell kv : set) { 048 context.write(row, new MapReduceExtendedCell(kv)); 049 if (++index % 100 == 0) { 050 context.setStatus("Wrote " + index); 051 } 052 } 053 } 054}