Eclipse Development HBase Program

Source: Internet
Author: User

First, install HBase.

Create a Java project to bring in the jar package for Lib in HBase

Then create a class

Package Sun_test_pig;import java.io.IOException; 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.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.util.bytes;public class HBaseTest {Stati    C Configuration cfg=hbaseconfiguration.create (); public static void Creat (String tablename,string columnfamily) throws Exception {hbaseadmin admin = new Hbaseadmin        (CFG);            if (admin.tableexists (tablename)) {System.out.println ("table exists!");        System.exit (0); } else{htabledescriptor Tabledesc = new Htabledescriptor (tablename);            Tabledesc.addfamily (New Hcolumndescriptor (columnfamily));            Admin.createtable (TABLEDESC);        SYSTEM.OUT.PRINTLN ("CREATE Table success!"); }} public static void put (string tablename,string row, string columnfamily,string column,string data) throws Except        Ion {htable table = new htable (CFG, tablename);        Put P1=new put (bytes.tobytes (row));        P1.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), bytes.tobytes (data));        Table.put (p1);    System.out.println ("Put" "+row+" ', ' "+columnfamily+": "+column+" ', ' "+data+" ' "); public static void Get (String tablename,string row) throws ioexception{htable table=new htable (cfg,tabl            ENAME);                Get G=new Get (bytes.tobytes (row));                Result Result=table.get (g);    System.out.println ("Get:" +result); }//Show all data through htable scan to get information about existing tables public static void scan (String tablename) throws Exception{htable table = new htable (CFG, tablename);         Scan s = new scan ();         Resultscanner rs = Table.getscanner (s);         for (Result r:rs) {System.out.println ("Scan:" +r); }} public static Boolean delete (String tablename) throws ioexception{Hbaseadmin Admin=n            EW hbaseadmin (CFG); if (admin.tableexists (tablename)) {try {admin.disabletabl                            E (tablename);                    Admin.deletetable (tablename);                            }catch (Exception ex) {ex.printstacktrace ();                    return false;    }} return true;            public static void Main (string [] agrs) {string tablename= "HBASE_TB";                      String columnfamily= "CF";     try {hbasetest.creat (tablename, columnfamily);       Hbasetest.put (tablename, "Row1", columnfamily, "CL1", "data");            Hbasetest.get (tablename, "Row1");            Hbasetest.scan (tablename);               if (True==hbasetest.delete (tablename)) System.out.println ("Delete table:" +tablename+ "success!");        } catch (Exception e) {e.printstacktrace (); }    }}

Eclipse Development HBase Program

Related Article

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.