From 28441ed946a1ab936658d0b20fb9176aa69069cb Mon Sep 17 00:00:00 2001 From: Artem Ervits Date: Fri, 2 Mar 2018 12:13:31 -0500 Subject: [PATCH] HBASE-20094 Import$CellWritableComparable should define equals() --- .../org/apache/hadoop/hbase/mapreduce/Import.java | 30 ++++++++++++++++++++-- .../hadoop/hbase/mapreduce/TestImportExport.java | 19 ++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/Import.java hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/Import.java index f7405fde83..92d55f721a 100644 --- hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/Import.java +++ hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/Import.java @@ -30,6 +30,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; import java.util.UUID; @@ -151,6 +152,29 @@ public class Import extends Configured implements Tool { return CellComparator.getInstance().compare(this.kv, o.kv); } + @Override + public int hashCode() { + int hash = 7; + hash = 19 * hash + Objects.hashCode(this.kv); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CellWritableComparable other = (CellWritableComparable) obj; + int result = CellComparator.getInstance().compare(this.kv, other.kv); + return (result == 0); + } + public static class CellWritableComparator extends WritableComparator { @Override @@ -482,6 +506,7 @@ public class Import extends Configured implements Tool { /** * Attempt to filter out the keyvalue + * @param filter a parameter to filter on * @param c {@link Cell} on which to apply the filter * @return null if the key should not be written, otherwise returns the original * {@link Cell} @@ -577,7 +602,7 @@ public class Import extends Configured implements Tool { if(sb.length() != 0) { sb.append(","); } - sb.append(sourceCf + ":" + destCf); + sb.append(sourceCf).append(":").append(destCf); } conf.set(CF_RENAME_PROP, sb.toString()); } @@ -618,7 +643,7 @@ public class Import extends Configured implements Tool { if (filter != null) { TableMapReduceUtil.addDependencyJarsForClasses(conf, filter); } - } catch (Exception e) { + } catch (IOException e) { throw new IOException(e); } @@ -714,6 +739,7 @@ public class Import extends Configured implements Tool { * need to flush all the regions of the table as the data is held in memory and is also not * present in the Write Ahead Log to replay in scenarios of a crash. This method flushes all the * regions of the table in the scenarios of import data to hbase with {@link Durability#SKIP_WAL} + * @param conf pass configuration to the method */ public static void flushRegionsIfNecessary(Configuration conf) throws IOException, InterruptedException { diff --git hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestImportExport.java hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestImportExport.java index 9349816fae..3dca6412e2 100644 --- hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestImportExport.java +++ hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestImportExport.java @@ -63,6 +63,7 @@ import org.apache.hadoop.hbase.filter.FilterBase; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.Import.CellImporter; +import org.apache.hadoop.hbase.mapreduce.Import.CellWritableComparable; import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener; import org.apache.hadoop.hbase.testclassification.MediumTests; import org.apache.hadoop.hbase.testclassification.VerySlowMapReduceTests; @@ -801,4 +802,22 @@ public class TestImportExport { return isVisited; } } + + @Test + public void testCellWritableComparableEquals() { + Cell aCell = CellUtil.createCell(ROW1); + CellWritableComparable anObj1 = new CellWritableComparable((Cell) aCell); + CellWritableComparable anObj2 = new CellWritableComparable((Cell) aCell); + assertTrue(anObj1.equals(anObj2)); + Cell aNewCell = CellUtil.createCell(ROW2); + assertFalse(anObj1.equals(aNewCell)); + } + + @Test + public void testCellWritableComparableHashCode() { + Cell aCell = CellUtil.createCell(ROW1); + CellWritableComparable anObj1 = new CellWritableComparable((Cell) aCell); + CellWritableComparable anObj2 = new CellWritableComparable((Cell) aCell); + assertEquals(anObj1.hashCode(), anObj2.hashCode()); + } } -- 2.16.2