hbase-8182. book.xml. Updating refGuide to use byte-array constants (setting good example).
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1459939 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
423f0742e3
commit
c36d3069e4
|
@ -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
|
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".
|
can be applied to a Scan instance to return the rows beginning with "row".
|
||||||
<programlisting>
|
<programlisting>
|
||||||
|
public static final byte[] CF = "cf".getBytes();
|
||||||
|
public static final byte[] ATTR = "attr".getBytes();
|
||||||
|
...
|
||||||
|
|
||||||
HTable htable = ... // instantiate HTable
|
HTable htable = ... // instantiate HTable
|
||||||
|
|
||||||
Scan scan = new Scan();
|
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.setStartRow( Bytes.toBytes("row")); // start key is inclusive
|
||||||
scan.setStopRow( Bytes.toBytes("row" + (char)0)); // stop key is exclusive
|
scan.setStopRow( Bytes.toBytes("row" + (char)0)); // stop key is exclusive
|
||||||
ResultScanner rs = htable.getScanner(scan);
|
ResultScanner rs = htable.getScanner(scan);
|
||||||
|
@ -389,9 +393,12 @@ try {
|
||||||
<title>Default Get Example</title>
|
<title>Default Get Example</title>
|
||||||
<para>The following Get will only retrieve the current version of the row
|
<para>The following Get will only retrieve the current version of the row
|
||||||
<programlisting>
|
<programlisting>
|
||||||
|
public static final byte[] CF = "cf".getBytes();
|
||||||
|
public static final byte[] ATTR = "attr".getBytes();
|
||||||
|
...
|
||||||
Get get = new Get(Bytes.toBytes("row1"));
|
Get get = new Get(Bytes.toBytes("row1"));
|
||||||
Result r = htable.get(get);
|
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
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
@ -399,11 +406,14 @@ byte[] b = r.getValue(Bytes.toBytes("cf"), Bytes.toBytes("attr")); // returns c
|
||||||
<title>Versioned Get Example</title>
|
<title>Versioned Get Example</title>
|
||||||
<para>The following Get will return the last 3 versions of the row.
|
<para>The following Get will return the last 3 versions of the row.
|
||||||
<programlisting>
|
<programlisting>
|
||||||
|
public static final byte[] CF = "cf".getBytes();
|
||||||
|
public static final byte[] ATTR = "attr".getBytes();
|
||||||
|
...
|
||||||
Get get = new Get(Bytes.toBytes("row1"));
|
Get get = new Get(Bytes.toBytes("row1"));
|
||||||
get.setMaxVersions(3); // will return last 3 versions of row
|
get.setMaxVersions(3); // will return last 3 versions of row
|
||||||
Result r = htable.get(get);
|
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
|
||||||
List<KeyValue> kv = r.getColumn(Bytes.toBytes("cf"), Bytes.toBytes("attr")); // returns all versions of this column
|
List<KeyValue> kv = r.getColumn(CF, ATTR); // returns all versions of this column
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
@ -425,8 +435,11 @@ List<KeyValue> kv = r.getColumn(Bytes.toBytes("cf"), Bytes.toBytes("attr")
|
||||||
<title>Implicit Version Example</title>
|
<title>Implicit Version Example</title>
|
||||||
<para>The following Put will be implicitly versioned by HBase with the current time.
|
<para>The following Put will be implicitly versioned by HBase with the current time.
|
||||||
<programlisting>
|
<programlisting>
|
||||||
|
public static final byte[] CF = "cf".getBytes();
|
||||||
|
public static final byte[] ATTR = "attr".getBytes();
|
||||||
|
...
|
||||||
Put put = new Put(Bytes.toBytes(row));
|
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);
|
htable.put(put);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</para>
|
</para>
|
||||||
|
@ -435,9 +448,12 @@ htable.put(put);
|
||||||
<title>Explicit Version Example</title>
|
<title>Explicit Version Example</title>
|
||||||
<para>The following Put has the version timestamp explicitly set.
|
<para>The following Put has the version timestamp explicitly set.
|
||||||
<programlisting>
|
<programlisting>
|
||||||
|
public static final byte[] CF = "cf".getBytes();
|
||||||
|
public static final byte[] ATTR = "attr".getBytes();
|
||||||
|
...
|
||||||
Put put = new Put( Bytes.toBytes(row));
|
Put put = new Put( Bytes.toBytes(row));
|
||||||
long explicitTimeInMs = 555; // just an example
|
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);
|
htable.put(put);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
Caution: the version timestamp is internally by HBase for things like time-to-live calculations.
|
Caution: the version timestamp is internally by HBase for things like time-to-live calculations.
|
||||||
|
@ -1179,12 +1195,14 @@ if (!b) {
|
||||||
This value is used as the key to emit from the mapper, and an <classname>IntWritable</classname> represents an instance counter.
|
This value is used as the key to emit from the mapper, and an <classname>IntWritable</classname> represents an instance counter.
|
||||||
<programlisting>
|
<programlisting>
|
||||||
public static class MyMapper extends TableMapper<Text, IntWritable> {
|
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 final IntWritable ONE = new IntWritable(1);
|
||||||
private Text text = new Text();
|
private Text text = new Text();
|
||||||
|
|
||||||
public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
|
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...
|
text.set(val); // we can only emit Writables...
|
||||||
|
|
||||||
context.write(text, ONE);
|
context.write(text, ONE);
|
||||||
|
@ -1194,6 +1212,8 @@ public static class MyMapper extends TableMapper<Text, IntWritable> {
|
||||||
In the reducer, the "ones" are counted (just like any other MR example that does this), and then emits a <classname>Put</classname>.
|
In the reducer, the "ones" are counted (just like any other MR example that does this), and then emits a <classname>Put</classname>.
|
||||||
<programlisting>
|
<programlisting>
|
||||||
public static class MyTableReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {
|
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 {
|
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -1201,7 +1221,7 @@ public static class MyTableReducer extends TableReducer<Text, IntWritable, Im
|
||||||
i += val.get();
|
i += val.get();
|
||||||
}
|
}
|
||||||
Put put = new Put(Bytes.toBytes(key.toString()));
|
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);
|
context.write(null, put);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue