Hbase Java API Invocation example __hbase

Source: Internet
Author: User
Tags zookeeper

1. First, import hbase-dependent jar packages into your project

2, modify the C:\Windows\System32\drivers\etc\hosts in Windows

10.49.85.152    Master
10.49.85.182    slaver1
10.49.85.183    slaver2

3. Java API

Import java.io.IOException;
Import java.util.ArrayList;

Import java.util.List;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.hbase.Cell;
Import Org.apache.hadoop.hbase.CellUtil;
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.TableName;
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.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.protobuf.generated.ClientProtos.Column; PuBlic class Hbasedemo {static Configuration conf = null;
        static{conf = hbaseconfiguration.create ();
        Conf.set ("Hbase.rootdir", "hdfs://10.49.85.152:9000/hbase");
        Conf.set ("Hbase.master", "hdfs://10.49.85.152:60000");
        Conf.set ("Hbase.zookeeper.property.clientPort", "2181");
    Conf.set ("Hbase.zookeeper.quorum", "Master,slaver1,slaver2"); //implemented the List function public static list<string> Getalltables () throws Masternotrunningexception, Zookeeperconnecti
        Onexception, ioexception{list<string> List = null;
        Hbaseadmin can create tables, delete tables, view tables hbaseadmin admin = new hbaseadmin (conf);
            if (admin!= null) {//htabledescriptor table related information htabledescriptor[] tables = Admin.listtables ();
                for (Htabledescriptor table:tables) {System.out.println (table.getnameasstring ());
                List = new arraylist<string> ();
 List.add (Table.getnameasstring ());           } admin.close ();
    return list; public static int createtabble (String tablename, string[] family) throws Masternotrunningexception, Zookeeperconnec
        Tionexception, ioexception{hbaseadmin admin = new hbaseadmin (conf);
Htabledescriptor table = new Htabledescriptor (tablename.valueof (tablename));
        Htabledescriptor table = new Htabledescriptor (tablename); Hcolumndescriptor column information for (String str:family) {hcolumndescriptor cloumn = new Hcolumndescriptor (s
            TR);
            Cloumn.setmaxversions (3);
        Table.addfamily (Cloumn);
            } if (Admin.tableexists (tablename)) {SYSTEM.OUT.PRINTLN (tablename + "already exists");
        return-1;
        } admin.createtable (table);
        Admin.close ();
    return 1; public static void Deletetable (String tablename) throws Masternotrunningexception, Zookeeperconnectionexception, IO exception{Hbaseadminadmin = new Hbaseadmin (conf);
        Admin.disabletable (tablename);
        Admin.deletetable (tablename);
        SYSTEM.OUT.PRINTLN (tablename + "successful deletion");
    Admin.close (); public static void Insert (String tablename, String Rowkey, String family, string column, String cell) throws Ioexce
        ption{put on = new put (rowkey.getbytes ());
        Htable is responsible for the table's get put delete operation htable table = new htable (conf, tablename);

        Put.add (Family.getbytes (), Column.getbytes (), cell.getbytes ());

    Table.put (Put); public static result Querybyrow (String tablename, String rowkey) throws ioexception{get get = new Get (Rowk
        Ey.getbytes ());
        htable table = new htable (conf, tablename);
    return Table.get (GET); public static result Querybycolumn (String tablename, String Rowkey, String family, String column) throws Ioexceptio
        n{Get get = new Get (Rowkey.getbytes ());
   htable table = new htable (conf, tablename);     Get.addcolumn (Family.getbytes (), column.getbytes ());
    return Table.get (GET); public static result Querybyrowbyversions (String tablename, String rowkey) throws ioexception{get get = NE
        W Get (Rowkey.getbytes ());
        Get.setmaxversions (3);
        htable table = new htable (conf, tablename);
    return Table.get (GET);      public static Resultscanner Querybyscan (String tablename) throws ioexception{Scan = new Scan ();
Scan.setstartrow (StartRow);
Scan.setstoprow (Stoprow);
        Scan.addcolumn (family, qualifier);
        htable table = new htable (conf, tablename);
    return Table.getscanner (scan); public static void Deletebycolumn (String tablename, String Rowkey, String family, String column) throws IOException
        {Delete delete = new Delete (Rowkey.getbytes ());
        htable table = new htable (conf, tablename);
        Delete.deletecolumn (Family.getbytes (), column.getbytes ()); Table.delEte (delete);
    SYSTEM.OUT.PRINTLN ("successful deletion"); public static void Deletebyrow (String tablename, String rowkey) throws ioexception{Delete delete = new De
        Lete (Rowkey.getbytes ());
        htable table = new htable (conf, tablename);
        Table.delete (delete);
    SYSTEM.OUT.PRINTLN ("successful deletion"); public static void Main (string[] args) throws Masternotrunningexception, Zookeeperconnectionexception, IOException

        {//1. Create Book table//Createtabble ("book", New string[]{"Author", "Info"});//Getalltables ();
2. Delete Book table//Deletetable ("book");

        Getalltables ();
3. Create Book table//Createtabble ("book", New string[]{"Author", "Info"});

        Getalltables ();
4.book Table Insert Data//Insert ("book", "rw001", "author", "Name", "Zhangsan");
Insert ("book", "rw001", "Author", "Age", "65");
Insert ("book", "rw001", "info", "name", "Database");


    Insert ("book", "rw001", "info", "Price", "35.6");    5. Query data by row (old method)//Result result = Querybyrow ("book", "rw001");
list<keyvalue> values = Result.list ();
System.out.println ("Column\t\t\tcell");
for (KeyValue value:values) {//System.out.print (New String (value.getfamily ()) + ":");
System.out.print (New String (Value.getqualifier ()) + "\t\t");
System.out.print ("value =" + New String (Value.getvalue ()) + ",");
System.out.println ("timestamp =" + Value.gettimestamp ());
//5. Query data by row (new method)//Result result = Querybyrow ("book", "rw001");
Cell[] cells = Result.rawcells ();
System.out.println ("Column\t\t\tcell");
for (cell cell:cells) {//System.out.print (New String (cellutil.clonefamily (cell) + ":");
System.out.print (New String (Cellutil.clonequalifier (cell)) + "\t\t");
System.out.print ("value =" + New String (Cellutil.clonevalue (cell) + ","); System.out.println ("Timestamp = "+ Cell.gettimestamp ());
//6. Query data by column (new method)//Result result = Querybycolumn ("book", "rw001", "author", "Name");
Cell[] cells = Result.rawcells ();
System.out.println ("Column\t\t\tcell");
for (cell cell:cells) {//System.out.print (New String (cellutil.clonefamily (cell) + ":");
System.out.print (New String (Cellutil.clonequalifier (cell)) + "\t\t");
System.out.print ("value =" + New String (Cellutil.clonevalue (cell) + ",");
System.out.println ("timestamp =" + Cell.gettimestamp ());
}//7.scan//Resultscanner scanner = Querybyscan ("book");
System.out.println ("Column\t\t\tcell");  for (result result:scanner) {//cell[] cells = result.rawcells ()////for (cell cell
: cells) {//System.out.print (New String (cellutil.clonefamily (cell)) + ":"); System.out.print (New String (Cellutil.clonequalifier (cell) + "\T\t ");
System.out.print ("value =" + New String (Cellutil.clonevalue (cell) + ",");
System.out.println ("timestamp =" + Cell.gettimestamp ());

        //}//8. Deletes a column//Deletebycolumn ("book", "rw001", "author", "Name");

        9. Delete a row of data//Deletebyrow ("book", "rw001"); 10.
        Query multiple versions of data by row (new method) Result result = Querybyrowbyversions ("book", "rw001");
        Cell[] cells = Result.rawcells ();
        System.out.println ("Column\t\t\tcell");
            for (cell cell:cells) {System.out.print (New String (cellutil.clonefamily (cell) + ":");
            System.out.print (New String (Cellutil.clonequalifier (cell)) + "\t\t");
            System.out.print ("value =" + New String (Cellutil.clonevalue (cell) + ",");
        System.out.println ("timestamp =" + Cell.gettimestamp ()); }

    }

}

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.