Java creates an instance of a datasheet in the HBase database

Source: Internet
Author: User
Tags try catch zookeeper

To create a datasheet in HBase through Java, you first need to import the Hbase-client.jar driver package. You can add dependencies in the project pom.xml configuration file:

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client<artifactId>
<version>1.1.0.1</version>
<dependency>

After adding dependencies, we need to create configuration objects and specify Core-site.xml and Hbase-site.xml as resource files.


Configuration config = hbaseconfiguration.create ();
Config.addresource (New Path ("/etc/hbase/conf/hbase-site.xml"));
Config.addresource (New Path ("/etc/hadoop/conf/core-site.xml"));

You also need to set the values of the Hbase.zookeeper.quorum and Hbase.zookeeper.property.clientPort parameters in the configuration object, which can also be found in the Hbase-site.xml configuration file:


Configuration config = hbaseconfiguration.create ();
Config.set ("Hbase.zookeeper.quorum", "127.0.0.1");
Config.set ("Hbase.zookeeper.property.clientPort", "2181"); When the

Configuration object is created, it then creates a Connection object that is connected to the HBase database and obtains the Admin object from this object, which is responsible for implementing the operation to create the data table:
Connection Connection = connectionfactory.createconnection (config);
Admin admin = connection.getadmin ();

Once you have created the Admin object, you can create a datasheet from the following code:


