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.CellComparatorImpl;
022import org.apache.hadoop.hbase.KeyValue;
023import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
024import org.apache.hadoop.mapreduce.Reducer;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Emits sorted KeyValues. Reads in all KeyValues from passed Iterator, sorts them, then emits
029 * KeyValues in sorted order. If lots of columns per row, it will use lots of memory sorting.
030 * @see HFileOutputFormat2
031 * @deprecated Use {@link CellSortReducer}. Will be removed from 3.0 onwards
032 */
033@Deprecated
034@InterfaceAudience.Public
035public class KeyValueSortReducer
036  extends Reducer<ImmutableBytesWritable, KeyValue, ImmutableBytesWritable, KeyValue> {
037  protected void reduce(ImmutableBytesWritable row, Iterable<KeyValue> kvs,
038    Reducer<ImmutableBytesWritable, KeyValue, ImmutableBytesWritable, KeyValue>.Context context)
039    throws java.io.IOException, InterruptedException {
040    TreeSet<KeyValue> map = new TreeSet<>(CellComparatorImpl.COMPARATOR);
041    for (KeyValue kv : kvs) {
042      try {
043        map.add(kv.clone());
044      } catch (CloneNotSupportedException e) {
045        throw new java.io.IOException(e);
046      }
047    }
048    context.setStatus("Read " + map.getClass());
049    int index = 0;
050    for (KeyValue kv : map) {
051      context.write(row, kv);
052      if (++index % 100 == 0) context.setStatus("Wrote " + index);
053    }
054  }
055}