Create tables, insert data, update data, and delete data in hbase

Source: Internet
Author: User

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 );
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.