17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

344<br />

NavigableMap that will describe the data to HBase <strong>and</strong> allow us to access it (annotation #2).<br />

F<strong>in</strong>ally we can get the result out of our navigable map. Gett<strong>in</strong>g the actual value of out HBase<br />

is not quite as clear as one would hope. We take the navigable map <strong>and</strong> call the get method<br />

on it with the name of our field, convert<strong>in</strong>g that field to bytes as we do (remember HBase<br />

only underst<strong>and</strong>s byte arrays). That gives us another navigable map object that conta<strong>in</strong>s the<br />

different values identified by their doma<strong>in</strong>. When we put objects <strong>in</strong>to HBase we specify an<br />

empty doma<strong>in</strong>, so we want to do the same th<strong>in</strong>g when we retrieve them, so we pass <strong>in</strong> an<br />

empty byte array. This will give us the bytes that make of the value identified by the key<br />

passed <strong>in</strong>.<br />

The ‘put’ method is much simpler but posses basically the same functionality, just reversed.<br />

public void put(Str<strong>in</strong>g key, byte[] value) throws IOException {<br />

Put put = new Put(key.getBytes()); #1<br />

put.add("value".getBytes(), "".getBytes(), value); #2<br />

}<br />

_table.put(put); #3<br />

We create a Put Object, specify<strong>in</strong>g the key <strong>in</strong> the constructor (annotation #1). We then add<br />

the value, <strong>in</strong> our default empty doma<strong>in</strong> (annotation #2). Notice aga<strong>in</strong> that we are only<br />

pass<strong>in</strong>g <strong>in</strong> byte arrays to HBase. F<strong>in</strong>ally, we use the table object that we <strong>in</strong>stantiated <strong>in</strong> the<br />

constructor of our class to actually <strong>in</strong>sert the value <strong>in</strong>to the system.<br />

F<strong>in</strong>ally we have the delete method. This method is also very similar to the ‘put’ <strong>and</strong> ‘get’<br />

methods.<br />

public void delete(Str<strong>in</strong>g key) throws IOException {<br />

Delete del = new Delete(key.getBytes()); #1<br />

}<br />

_table.delete(del); #2<br />

First we create the HBase Delete object (annotation #1), then we pass that to the delet<br />

method of the table object (annotation #2). Once aga<strong>in</strong>, we only pass byte arrays to HBase.<br />

Our orig<strong>in</strong>al goal here was to provide an way to manipulate HBase values from <strong>Erlang</strong>. In<br />

persuit of that goal we have def<strong>in</strong>ed a basic API for <strong>in</strong>teract<strong>in</strong>g with HBase. We now cont<strong>in</strong>ue<br />

by tak<strong>in</strong>g a look at the core part of our system, the SimpleCacheHBaseMa<strong>in</strong> class. This class<br />

provides the core functionality for the Java node that will be provid<strong>in</strong>g services to our<br />

system. Code list<strong>in</strong>g 13.2 shows its few member variables <strong>and</strong> a constructor.<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!