You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by dm...@apache.org on 2013/03/22 20:23:16 UTC

svn commit: r1459939 - /hbase/trunk/src/docbkx/book.xml

Author: dmeil
Date: Fri Mar 22 19:23:15 2013
New Revision: 1459939

URL: http://svn.apache.org/r1459939
Log:
hbase-8182.  book.xml.  Updating refGuide to use byte-array constants (setting good example).

Modified:
    hbase/trunk/src/docbkx/book.xml

Modified: hbase/trunk/src/docbkx/book.xml
URL: http://svn.apache.org/viewvc/hbase/trunk/src/docbkx/book.xml?rev=1459939&r1=1459938&r2=1459939&view=diff
==============================================================================
--- hbase/trunk/src/docbkx/book.xml (original)
+++ hbase/trunk/src/docbkx/book.xml Fri Mar 22 19:23:15 2013
@@ -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".
 <programlisting>
+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 @@ try {
         <title>Default Get Example</title>
         <para>The following Get will only retrieve the current version of the row
 <programlisting>
+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
 </programlisting>
         </para>
         </section>
@@ -399,11 +406,14 @@ byte[] b = r.getValue(Bytes.toBytes("cf"
         <title>Versioned Get Example</title>
         <para>The following Get will return the last 3 versions of the row.
 <programlisting>
+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&lt;KeyValue&gt; 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&lt;KeyValue&gt; kv = r.getColumn(CF, ATTR);  // returns all versions of this column
 </programlisting>
         </para>
         </section>
@@ -425,8 +435,11 @@ List&lt;KeyValue&gt; kv = r.getColumn(By
           <title>Implicit Version Example</title>
           <para>The following Put will be implicitly versioned by HBase with the current time.
 <programlisting>
+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);
 </programlisting>
           </para>
@@ -435,9 +448,12 @@ htable.put(put);
           <title>Explicit Version Example</title>
           <para>The following Put has the version timestamp explicitly set.
 <programlisting>
+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);
 </programlisting>
           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.
     <programlisting>
 public static class MyMapper extends TableMapper&lt;Text, IntWritable&gt;  {
+	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 @@ public static class MyMapper extends Tab
     In the reducer, the "ones" are counted (just like any other MR example that does this), and then emits a <classname>Put</classname>.
     <programlisting>
 public static class MyTableReducer extends TableReducer&lt;Text, IntWritable, ImmutableBytesWritable&gt;  {
+	public static final byte[] CF = "cf".getBytes();
+	public static final byte[] COUNT = "count".getBytes();
 
  	public void reduce(Text key, Iterable&lt;IntWritable&gt; values, Context context) throws IOException, InterruptedException {
     		int i = 0;
@@ -1201,7 +1221,7 @@ public static class MyTableReducer exten
     			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);
    	}