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.hbase.HBaseClusterTestCase;
28 import org.apache.hadoop.hbase.HColumnDescriptor;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.util.Bytes;
31
32
33
34
35
36 public class TestGetRowVersions extends HBaseClusterTestCase {
37 private static final Log LOG = LogFactory.getLog(TestGetRowVersions.class);
38
39 private static final String TABLE_NAME = "test";
40 private static final byte [] CONTENTS = Bytes.toBytes("contents");
41 private static final byte [] ROW = Bytes.toBytes("row");
42 private static final byte [] VALUE1 = Bytes.toBytes("value1");
43 private static final byte [] VALUE2 = Bytes.toBytes("value2");
44 private static final long TIMESTAMP1 = 100L;
45 private static final long TIMESTAMP2 = 200L;
46 private HBaseAdmin admin = null;
47 private HTable table = null;
48
49 @Override
50 public void setUp() throws Exception {
51 super.setUp();
52 HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
53 desc.addFamily(new HColumnDescriptor(CONTENTS));
54 this.admin = new HBaseAdmin(conf);
55 this.admin.createTable(desc);
56 this.table = new HTable(conf, TABLE_NAME);
57 }
58
59
60 public void testGetRowMultipleVersions() throws Exception {
61 Put put = new Put(ROW, TIMESTAMP1, null);
62 put.add(CONTENTS, CONTENTS, VALUE1);
63 this.table.put(put);
64
65 this.cluster.shutdown();
66 this.zooKeeperCluster.shutdown();
67 LOG.debug("HBase cluster shut down -- restarting");
68 this.hBaseClusterSetup();
69
70 this.table = new HTable(conf, TABLE_NAME);
71
72 put = new Put(ROW, TIMESTAMP2, null);
73 put.add(CONTENTS, CONTENTS, VALUE2);
74 this.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 }