一个使用HBase的例子,如下。
import java.io.IOException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scanner;
import org.apache.hadoop.hbase.io.BatchUpdate;
import org.apache.hadoop.hbase.io.Cell;
import org.apache.hadoop.hbase.io.RowResult;
public class MyClient {
public static void main(String args[]) throws IOException {
// You need a configuration object to tell the client where to connect.
// But don't worry, the defaults are pulled from the local config file.
HBaseConfiguration config = new HBaseConfiguration();
// This instantiates an HTable object that connects you to the "myTable"
// table.
HTable table = new HTable(config, "myTable");
// 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
// updates will affect.
BatchUpdate batchUpdate = new BatchUpdate("myRow");
// The BatchUpdate#put method takes a Text that describes what cell you want
// to put a value into, and a byte array that is the value you want to
// store. Note that if you want to store strings, you have to getBytes()
// from the string for HBase to understand how to store it. (The same goes
// for primitives like ints and longs and user-defined classes - you must
// find a way to reduce it to bytes.)
batchUpdate.put("myColumnFamily:columnQualifier1",
"columnQualifier1 value!".getBytes());
// Deletes are batch operations in HBase as well.
batchUpdate.delete("myColumnFamily:cellIWantDeleted");
// Once you've done all the puts you want, you need to commit the results.
// The HTable#commit method takes the BatchUpdate instance you've been
// building and pushes the batch of changes you made into HBase.
table.commit(batchUpdate);
// Now, to retrieve the data we just wrote. The values that come back are
// Cell instances. A Cell is a combination of the value as a byte array and
// the timestamp the value was stored with. If you happen to know that the
// value contained is a string and want an actual string, then you must
// convert it yourself.
Cell cell = table.get("myRow", "myColumnFamily:columnQualifier1");
String valueStr = new String(cell.getValue());
// 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
// of the table.
Scanner scanner =
// we want to get back only "myColumnFamily:columnQualifier1" when we iterate
table.getScanner(new String[]{"myColumnFamily:columnQualifier1"});
// Scanners in HBase 0.2 return RowResult instances. A RowResult is like the
// row key and the columns all wrapped up in a single interface.
// RowResult#getRow gives you the row key. RowResult also implements
// 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:
RowResult rowResult = scanner.next();
while(rowResult != null) {
// print out the row we found and the columns we were looking for
System.out.println("Found row: " + new String(rowResult.getRow()) + " with value: " +
rowResult.get("myColumnFamily:columnQualifier1".getBytes()));
rowResult = scanner.next();
}
// The other approach is to use a foreach loop. Scanners are iterable!
for (RowResult result : scanner) {
// print out the row we found and the columns we were looking for
System.out.println("Found row: " + new String(result.getRow()) + " with value: " +
result.get("myColumnFamily:columnQualifier1".getBytes()));
}
// Make sure you close your scanners when you are done!
scanner.close();
}
}
在这个例子中,使用了HBase中的很多概念,包括:
HBaseConfiguration: 用于告诉client如何连接,连接到哪个HBase的服务器上。
HTable:代表一个HBase表格。
BatchUpdate:用于表格中一行的更新。包括添加某个列,修改某列的值,删除某列等。
commit:table的一个方法。代表某个BatchUpdate操作可以生效了。类似于数据库中的commit操作。
Cell:table中对应某个(行key, 列值,时间戳)下的单元格值。
获取Cell的方法。For example:
table.get("myRow", "myColumnFamily:columnQualifier1");
scanner:用于遍历表格。
rowResult:遍历过程当中保存某行信息。
--
从上面可以看到,HBase中的数据都是Bytes。HBase并不care里面实际存的数据到底是什么数据,只要
该数据可以转化成byte[]即可。