Package org.test;
Import java.io.IOException;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.NavigableMap;
Import Java.util.Set;
Import Java.util.Vector;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.HColumnDescriptor;
Import Org.apache.hadoop.hbase.HTableDescriptor;
Import Org.apache.hadoop.hbase.KeyValue;
Import org.apache.hadoop.hbase.MasterNotRunningException;
Import org.apache.hadoop.hbase.ZooKeeperConnectionException;
Import org.apache.hadoop.hbase.client.*;
Import org.apache.hadoop.hbase.util.Bytes;
/*
* Tab_global Param:userid
*
* Tab_user2id Info:id
*
* Tab_id2user Info:username, Info:password
*
* Tab_users user:follow user:followd user:inbox user:sent
*
* Tab_post post:content
*
* */
HBase Interface Class
public class Hbaseif {
Configuration conf;
public static Hbaseif ghbase = null;
public static Hbaseif getinstance () {
if (ghbase = = null)
Ghbase = new Hbaseif ();
return ghbase;
}
Hbaseif () {
conf = Hbaseconfiguration.create ();
}
Ways to create tables
public void Create_table (string name, string col, int version)
Throws Exception {
Hbaseadmin admin = new hbaseadmin (conf);
First check if the table exists
if (admin.tableexists (name)) {
Admin.disabletable (name);
Admin.deletetable (name);
}
Htabledescriptor Tabledesc = new Htabledescriptor (name);
Hcolumndescriptor HD = new Hcolumndescriptor (col);
Hd.setmaxversions (version);
Tabledesc.addfamily (HD);
Admin.createtable (TABLEDESC);
Admin.close ();
}
Public list<post> Getpost (String username) throws exception{
list<post> list = new arraylist<post> ();
Long id = this.getidbyusername (username);
byte[] begin = Bytes.add (bytes.tobytes (ID), bytes.tobytes (Long.max_value-long.max_value));
byte[] begin = Bytes.tobytes (ID);
byte[] End = Bytes.add (Bytes.tobytes (ID), bytes.tobytes (Long.max_value));
byte[] End = Bytes.tobytes (id+1);
Scan s = new scan ();
S.setstartrow (begin);
S.setstoprow (end);
htable tab_post = new htable (conf, "tab_post");
htable Tab_inbox = new htable (conf, "Tab_inbox");
Resultscanner SS = Tab_inbox.getscanner (s);
Get get = null;
Post p = null;
for (Result r:ss) {
byte[] PostID = R.getvalue (Bytes.tobytes ("PostID"), null);
get = new Get (PostID);
Result rs = Tab_post.get (get);
String post_username = bytes.tostring (Rs.getvalue (Bytes.tobytes ("post"), Bytes.tobytes ("username"));
String post_content = bytes.tostring (Rs.getvalue (Bytes.tobytes ("post"), Bytes.tobytes ("content"));
String post_ts = bytes.tostring (Rs.getvalue (Bytes.tobytes ("post"), Bytes.tobytes ("ts"));
p = new Post (Post_username, Post_content, post_ts);
List.add (0,P);
}
return list;
}
public Boolean post (string username, string content)
Throws Exception {
htable Tab_global = new htable (conf, "Tab_global");
htable tab_post = new htable (conf, "tab_post");
Long id = tab_global.incrementcolumnvalue (bytes.tobytes ("Row_postid"),
Bytes.tobytes ("param"), bytes.tobytes ("PostID"), 1);
byte[] PostID = bytes.tobytes (ID);
Insert record in Tab_post
Put put = new put (PostID);
DateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String ts = Dateformat.format (new Date ());
Put.add (Bytes.tobytes ("post"), Bytes.tobytes ("username"), username.getbytes ());
Put.add (Bytes.tobytes ("post"), Bytes.tobytes ("content"), content.getbytes ());
Put.add (Bytes.tobytes ("post"), Bytes.tobytes ("ts"), ts.getbytes ());
Tab_post.put (Put);
Tab_global.close ();
Tab_post.close ();
Send the Post
Long SenderID = This.getidbyusername (username);
SYSTEM.OUT.PRINTLN ("Sender ID:" + senderid);
byte[] begin = Bytes.add (Bytes.tobytes (SenderID), Bytes.tobytes (Long.max_value-long.max_value));
byte[] End = Bytes.add (Bytes.tobytes (SenderID), Bytes.tobytes (Long.max_value));
Scan s = new scan ();
S.setstartrow (begin);
S.setstoprow (end);
htable tab_followed = new htable (conf, "tab_followed");
htable Tab_inbox = new htable (conf, "Tab_inbox");
Resultscanner SS = Tab_followed.getscanner (s);
put = new put (Bytes.add (Bytes.tobytes (SenderID), PostID));
Put.add (Bytes.tobytes ("PostID"), null, PostID);
Tab_inbox.put (Put);
for (Result r:ss) {
Byte[] did = R.getvalue (bytes.tobytes ("userid"), NULL);
put = new put (Bytes.add (did, PostID));
Put.add (Bytes.tobytes ("PostID"), null, PostID);
Tab_inbox.put (Put);
}
Tab_followed.close ();
Tab_inbox.close ();
return true;
}
Execute CREATE TABLE method
public void Createtables () throws Exception {
Create Tag_global and initialization
Create_table ("Tab_global", "param", 1);
htable ht = new htable (conf, "Tab_global");
Put put = new put (Bytes.tobytes ("Row_userid"));
Long id = 0;
Put.add (bytes.tobytes ("param"), bytes.tobytes ("userid"),
Bytes.tobytes (ID));
Ht.put (Put);
put = new put (Bytes.tobytes ("Row_postid"));
Put.add (bytes.tobytes ("param"), bytes.tobytes ("PostID"),
Bytes.tobytes (ID));
Ht.put (Put);
Create Tab_user2id
Create_table ("Tab_user2id", "info", 1);
Create Tab_id2user
Create_table ("Tab_id2user", "info", 1);
/*
* Tab_follow Rowkey:userid CF:name:userid = Username Version = 1
*/
Create_table ("Tab_follow", "name", 1);
/*
* tab_followed Rowkey:userid_{userid} cf:userid = userid
*/
Create_table ("tab_followed", "userid", 1);
/*
* Tab_post
* Rowkey:postid
* Cf:content
* */
Create_table ("Tab_post", "post", 1);
/*
* Tab_inbox
* Rowkey:userid+postid
* Cf:postid
*/
Create_table ("Tab_inbox", "PostID", 1);
}
Get all Users
Public set<string> Getalluser () throws Exception {
set<string> set = new hashset<string> ();
htable tab_user2id = new htable (conf, "tab_user2id");
Scan s = new scan ();
Resultscanner SS = Tab_user2id.getscanner (s);
for (Result r:ss) {
String name = new String (R.getrow ());
Set.add (name);
System.out.print (name);
}
return set;
}
Public set<string> Getfollow (String username) throws Exception {
Long id = this.getidbyusername (username);
set<string> set = new hashset<string> ();
htable Tab_follow = new htable (conf, "Tab_follow");
Get get = new Get (bytes.tobytes (ID));
Result rs = Tab_follow.get (get);
For (KeyValue Kv:rs.raw ()) {
string s = new string (Kv.getvalue ());
Set.add (s);
System.out.println (s);
}
Tab_follow.close ();
return set;
}
public boolean Alreadyfollow (Long oid, Long did) throws Exception {
htable tab_users = new htable (conf, "tab_users");
Get get = new Get (Bytes.tobytes (OID));
Get.setmaxversions (500);
Result rs = Tab_users.get (get);
list<keyvalue> list = Rs.getcolumn (bytes.tobytes ("User"),
Bytes.tobytes ("Follow"));
Tab_users.close ();
for (KeyValue kv:list) {
if (did = = Bytes.tolong (Kv.getvalue ()))
return true;
}
return false;
}
public boolean follow (string oname, String dname) throws Exception {
Long oid = This.getidbyusername (oname);
Long did = This.getidbyusername (dname);
if (oid = = 0 | | did = 0 | | oid = = did)
return false;
/*
* Tab_follow Rowkey:userid CF:name:userid = Username Version = 1
*/
htable Tab_follow = new htable (conf, "Tab_follow");
Put put = new put (Bytes.tobytes (OID));
Put.add (bytes.tobytes ("name"), Bytes.tobytes (did), Dname.getbytes ());
Tab_follow.put (Put);
Tab_follow.close ();
/*
* tab_followed Rowkey:userid_{userid} cf:userid = userid
*/
htable tab_followed = new htable (conf, "tab_followed");
put = new put (Bytes.add (Bytes.tobytes (did), Bytes.tobytes (OID));
Put.add (Bytes.tobytes ("userid"), NULL, Bytes.tobytes (OID));
Tab_followed.put (Put);
Tab_followed.close ();
return true;
}
public boolean unfollow (string oname, String dname) throws Exception {
Long oid = This.getidbyusername (oname);
Long did = This.getidbyusername (dname);
if (oid = = 0 | | did = 0 | | oid = = did)
return false;
/*
* Tab_follow Rowkey:userid CF:name:userid = Username Version = 1
*/
htable Tab_follow = new htable (conf, "Tab_follow");
Delete del = new Delete (Bytes.tobytes (OID));
Del.deletecolumns (bytes.tobytes ("name"), Bytes.tobytes (did));
Tab_follow.delete (DEL);
Tab_follow.close ();
/*
* tab_followed Rowkey:userid_{userid} cf:userid = userid
*/
htable tab_followed = new htable (conf, "tab_followed");
del = new Delete (Bytes.add (Bytes.tobytes (did), Bytes.tobytes (OID));
Tab_followed.delete (DEL);
Tab_followed.close ();
return true;
}
public Boolean deleteuser (Long id) throws Exception {
String username = Getnamebyid (ID);
if (Username.equals (""))
return false;
htable tab_user2id = new htable (conf, "tab_user2id");
htable tab_id2user = new htable (conf, "Tab_id2user");
Delete del = new Delete (Username.getbytes ());
Tab_user2id.delete (DEL);
del = new Delete (bytes.tobytes (id));
Tab_id2user.delete (DEL);
Tab_user2id.close ();
Tab_id2user.close ();
return true;
}
Add user
public boolean CreateNewUser (string name, string password)
Throws IOException {
htable Tab_global = new htable (conf, "Tab_global");
htable tab_user2id = new htable (conf, "tab_user2id");
htable tab_id2user = new htable (conf, "Tab_id2user");
if (Tab_user2id.exists (New Get (Name.getbytes ())))
return false;
Long id = tab_global.incrementcolumnvalue (bytes.tobytes ("Row_userid"),
Bytes.tobytes ("param"), bytes.tobytes ("userid"), 1);
Insert record in Tab_user2id
Put put = new put (name.getbytes ());
Put.add (bytes.tobytes ("info"), bytes.tobytes ("id"), bytes.tobytes (ID));
Tab_user2id.put (Put);
Insert record in Tab_id2user
put = new put (Bytes.tobytes (id));
Put.add (bytes.tobytes ("info"), Bytes.tobytes ("username"),
Bytes.tobytes (name));
Put.add (bytes.tobytes ("info"), bytes.tobytes ("password"),
Bytes.tobytes (password));
Tab_id2user.put (Put);
Tab_global.close ();
Tab_user2id.close ();
Tab_id2user.close ();
return true;
}
Get user user name by ID
Public String Getnamebyid (long id) {
try {
htable tab_id2user = new htable (conf, "Tab_id2user");
Result rs = tab_id2user.get (new Get (Bytes.tobytes (ID)));
Get the latest column
KeyValue kv = rs.getcolumnlatest (bytes.tobytes ("info"),
Bytes.tobytes ("username"));
Return bytes.tostring (Kv.getvalue ());
} catch (Exception e) {
Return "";
}
}
Public long Getidbyusername (String username) {
try {
htable tab_user2id = new htable (conf, "tab_user2id");
Result rs = Searchbyrowkey (tab_user2id, username);
KeyValue kv = rs.getcolumnlatest (bytes.tobytes ("info"),
Bytes.tobytes ("id"));
byte[] bid = Kv.getvalue ();
Return Bytes.tolong (BID);
} catch (Exception e) {
return 0;
}
}
return 0:not matched >0:match
Public long Checkpassword (string name, string password) throws Exception {
htable tab_user2id = new htable (conf, "tab_user2id");
htable tab_id2user = new htable (conf, "Tab_id2user");
if (!tab_user2id.exists (New Get (Name.getbytes ())))
return 0;
Result rs = Searchbyrowkey (tab_user2id, name);
KeyValue kv = rs.getcolumnlatest (bytes.tobytes ("info"),
Bytes.tobytes ("id"));
byte[] bid = Kv.getvalue ();
Get get = new get (BID);
rs = Tab_id2user.get (get);
KV = Rs.getcolumnlatest (bytes.tobytes ("info"),
Bytes.tobytes ("password"));
String passwordindb = bytes.tostring (Kv.getvalue ());
System.out.println (PASSWORDINDB);
if (!password.equals (PASSWORDINDB))
return 0;
Long id = bytes.tolong (BID);
return ID;
}
Public Result Searchbyrowkey (htable ht, String rk) throws Exception {
Get get = new Get (Rk.getbytes ());
Result rs = Ht.get (get);
Return RS;
}
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
Hbaseif hbase = new Hbaseif ();
Hbase.createtables ();
/*
* H.createtables (); if (H.createnewuser ("Robby1", "Robby"))
* SYSTEM.OUT.PRINTLN ("Add user Success"); Else
* SYSTEM.OUT.PRINTLN ("Add user Failed");
*/
Hbase.createtables ();
Hbase.createnewuser ("user1", "pwd1");
Hbase.createnewuser ("User2", "pwd1");
Hbase.createnewuser ("User3", "pwd1");
Hbase.createnewuser ("User4", "pwd1");
Hbase.createnewuser ("User5", "pwd1");