String tablename = "Users";
if (!admin.istableavailable (tablename.valueof (tablename)) {
    htabledescriptor hbasetable = new Htabledescriptor (tablename.valueof (tablename));
    hbasetable.addfamily (New Hcolumndescriptor ("name"));
    hbasetable.addfamily (New Hcolumndescriptor ("Contact_info"));
    hbasetable.addfamily (New Hcolumndescriptor ("Personal_info"));
    admin.createtable (hbasetable);
}

A data table with the "users" name seriously exists, and a new table will be created if the table does not exist, including: family name, contact information, and personal information. The


Complete program is as follows:

package com.wordpress.khodeprasad;
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.tablename;
import org.apache.hadoop.hbase.client.admin;
import org.apache.hadoop.hbase.client.connection;
import org.apache.hadoop.hbase.client.connectionfactory; /** *  @author  jie * * * * public class createtable {    public  Static void main (String[] args)  {        
Createtable object = new createtable ();
        object.createtable (); &NBSP;&NBSP;&NBSP;&NBSP}     public void createtable ()  {         configuration config = hbaseconfIguration.create ();
        config.set ("Hbase.zookeeper.quorum",  "127.0.0.1");         config.set ("Hbase.zookeeper.property.clientPort",  "2181")
;
        Connection connection = null;
        Admin admin = null;         try {        
    connection = connectionfactory.createconnection (config);             admin = connection.getadmin (
);             String tableName =  "
Users ";             if  (!admin.istableavailable ( Tablename.valueof (tablename))  {   &Nbsp;            htabledescriptor hbasetable
 = new htabledescriptor (tablename.valueof (tablename));                 
Hbasetable.addfamily (New hcolumndescriptor ("name"));                 
Hbasetable.addfamily (New hcolumndescriptor ("Contact_info"));                 
Hbasetable.addfamily (New hcolumndescriptor ("Personal_info"));                 
Admin.createtable (hbasetable);             }          } catch  (exception e)  {        &Nbsp;   e.printstacktrace ();         } finally {             try {                 if  (admin != null)  {     
               admin.close ();                 }                  if  (connection ! = null && !connection.isclosed ())  {       
             connection.close ();                 }             } catch  (EXCEPTION&NBSP;E2)  {                 
E2.printstacktrace ();             }          }    &nbsp}}


Java operation HBase to build a table, delete the table and the data for the increase of the check, condition query

1, build the environment

To create a new Java project, add a package that has:

About the Hadoop-core-0.20.204.0.jar of Hadoop

For all jar packages in the Hbase-0.90.4.jar, Hbase-0.90.4-tests.jar, and HBase resource bundle of HBase, in the Lib directory



2. Main procedure


    package com.wujintao.hbase.test;              import java.io.IOException;       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.keyvalue;       import  org.apache.hadoop.hbase.masternotrunningexception;       import  org.apache.hadoop.hbase.zookeeperconnectionexception;       import org.apache.hadoop.hbase.client.delete;       import  org.apache.hadoop.hbase.client.Get;       import  org.apache.hadoop.hbase.client.hbaseadmin;       import  org.apache.hadoop.hbase.client.htable;       import  org.apache.hadoop.hbase.client.htablepool;       import  org.apache.hadoop.hbase.client.put;       import  org.apache.hadoop.hbase.client.result;       import  org.apache.hadoop.hbase.client.resultscanner;       import  org.apache.hadoop.hbase.client.scan;       import  org.apache.hadoop.hbase.filter.filter;       import  org.apache.hadoop.hbase.filter.filterlist;       import org.apache.hadoop.hbase.filter.singlecolumnvaluefilter;       import  org.apache.hadoop.hbase.filter.comparefilter.compareop;       import  org.apache.hadoop.hbase.util.bytes;               public class jintaotest {                   public static Configuration configuration;            static {                configuration = hbaseconfiguration.create ();                configuration.set (" Hbase.zookeeper.property.clientPort ", " 2181 ");                configuration.set ("Hbase.zookeeper.quoRum ", " 192.168.1.100 ");                configuration.set ("Hbase.master",  "192.168.1.100:600000");            }                   public static void main (String[] args)  {                // createtable ("Wujintao");                // insertdata ("Wujintao");    
           // queryall ("Wujintao");               // querybycondition1 ("Wujintao");                // querybycondition2 ( "Wujintao");               //querybycondition3 ("Wujintao");                //deleterow ("Wujintao", " ABCdef ");               deletebycondition ("Wujintao", "abcdef");           }                   /**            *  CREATE TABLE            * @ param tablename           */            public static void createtable (String tableName)   {               system.out.println ("Start  create table&nbsp ... ");               try  {                    hbaseadmin hbaseadmin = new hbaseadmin (configuration);                    if  (Hbaseadmin.tableexists ( TableName)  {//  If there is a table to create, delete it first and then create                         hbaseadmin.disabletable (TableName);                         hbaseadmin.deletetable (tablename);                        system.out.println ( tablename +  "&nbSp;is exist,detele ... ");                    }                    htabledescriptor tabledescriptor = new htabledescriptor ( TableName);                    tabledescriptor.addfamily (New hcolumndescriptor ("Column1"));                    tabledescriptor.addfamily (New  hcolumndescriptor ("Column2"));                    tabledescriptor.addfamily (New hcolumndescriptor ("Column3"));                     Hbaseadmin.createtable (TABLEDESCRIptor);               } catch  ( Masternotrunningexception e)  {                    e.printstacktrace ();         
      } catch  (zookeeperconnectionexception e)  {                    E.printstacktrace ();               }  catch  (ioexception e)  {                    e.printstacktrace ();                }                system.out.priNtln ("END&NBSP;CREATE&NBSP;TABLE&NBSP; ...");           }
                  /**           *  Insert Data             *  @param  tableName           */            public static void insertdata ( String tablename)  {                system.out.println ("START&NBSP;INSERT&NBSP;DATA&NBSP; ...");                htablepool pool = new htablepool (Configuration,  1000);               HTable  table =  (HtabLe)  pool.gettable (tablename);                put put = new put ("112233BBBCCCC". GetBytes ());//  A put represents a row of data, And a new put represents the second row of data, each row a unique Rowkey, where Rowkey the value passed in for the put constructor                 put.add ("Column1". GetBytes (), null,  "AAA". GetBytes ());//  the first column of the bank's data                put.add ("Column2". GetBytes (), null,  "BBB" GetBytes ();//  third column of the bank's data                 put.add ("Column3". GetBytes (), null,  "CCC" GetBytes ());//  third column of the bank's data
               try {                   table.put (Put) ;               } catch  (ioexception e)  {                     E.printstacktrace ();               }                system.out.println ("End
 insert data&nbsp ... ");           }                  /**            *  Delete a table           
 *  @param  tableName           */           public static void droptable (String  TableName)  {               try {                    hbaseadmin admin =  new hbaseadmin (configuration);                    admin.disabletable (tablename);                    admin.deletetable (TableName);                } catch  ( Masternotrunningexception e)  {                    e.printstacktrace ();         
      } catch  (zookeeperconnectionexception e)  {        &nbsP;          e.printstacktrace ();                } catch  (ioexception e)  {                     E.printstacktrace ();               }
                  }           /**            *  Delete a record according to  rowkey            *  @param
 tablename           *  @param  rowkey           */             public statiC void deleterow (String tablename, string rowkey)   {                try {                    HTable table =  New htable (configuration, tablename);           
        list list = new arraylist ();                   delete d1  = new delete (Rowkey.getbytes ());                    list.add (d1);                                        table.delete (list);                    system.out.println ("Delete Row succeeded!");                                    } catch  ( Ioexception e)  {                    e.printstacktrace ();                }                                  }                    /**             *  combination Condition deletion             *  @param  
tablename            *  @param  rowkey            */             public static void deletebycondition (string tablename, string  Rowkey)   {                    //has not yet found a valid API to implement   remove   This functionality according to Rowkey conditions, as well as API operations to clear all data on the table                    }       
                  /**           *  query all data             *  @param  tableName           */            public static void queryall (String tableName)  {               HTablePool  Pool = new htablepool (configuration, 1000);                HTable table =  (htable)  pool.gettable (tablename)
;               try {                   resultscanner  rs = table.getscanner (New scan ());                    for  (RESULT&NBSP;R&NBSP;:&NBSP;RS)  {                         System.out.println ("obtained to Rowkey:"  + new string (R.getrow ()));                        for  ( Keyvalue keyvalue : r.raw ())  {                             SYSTEM.OUT.PRINTLN ("Column:"  + new string (keyvalue.getfamily ())                                      +  "= = = value:"  + new string ( Keyvalue.getvalue ());                       &nbsP;}                    }                } catch  ( Ioexception e)  {                    e.printstacktrace ();                }           }                   /**            *  single condition query, according to Rowkey query only one record           
 *  @param  tableName           */           public static void querybycondition1 (String  tablename) &NBsp {                       htablepool pool = new htablepool (configuration, 1000);                HTable table =  (htable)   Pool.gettable (tablename);                try {                    get scan = new get ("ABCdef". GetBytes ());//  according to Rowkey query                     result r =  table.get (scan);               
    system.out.println ("obtained to Rowkey:"  + new string (R.getRow ()));                   for  (keyvalue  Keyvalue : r.raw ())  {                        system.out.println ("Columns:"  + new  String (keyvalue.getfamily ())                                 +  " = = = Value: " + new string (Keyvalue.getvalue ());                    }                } catch  (ioexception e)  {                    e.printstacktrace ();    &NBSP;&NBSP;&NBSP;&NBsp;        }            }                  /**            *  single condition by query, query multiple records             *  @param  tableName            */           public static void  QueryByCondition2 (string tablename)  {                       try {                    htablepool pool = new  htablepool (configuration, 1000);                    htable table =  (htable)  pool.gettable (tablename);                    filter filter  = new singlecolumnvaluefilter (bytes                            .tobytes ("Column1"), null, compareop.equal, bytes                            .tobytes ("AAA"));  //  Query               When the value of column Column1 is AAA       scan s = new scan ();      
             s.setfilter (filter);                   resultscanner rs =  table.getscanner (s);                    for  (result r : rs)  {                         System.out.println ("obtained to Rowkey:"  + new string (R.getrow ()));                        for  ( Keyvalue keyvalue : r.raw ())  {                             SYSTEM.OUT.PRINTLN ("Column:"  + new string (keyvalue.getfamily ())                                      +  "= = = value:"  + new string (Keyvalue.getvalue ());                        }                    }                } catch  ( Exception e)  {                    e.printstacktrace ();                }                   }                  /**   &NBSP;&NBSP;&NBsp;      *  combination condition Query           
 *  @param  tableName           */           public static void querybycondition3 (String  tablename)  {                       try {                    htablepool pool = new htablepool ( configuration, 1000);               
    HTable table =  (htable)  pool.gettable (tablename);                           list<fiLter> filters = new arraylist<filter> ();                            Filter filter1 = new singlecolumnvaluefilter (bytes                             .tobytes ("Column1"), null, compareop.equal, bytes                             .tobytes ("AAA");             
      filters.add (filter1);                          Filter  Filter2 = new sinGlecolumnvaluefilter (bytes                            .tobytes ("Column2"), null,  compareop.equal, bytes              
             .tobytes ("BBB");                   filters.add ( Filter2);                           Filter filter3 = new  Singlecolumnvaluefilter (bytes                            .tobytes ("Column3"),  null,  CompareOp.EQUAL, Bytes  
                         .tobytes ("CCC");                    filters.add (filter3);                            filterlist filterlist1 = new filterlist (filters);                            scan scan = new scan ();                    scan.setfilter (filterList1);                    resultscanner rs =  table.getscanner (scan);nbsp;                  for   (RESULT&NBSP;R&NBSP;:&NBSP;RS)  {                        system.out.println ("Get to Rowkey:"  +  New string (R.getrow ());                        for  (Keyvalue keyvalue : r.raw ())  {                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Column:"  + new string ( Keyvalue.getfamily ())                                      +  "= = =Value: " + new string" (Keyvalue.getvalue ());                        }                    }                    rs.close ();                       }  catch  (exception e)  {                    e.printstacktrace ();                }                   }              }


Note: You may not see the operation of the update data, in fact, update the operation and add exactly the same, just add it rowkey does not exist, update rowkey already exist, and timstamp the same situation, there is now seems to be no way to achieve hbase data paging query, I don't know if anyone knows how to do it.



hbase Performance Optimization recommendations:

For the previous code, there are a lot of deficiencies, here I do not modify the above code, but to make suggestions for the place, we add

1) configuration

When you call the Create method, two profiles are loaded: Hbase-default.xml and Hbase-site.xml, using the current Java classpath, These configurations configuration set in code will overwrite the same configuration in Hbase-default.xml and Hbase-site.xml, if all two profiles exist and the properties below the corresponding parameters are set



2) about building tables



public void CreateTable (Htabledescriptor desc)



Htabledescriptor represents the schema of the table, and the methods provided are more useful in the

Setmaxfilesize, specify the maximum region size

SETMEMSTOREFLUSHSIZE Specifies the file size of Memstore flush to HDFs

Increase family through addfamily method



public void addfamily (final hcolumndescriptor family)



Hcolumndescriptor represents the schema of column and provides methods that are more commonly used

Settimetolive: Specifies the maximum TTL, the unit is MS, and expired data is automatically deleted.

Setinmemory: Specifies whether to be in memory, useful for small tables, and can be used to improve efficiency. Default shutdown

Setbloomfilter: Specifies whether to use Bloomfilter to improve the efficiency of random queries. Default shutdown

Setcompressiontype: Sets the data compression type. Default no compression.

Setmaxversions: Specifies the maximum number of versions of the data saved. The default is 3.



Note that generally we do not go setinmemory to True, the default is off



3) About warehousing

Official recommendations

Table.setautoflush (FALSE); Set this to false before data is stored in the warehouse

Table.setflushcommits ()///After the storage is completed, manually brush the data

Attention:

In the process of warehousing, Put.setwritetowal (true/flase);

About this item if you do not want a large amount of data to be lost in a stored procedure, the recommendation is set to true, and if it is only during the test walkthrough phase, it is recommended to save time for warehousing to False



4 about getting table instances

Htablepool pool = new Htablepool (configuration, Integer.max_value);

htable table = (htable) pool.gettable (tablename);

It is recommended to use the table connection pool to get the table, what is the role of the specific pool, I want to use the database connection pool students know that I will not repeat

It is not recommended to use new htable (configuration,tablename) to get tables



5) about the query

It is recommended that each query statement be placed in a try Catch statement block, and finally the operation to close the Resultscanner instance and put unused tables back into the Htablepool, as follows



public static void Queryall (String tablename) {
Htablepool pool = new Htablepool (configuration, Integer.max_value);
htable table = null;
Resultscanner rs = null;
try {
Scan Scan = new Scan ();
Table = (htable) pool.gettable (tablename);
rs = Table.getscanner (scan);
for (result R:rs) {
System.out.println ("Get to Rowkey:" + New String (R.getrow ()));
For (KeyValue Keyvalue:r.raw ()) {
SYSTEM.OUT.PRINTLN ("column:" + New String (keyvalue.getfamily ())
+ "= = = value:" + New String (Keyvalue.getvalue ());
}
}
catch (IOException e) {
E.printstacktrace ();
}finally{
Rs.close ()//finally shut down
Pool.puttable (table); In the actual application process, the pool obtains the instance the way should be extracted as the singleton pattern, should not be retrieved once in each method (single example understand?) is to extract the logical class specifically for the pool, with the specific logic that if the pool exists for direct use, if it does not exist, new
}
}



Therefore, the above code has the flaw place, the interested schoolmate may make the corresponding modification to the optimization suggestion

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.