Putting it all together Example
Now that you’ve seen how to interact with HBase, let’s assemble what you know into a
working example. To start, define a simple model object for the User instances, as in
the next listing.
<span style="font-size:14px;">package HBaseIA.TwitBase.model; public abstract class User { public String user; public String name; public String email; public String password; @Override public String toString() { return String.format("<User: %s, %s, %s>", user, name, email); } }</span>
Let’s wrap all the user-centric HBase interactions in a single class. Start by declaring
the commonly used byte[] constants. Then define some helper methods to encapsulate
command creation. Follow that with the public interfaces and a private implementation
of the User model, as shown next.
<span style="font-size:14px;"><pre name="code" class="java">package HBaseIA.TwitBase.hbase; //... public class UsersDAO { public static final byte[] TABLE_NAME = Bytes.toBytes("users"); public static final byte[] INFO_FAM = Bytes.toBytes("info"); private static final byte[] USER_COL = Bytes.toBytes("user"); private static final byte[] NAME_COL = Bytes.toBytes("name"); private static final byte[] EMAIL_COL = Bytes.toBytes("email"); private static final byte[] PASS_COL = Bytes.toBytes("password"); public static final byte[] TWEETS_COL = Bytes.toBytes("tweet_count"); private HTablePool pool; public UsersDAO(HTablePool pool) { this.pool = pool; } private static Get mkGet(String user) { Get g = new Get(Bytes.toBytes(user)); g.addFamily(INFO_FAM); return g; } private static Put mkPut(User u) { Put p = new Put(Bytes.toBytes(u.user)); p.add(INFO_FAM, USER_COL, Bytes.toBytes(u.user)); p.add(INFO_FAM, NAME_COL, Bytes.toBytes(u.name)); p.add(INFO_FAM, EMAIL_COL, Bytes.toBytes(u.email)); p.add(INFO_FAM, PASS_COL, Bytes.toBytes(u.password)); return p; } private static Delete mkDel(String user) { Delete d = new Delete(Bytes.toBytes(user)); return d; } public void addUser(String user, String name, String email, String password) throws IOException { HTableInterface users = pool.getTable(TABLE_NAME); Put p = mkPut(new User(user, name, email, password)); users.put(p); users.close(); } public HBaseIA.TwitBase.model.User getUser(String user) throws IOException { HTableInterface users = pool.getTable(TABLE_NAME); Get g = mkGet(user); Result result = users.get(g); if (result.isEmpty()) { return null; } User u = new User(result); users.close(); return u; } public void deleteUser(String user) throws IOException { HTableInterface users = pool.getTable(TABLE_NAME); Delete d = mkDel(user); users.delete(d); users.close(); } private static class User extends HBaseIA.TwitBase.model.User { private User(Result r) { this(r.getValue(INFO_FAM, USER_COL), r.getValue(INFO_FAM, NAME_COL), r.getValue(INFO_FAM, EMAIL_COL), r.getValue(INFO_FAM, PASS_COL), (r.getValue(INFO_FAM, TWEETS_COL) == null) ? Bytes.toBytes(0L) : r.getValue( INFO_FAM, TWEETS_COL)); } private User(byte[] user, byte[] name, byte[] email, byte[] password, byte[] tweetCount) { this(Bytes.toString(user), Bytes.toString(name), Bytes.toString(email), Bytes.toString(password)); this.tweetCount = Bytes.toLong(tweetCount); } private User(String user, String name, String email, String password) { this.user = user; this.name = name; this.email = email; this.password = password; } } } </span>
Listing 2.1 Userdata model
Listing 2.2 CRUD operations in UsersDAO.
The last piece of this puzzle is a main() method. Let’s make a UsersTool, shown in the
next listing, to simplify interaction with the users table in HBase.
<span style="font-size:14px;">package HBaseIA.TwitBase; //... public class UsersTool { public static final String usage = "UsersTool action ...\n" + " help - print this message and exit.\n" + " add user name email password" + " - add a new user.\n" + " get user - retrieve a specific user.\n" + " list - list all installed users.\n"; public static void main(String[] args) throws IOException { if ((args.length == 0) || "help".equals(args[0])) { System.out.println(usage); System.exit(0); } HTablePool pool = new HTablePool(); UsersDAO dao = new UsersDAO(pool); if ("get".equals(args[0])) { System.out.println("Getting user " + args[1]); User u = dao.getUser(args[1]); System.out.println(u); } if ("add".equals(args[0])) { System.out.println("Adding user..."); dao.addUser(args[1], args[2], args[3], args[4]); User u = dao.getUser(args[1]); System.out.println("Successfully added user " + u); } if ("list".equals(args[0])) { for (User u : dao.getUsers()) { System.out.println(u); } } pool.closeTablePool(UsersDAO.TABLE_NAME); } } </span>
With all the code available, you can try the whole thing. In the root directory of this
book’s source code, compile the application jar:
<span style="font-size:14px;">$ mvn package ... [INFO] ----------------------------------------------------- [INFO] BUILD SUCCESS [INFO] ----------------------------------------------------- [INFO] Total time: 20.467s</span>
This produces a twitbase-1.0.0.jar file in the target directory.
Using UsersTool to add Mark to the users table is easy:
$ java -cp target/twitbase-1.0.0.jar \
<span style="font-size:14px;">HBaseIA.TwitBase.UsersTool \ add \ TheRealMT \ "Mark Twain" \ [email protected] \ abc123 Successfully added user <User: TheRealMT></span>
You can list the contents of your table:
<span style="font-size:14px;">$ java -cp target/twitbase-1.0.0.jar \ HBaseIA.TwitBase.UsersTool \ list 21:49:30 INFO cli.UsersTool: Found 1 users. <User: TheRealMT></span>
Now that you’ve seen a little of how to interact with HBase, let’s better understand the
logical and physical data models present in HBase.