diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Increment.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Increment.java index 6df3626478f..c5a8608d48a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Increment.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Increment.java @@ -22,6 +22,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -132,6 +134,29 @@ public class Increment extends Mutation implements Comparable { return !this.familyMap.isEmpty(); } + /** + * Before 0.95, when you called {@link Increment#getFamilyMap()}}, you got back + * a map of families to a list of Longs. Now, {@link #getFamilyMap()} returns + * families by list of Cells. This method has been added so you can have the + * old behavior. + * @return Map of families to a Map of qualifers and their Long increments. + * @since 0.95.0 + */ + public Map> getFamilyMapOfLongs() { + NavigableMap> map = super.getFamilyMap(); + Map> results = + new TreeMap>(Bytes.BYTES_COMPARATOR); + for (Map.Entry> entry: map.entrySet()) { + NavigableMap longs = new TreeMap(Bytes.BYTES_COMPARATOR); + for (Cell cell: entry.getValue()) { + KeyValue kv = KeyValueUtil.ensureKeyValue(cell); + longs.put(kv.getQualifier(), Bytes.toLong(kv.getValue())); + } + results.put(entry.getKey(), longs); + } + return results; + } + /** * @return String */ diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestIncrement.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestIncrement.java new file mode 100644 index 00000000000..19ff8674a93 --- /dev/null +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestIncrement.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; +import java.util.NavigableMap; + +import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Test; +import org.junit.experimental.categories.Category; +@Category(SmallTests.class) +public class TestIncrement { + @Test + public void test() { + final long expected = 13; + Increment inc = new Increment(new byte [] {'r'}); + int total = 0; + for (int i = 0; i < 2; i++) { + byte [] bytes = Bytes.toBytes(i); + inc.addColumn(bytes, bytes, expected); + total++; + } + Map> familyMapOfLongs = inc.getFamilyMapOfLongs(); + int found = 0; + for (Map.Entry> entry: familyMapOfLongs.entrySet()) { + for (Map.Entry e: entry.getValue().entrySet()) { + assertEquals(expected, e.getValue().longValue()); + found++; + } + } + assertEquals(total, found); + } +} \ No newline at end of file