You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by Apache Wiki <wi...@apache.org> on 2007/09/28 23:51:35 UTC

[Lucene-hadoop Wiki] Update of "Hbase/FAQ" by stack

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Lucene-hadoop Wiki" for change notification.

The following page has been changed by stack:
http://wiki.apache.org/lucene-hadoop/Hbase/FAQ

The comment on the change is:
Answered question looking for sample code

New page:
[[Anchor(1)]]
'''1. [#1 Can someone give an example of basic API-usage going against hbase?]'''

The two main client-side entry points are [http://lucene.zones.apache.org:8080/hudson/job/Hadoop-Nightly/javadoc/org/apache/hadoop/hbase/HBaseAdmin.html H!BaseAdmin] and [http://lucene.zones.apache.org:8080/hudson/job/Hadoop-Nightly/javadoc/org/apache/hadoop/hbase/HTable.html H!Table].  Use H!BaseAdmin to create, drop, list, enable and disable tables.  Use it also to add and drop table column families.  For adding, updating and deleting data, use HTable.  Here is some pseudo code absent error checking, imports, etc., that creates a table, adds data, does a fetch of just-added data and then deletes the table.

{{{// First get a conf object.  This will read in the configuration
// that is out in your hbase-*.xml files such as location of the
// hbase master node.
HBaseConfiguration conf = new HBaseConfiguration();
// Create a table named 'test' that has two column families,
// one named 'content, and the other 'anchor'.  The colons
// are required for column family names.
HTableDescriptor desc = new HTableDescriptor("test");
desc.addFamily(new HColumnDescriptor(new Text("content:"));
desc.addFamily(new HColumnDescriptor(new Text("anchor:"));
HBaseAdmin admin = new HBaseAdmin(conf);
admin.createTable(desc);
HTableDescriptor[] tables = admin.listTables();
// New table should be in list of returned tables.
// Or you could call admin.exists();

// Add content to 'column:' on a row named 'row_x'
Text row = new Text("row_x");
long id = table.startUpdate(row);
table.put(id, "content:",
  "some content".getBytes(HConstants.UTF8_ENCODING));
table.commit(id);

// Now fetch the content just added
byte data[] = table.get(row, "content:");

// Delete the table.
admin.deleteTable(desc.getName());}}}

For further examples, check out the hbase unit tests.  These are probably your best source for sample code.  Start with the code in org.apache.hadoop.hbase.TestHBaseCluster.  It does a general table setup and then performs various client operations on the created table: loading, scanning, deleting, etc.

Don't forget your client will need a running hbase instance to connect to (See the ''Getting Started'' section toward the end of this 
[http://lucene.zones.apache.org:8080/hudson/job/Hadoop-Nightly/javadoc/org/apache/hadoop/hbase/package-summary.html#package_description Hbase Package Summary] page).