Index: src/docbkx/book.xml =================================================================== --- src/docbkx/book.xml (revision 1459918) +++ src/docbkx/book.xml (working copy) @@ -261,10 +261,14 @@ and then another set of rows with the keys "abc1", "abc2", and "abc3". The following example shows how startRow and stopRow can be applied to a Scan instance to return the rows beginning with "row". +public static final byte[] CF = "cf".getBytes(); +public static final byte[] ATTR = "attr".getBytes(); +... + HTable htable = ... // instantiate HTable Scan scan = new Scan(); -scan.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("attr")); +scan.addColumn(CF, ATTR); scan.setStartRow( Bytes.toBytes("row")); // start key is inclusive scan.setStopRow( Bytes.toBytes("row" + (char)0)); // stop key is exclusive ResultScanner rs = htable.getScanner(scan); @@ -389,9 +393,12 @@ Default Get Example The following Get will only retrieve the current version of the row +public static final byte[] CF = "cf".getBytes(); +public static final byte[] ATTR = "attr".getBytes(); +... Get get = new Get(Bytes.toBytes("row1")); Result r = htable.get(get); -byte[] b = r.getValue(Bytes.toBytes("cf"), Bytes.toBytes("attr")); // returns current version of value +byte[] b = r.getValue(CF, ATTR); // returns current version of value @@ -399,11 +406,14 @@ Versioned Get Example The following Get will return the last 3 versions of the row. +public static final byte[] CF = "cf".getBytes(); +public static final byte[] ATTR = "attr".getBytes(); +... Get get = new Get(Bytes.toBytes("row1")); get.setMaxVersions(3); // will return last 3 versions of row Result r = htable.get(get); -byte[] b = r.getValue(Bytes.toBytes("cf"), Bytes.toBytes("attr")); // returns current version of value -List<KeyValue> kv = r.getColumn(Bytes.toBytes("cf"), Bytes.toBytes("attr")); // returns all versions of this column +byte[] b = r.getValue(CF, ATTR); // returns current version of value +List<KeyValue> kv = r.getColumn(CF, ATTR); // returns all versions of this column @@ -425,8 +435,11 @@ Implicit Version Example The following Put will be implicitly versioned by HBase with the current time. +public static final byte[] CF = "cf".getBytes(); +public static final byte[] ATTR = "attr".getBytes(); +... Put put = new Put(Bytes.toBytes(row)); -put.add(Bytes.toBytes("cf"), Bytes.toBytes("attr1"), Bytes.toBytes( data)); +put.add(CF, ATTR, Bytes.toBytes( data)); htable.put(put); @@ -435,9 +448,12 @@ Explicit Version Example The following Put has the version timestamp explicitly set. +public static final byte[] CF = "cf".getBytes(); +public static final byte[] ATTR = "attr".getBytes(); +... Put put = new Put( Bytes.toBytes(row)); long explicitTimeInMs = 555; // just an example -put.add(Bytes.toBytes("cf"), Bytes.toBytes("attr1"), explicitTimeInMs, Bytes.toBytes(data)); +put.add(CF, ATTR, explicitTimeInMs, Bytes.toBytes(data)); htable.put(put); Caution: the version timestamp is internally by HBase for things like time-to-live calculations. @@ -1179,12 +1195,14 @@ This value is used as the key to emit from the mapper, and an IntWritable represents an instance counter. public static class MyMapper extends TableMapper<Text, IntWritable> { + public static final byte[] CF = "cf".getBytes(); + public static final byte[] ATTR1 = "attr1".getBytes(); private final IntWritable ONE = new IntWritable(1); private Text text = new Text(); public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException { - String val = new String(value.getValue(Bytes.toBytes("cf"), Bytes.toBytes("attr1"))); + String val = new String(value.getValue(CF, ATTR1)); text.set(val); // we can only emit Writables... context.write(text, ONE); @@ -1194,6 +1212,8 @@ In the reducer, the "ones" are counted (just like any other MR example that does this), and then emits a Put. public static class MyTableReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> { + public static final byte[] CF = "cf".getBytes(); + public static final byte[] COUNT = "count".getBytes(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int i = 0; @@ -1201,7 +1221,7 @@ i += val.get(); } Put put = new Put(Bytes.toBytes(key.toString())); - put.add(Bytes.toBytes("cf"), Bytes.toBytes("count"), Bytes.toBytes(i)); + put.add(CF, COUNT, Bytes.toBytes(i)); context.write(null, put); }