1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.client;
22
23 import java.util.NavigableMap;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseClusterTestCase;
29 import org.apache.hadoop.hbase.HColumnDescriptor;
30 import org.apache.hadoop.hbase.HTableDescriptor;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33
34
35
36
37 public class TestGetRowVersions extends HBaseClusterTestCase {
38 private static final Log LOG = LogFactory.getLog(TestGetRowVersions.class);
39
40 private static final String TABLE_NAME = "test";
41 private static final byte [] CONTENTS = Bytes.toBytes("contents");
42 private static final byte [] ROW = Bytes.toBytes("row");
43 private static final byte [] VALUE1 = Bytes.toBytes("value1");
44 private static final byte [] VALUE2 = Bytes.toBytes("value2");
45 private static final long TIMESTAMP1 = 100L;
46 private static final long TIMESTAMP2 = 200L;
47
48 @Override
49 public void setUp() throws Exception {
50 super.setUp();
51 HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
52 desc.addFamily(new HColumnDescriptor(CONTENTS));
53 HBaseAdmin admin = new HBaseAdmin(conf);
54 admin.createTable(desc);
55 }
56
57
58 public void testGetRowMultipleVersions() throws Exception {
59 Put put = new Put(ROW, TIMESTAMP1, null);
60 put.add(CONTENTS, CONTENTS, VALUE1);
61 HTable table = new HTable(new Configuration(conf), TABLE_NAME);
62 table.put(put);
63
64 this.cluster.shutdown();
65 this.zooKeeperCluster.shutdown();
66 LOG.debug("HBase cluster shut down -- restarting");
67 this.hBaseClusterSetup();
68
69
70 table = new HTable(new Configuration(conf), TABLE_NAME);
71
72 put = new Put(ROW, TIMESTAMP2, null);
73 put.add(CONTENTS, CONTENTS, VALUE2);
74 table.put(put);
75
76 Get get = new Get(ROW);
77
78 Result r = table.get(get);
79 assertNotNull(r);
80 assertFalse(r.isEmpty());
81 assertTrue(r.size() == 1);
82 byte [] value = r.getValue(CONTENTS, CONTENTS);
83 assertTrue(value.length != 0);
84 assertTrue(Bytes.equals(value, VALUE2));
85
86 get = new Get(ROW);
87 get.setMaxVersions();
88 r = table.get(get);
89 assertTrue(r.size() == 2);
90 value = r.getValue(CONTENTS, CONTENTS);
91 assertTrue(value.length != 0);
92 assertTrue(Bytes.equals(value, VALUE2));
93 NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map =
94 r.getMap();
95 NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap =
96 map.get(CONTENTS);
97 NavigableMap<Long, byte[]> versionMap = familyMap.get(CONTENTS);
98 assertTrue(versionMap.size() == 2);
99 assertTrue(Bytes.equals(VALUE1, versionMap.get(TIMESTAMP1)));
100 assertTrue(Bytes.equals(VALUE2, versionMap.get(TIMESTAMP2)));
101 }
102 }