HBASE-1016 Fix example in javadoc overview
git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@719717 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
69cbc5d27c
commit
69be09bed5
|
@ -80,6 +80,7 @@ Release 0.19.0 - Unreleased
|
||||||
HBASE-910 Scanner misses columns / rows when the scanner is obtained
|
HBASE-910 Scanner misses columns / rows when the scanner is obtained
|
||||||
durring a memcache flush
|
durring a memcache flush
|
||||||
HBASE-1009 Master stuck in loop wanting to assign but regions are closing
|
HBASE-1009 Master stuck in loop wanting to assign but regions are closing
|
||||||
|
HBASE-1016 Fix example in javadoc overvie
|
||||||
|
|
||||||
IMPROVEMENTS
|
IMPROVEMENTS
|
||||||
HBASE-901 Add a limit to key length, check key and value length on client side
|
HBASE-901 Add a limit to key length, check key and value length on client side
|
||||||
|
|
|
@ -59,7 +59,6 @@ public class IndexSpecification implements Writable {
|
||||||
* @param indexedColumns
|
* @param indexedColumns
|
||||||
* @param additionalColumns
|
* @param additionalColumns
|
||||||
* @param keyGenerator
|
* @param keyGenerator
|
||||||
* @param keyComparator
|
|
||||||
*/
|
*/
|
||||||
public IndexSpecification(String indexId, byte[][] indexedColumns,
|
public IndexSpecification(String indexId, byte[][] indexedColumns,
|
||||||
byte[][] additionalColumns, IndexKeyGenerator keyGenerator) {
|
byte[][] additionalColumns, IndexKeyGenerator keyGenerator) {
|
||||||
|
|
|
@ -52,7 +52,6 @@ public class IndexedTable extends TransactionalTable {
|
||||||
|
|
||||||
private Map<String, HTable> indexIdToTable = new HashMap<String, HTable>();
|
private Map<String, HTable> indexIdToTable = new HashMap<String, HTable>();
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
public IndexedTable(final HBaseConfiguration conf, final byte[] tableName)
|
public IndexedTable(final HBaseConfiguration conf, final byte[] tableName)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
super(conf, tableName);
|
super(conf, tableName);
|
||||||
|
|
|
@ -195,6 +195,7 @@ import org.apache.hadoop.hbase.client.Scanner;
|
||||||
import org.apache.hadoop.hbase.io.BatchUpdate;
|
import org.apache.hadoop.hbase.io.BatchUpdate;
|
||||||
import org.apache.hadoop.hbase.io.Cell;
|
import org.apache.hadoop.hbase.io.Cell;
|
||||||
import org.apache.hadoop.hbase.io.RowResult;
|
import org.apache.hadoop.hbase.io.RowResult;
|
||||||
|
import org.apache.hadoop.hbase.util.Bytes;
|
||||||
|
|
||||||
public class MyClient {
|
public class MyClient {
|
||||||
|
|
||||||
|
@ -209,17 +210,20 @@ public class MyClient {
|
||||||
|
|
||||||
// To do any sort of update on a row, you use an instance of the BatchUpdate
|
// To do any sort of update on a row, you use an instance of the BatchUpdate
|
||||||
// class. A BatchUpdate takes a row and optionally a timestamp which your
|
// class. A BatchUpdate takes a row and optionally a timestamp which your
|
||||||
// updates will affect.
|
// updates will affect. If no timestamp, the server applies current time
|
||||||
|
// to the edits.
|
||||||
BatchUpdate batchUpdate = new BatchUpdate("myRow");
|
BatchUpdate batchUpdate = new BatchUpdate("myRow");
|
||||||
|
|
||||||
// The BatchUpdate#put method takes a Text that describes what cell you want
|
// The BatchUpdate#put method takes a byte [] (or String) that designates
|
||||||
// to put a value into, and a byte array that is the value you want to
|
// what cell you want to put a value into, and a byte array that is the
|
||||||
// store. Note that if you want to store strings, you have to getBytes()
|
// value you want to store. Note that if you want to store Strings, you
|
||||||
// from the string for HBase to understand how to store it. (The same goes
|
// have to getBytes() from the String for HBase to store it since HBase is
|
||||||
// for primitives like ints and longs and user-defined classes - you must
|
// all about byte arrays. The same goes for primitives like ints and longs
|
||||||
// find a way to reduce it to bytes.)
|
// and user-defined classes - you must find a way to reduce it to bytes.
|
||||||
|
// The Bytes class from the hbase util package has utility for going from
|
||||||
|
// String to utf-8 bytes and back again and help for other base types.
|
||||||
batchUpdate.put("myColumnFamily:columnQualifier1",
|
batchUpdate.put("myColumnFamily:columnQualifier1",
|
||||||
"columnQualifier1 value!".getBytes());
|
Bytes.toBytes("columnQualifier1 value!"));
|
||||||
|
|
||||||
// Deletes are batch operations in HBase as well.
|
// Deletes are batch operations in HBase as well.
|
||||||
batchUpdate.delete("myColumnFamily:cellIWantDeleted");
|
batchUpdate.delete("myColumnFamily:cellIWantDeleted");
|
||||||
|
@ -235,7 +239,9 @@ public class MyClient {
|
||||||
// value contained is a string and want an actual string, then you must
|
// value contained is a string and want an actual string, then you must
|
||||||
// convert it yourself.
|
// convert it yourself.
|
||||||
Cell cell = table.get("myRow", "myColumnFamily:columnQualifier1");
|
Cell cell = table.get("myRow", "myColumnFamily:columnQualifier1");
|
||||||
String valueStr = new String(cell.getValue());
|
// This could throw a NullPointerException if there was no value at the cell
|
||||||
|
// location.
|
||||||
|
String valueStr = Bytes.toString(cell.getValue());
|
||||||
|
|
||||||
// Sometimes, you won't know the row you're looking for. In this case, you
|
// Sometimes, you won't know the row you're looking for. In this case, you
|
||||||
// use a Scanner. This will give you cursor-like interface to the contents
|
// use a Scanner. This will give you cursor-like interface to the contents
|
||||||
|
@ -245,30 +251,31 @@ public class MyClient {
|
||||||
table.getScanner(new String[]{"myColumnFamily:columnQualifier1"});
|
table.getScanner(new String[]{"myColumnFamily:columnQualifier1"});
|
||||||
|
|
||||||
|
|
||||||
// Scanners in HBase 0.2 return RowResult instances. A RowResult is like the
|
// Scanners return RowResult instances. A RowResult is like the
|
||||||
// row key and the columns all wrapped up in a single interface.
|
// row key and the columns all wrapped up in a single Object.
|
||||||
// RowResult#getRow gives you the row key. RowResult also implements
|
// RowResult#getRow gives you the row key. RowResult also implements
|
||||||
// Map, so you can get to your column results easily.
|
// Map, so you can get to your column results easily.
|
||||||
|
|
||||||
// Now, for the actual iteration. One way is to use a while loop like so:
|
// Now, for the actual iteration. One way is to use a while loop like so:
|
||||||
RowResult rowResult = scanner.next();
|
RowResult rowResult = scanner.next();
|
||||||
|
|
||||||
while(rowResult != null) {
|
while (rowResult != null) {
|
||||||
// print out the row we found and the columns we were looking for
|
// print out the row we found and the columns we were looking for
|
||||||
System.out.println("Found row: " + new String(rowResult.getRow()) + " with value: " +
|
System.out.println("Found row: " + Bytes.toString(rowResult.getRow()) +
|
||||||
rowResult.get("myColumnFamily:columnQualifier1".getBytes()));
|
" with value: " + rowResult.get(Bytes.toBytes("myColumnFamily:columnQualifier1")));
|
||||||
|
|
||||||
rowResult = scanner.next();
|
rowResult = scanner.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The other approach is to use a foreach loop. Scanners are iterable!
|
// The other approach is to use a foreach loop. Scanners are iterable!
|
||||||
for (RowResult result : scanner) {
|
for (RowResult result : scanner) {
|
||||||
// print out the row we found and the columns we were looking for
|
// print out the row we found and the columns we were looking for
|
||||||
System.out.println("Found row: " + new String(result.getRow()) + " with value: " +
|
System.out.println("Found row: " + Bytes.toString(rowResult.getRow()) +
|
||||||
result.get("myColumnFamily:columnQualifier1".getBytes()));
|
" with value: " + rowResult.get(Bytes.toBytes("myColumnFamily:columnQualifier1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure you close your scanners when you are done!
|
// Make sure you close your scanners when you are done!
|
||||||
|
// Its probably best to put the iteration into a try/finally with the below
|
||||||
|
// inside the finally clause.
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue