HBase Client Api-batch Operations

Source: Internet
Author: User

The previous blog said that using the HBase client API to manipulate the records in the HBase table, today we look at how to use the API to bulk manipulate the data in the table.

Install the method in the previous blog if you update (Add/modify/delete) records in HBase, is an update by line, this method in processing a large number of update operations, performance is poor, fortunately in the HBase provides a Batch way to batch update the data table method. Let's see how to batch update by Table.batch () method

To use the batch mode of the Table for batch updating, we need to create a set of put operations that provides an array of object objects equal to the length of the put operation set, which is used to store the results of the operation. Then call "Table.batch" (Actions, results), and then look at the code snippet below.

    private void Batch () throws IOException {//CREATE TABLE ...

        Table table = connection.gettable (tablename.valueof (table_name));
        list<row> actions = new arraylist<row> ();
            for (int i = 0; i < 10000. i++) {Put on = new put (Bytes.tobytes ("Row_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_username), Bytes.tobytes ("user_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_password), Bytes.tobytes ("Password_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_home), Bytes.tobytes ("home_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_office), Bytes.tobytes ("Office_" + i));
        Actions.Add (Put);

        } object[] results = new object[actions.size ()];
        try {table.batch (actions, results); catch (Interruptedexception E) {e.printstacktrace ();
        } Scan Scan = new Scan ();
        Resultscanner Resultscanner = Table.getscanner (scan);
        Iterator<result> it = Resultscanner.iterator ();
            while (It.hasnext ()) {result result = It.next ();
        Printrow (result);

        } table.close (); Delete Table ...}

Complete example code is as follows

Package my.hbasestudy;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.TableName;
Import org.apache.hadoop.hbase.client.*;

Import org.apache.hadoop.hbase.util.Bytes;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.Iterator;

Import java.util.List;

    public class Testbatch {private static final String table_name = "user";
    private static final String column_family_base = "BASE";

    private static final String column_family_address = "Address";
    private static final String Column_username = "USERNAME";
    private static final String Column_password = "PASSWORD";
    private static final String Column_home = "Home";

    private static final String Column_office = "OFFICE";

    Private Connection Connection;
        public static void Main (string[] args) throws Exception {Configuration config = hbaseconfiguration.create (); Connection Connection = ConnectionFactory.createconnection (config);

        Long T1 = System.currenttimemillis ();
        Testbatch t = new Testbatch (connection);

        T.batch ();
        Long t2 = System.currenttimemillis ();

        System.out.println ("Time:" + (T2-T1));
    Connection.close ();
    Public Testbatch (Connection Connection) {this.connection = Connection;

        private void Batch () throws IOException {createtable ();

        Table table = connection.gettable (tablename.valueof (table_name));
        list<row> actions = new arraylist<row> ();
            for (int i = 0; i < 10000. i++) {Put on = new put (Bytes.tobytes ("Row_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_username), Bytes.tobytes ("user_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_password), Bytes.tobytes ("Password_" + i)); Put.addcolumn (Bytes.tobytes (column_family_address), ByteS.tobytes (Column_home), Bytes.tobytes ("home_" + i));
            Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_office), Bytes.tobytes ("Office_" + i));
        Actions.Add (Put);

        } object[] results = new object[actions.size ()];
        try {table.batch (actions, results);
        catch (Interruptedexception e) {e.printstacktrace ();
        } Scan Scan = new Scan ();
        Resultscanner Resultscanner = Table.getscanner (scan);
        Iterator<result> it = Resultscanner.iterator ();
            while (It.hasnext ()) {result result = It.next ();
        Printrow (result);

        } table.close ();
    Deletetable ();

        private void CreateTable () throws IOException {Admin admin = connection.getadmin ();
                    try {tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder (tablename.valueof (table_name)) . addcolumnfamily (COLUMNFAmilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_base)). Build (). addcolumnfamily (ColumnF
            Amilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_address)). Build ().
        Admin.createtable (TABLEDESC);
        finally {admin.close ();

        } private void Deletetable () throws IOException {Admin admin = connection.getadmin ();
            try {admin.disabletable (tablename.valueof (table_name));
        Admin.deletetable (TABLENAME.VALUEOF (table_name));
        finally {admin.close (); } private void Printrow (result result) {if (Bytes.tostring (Result.getrow ())!= null) {STR
            Ingbuilder sb = new StringBuilder ();
            Sb.append (Bytes.tostring (Result.getrow ()));
            Sb.append ("[");
 Sb.append ("base:username=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("username")));           Sb.append (", base:password=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("password"))
            ));
            Sb.append (", address:home=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Home"))); Sb.append (", address:office=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Office"))
            );
            Sb.append ("]");
        System.out.println (Sb.tostring ()); }
    }
}

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.