The required packages include:
Commons-codec-1.4.jar
Commons-logging-1.1.1.jar
Hadoop-0.20.2-core.jar
Hbase-0.90.2.jar
Log4j-1.2.16.jar
Zookeeper-3.3.2.jar
Background:
Suppose there is a table that does not know what to do :)
Personnel and their corresponding department information need to be stored in the table
Code:
Import java. util. arraylist;
Import java. util. List;
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. Client. Delete;
Import org. Apache. hadoop. hbase. Client. hbaseadmin;
Import org. Apache. hadoop. hbase. Client. htable;
Import org. Apache. hadoop. hbase. Client. Put;
Public class hbaseaddedtdel {
Public static configuration = NULL;
Static {
Configuration = hbaseconfiguration. Create ();
Configuration. Set ("hbase. Master", "192.168.0.201: 60000 ");
Configuration. Set ("hbase. zookeeper. Quorum", "192.168.0.201, 192.168.0.202, 192.168.0.203 ");
Configuration. Set ("hbase. zookeeper. Property. clientport", "2181 ");
}
Public static void main (string [] ARGs) throws exception {
Hbaseadmin admin = new hbaseadmin (configuration );
If (Admin. tableexists ("riapguh ")){
System. Out. println ("Delete table ");
Admin. disabletable ("riapguh ");
Admin. deletetable ("riapguh ");
}
// Create a riapguh table
System. Out. println ("create table ");
Htabledescriptor tabledescripter = new htabledescriptor ("riapguh". getbytes (); // create a table
Tabledescripter. addfamily (New hcolumndescriptor ("user"); // create a column cluster user
Tabledescripter. addfamily (New hcolumndescriptor ("DPT"); // create a column cluster DPT
Admin. createtable (tabledescripter );
Htable table = new htable (configuration, "riapguh ");
// Insert data
System. Out. println ("add riapguh data ");
List <put> putuser = new arraylist <put> ();
Put user1 = new put (new string ("user a"). getbytes ());
// Write user information
User1.add (new string ("user"). getbytes (), new string ("user_code"). getbytes (), new string ("u_0001"). getbytes ());
User1.add (new string ("user "). getbytes (), new string ("user_name "). getbytes (), new string ("U _ User "). getbytes ());
// Write Department Information
User1.add (new string ("DPT"). getbytes (), new string ("dpt_code"). getbytes (), new string ("d_001"). getbytes ());
User1.add (new string ("DPT "). getbytes (), new string ("dpt_name "). getbytes (), new string ("D _ department "). getbytes ());
Putuser. Add (user1 );