HBase Javaapi Operation Example

Source: Internet
Author: User


Package Testhbase;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 TestHBase {static Configuration cfg = hbaseconfiguration.create (); Get config object public static void Create (String tableName, String columnfamily) throws exception{hbaseadmin admin = new HBASEADMI N (CFG); Get Hbaseadmin object for Operation Hbaseif (Admin.tableexists (tableName)) {System.out.println ("Table" + tableName + "exists!"); System.exit (0);} Else{htabledescriptor Tabledesc = new Htabledescriptor (tableName);//Get Table Description object used when creating table TabledeSc.addfamily (New Hcolumndescriptor (columnfamily)); Hcolumndescriptor Column Family Description Object admin.createtable (TABLEDESC); CREATE TABLE System.out.println ("table" + TableName + "success!");}} static void put (String tablename,string row,string columnfamily,string column,string data) throws Exception{htable table = new Htable (cfg,tablename); Get the Table object put put = new put (bytes.tobytes (row));//Insert Data Put.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), Bytes.tobytes (data)); Table.put (put); System.out.println ("put" + row + "," + "columnfamily:" + column + "," + data "); static void Get (String tablename,string row) throws exception{htable table = new htable (cfg,tablename); Get get = new Get (bytes.tobytes (row));//Gets the row data result = Table.get (get); System.out.println ("Get:" + result); static void Scan (String tableName) throws exception{htable table = new htable (cfg,tablename); Scan scan = new Scan (bytes.tobytes (tableName)); Scan Full table Resultscanner rs = table.getscanner (scan), for (Result r:rs) {System.out.println ("Scan:"+ R);}} Static Boolean Delete (String tableName) throws exception{hbaseadmin admin = new Hbaseadmin (CFG), if (Admin.tableexists ( TableName)) {admin.disabletable (tableName); First DisableSystem.out.println ("Disable Table" + tableName + "success!"); Admin.deletetable (TableName); Delete System.out.println ("delete Table" + tableName + "success!");} return true;} public static void Main (string[] args) throws Exception {Cfg.set ("Hbase.master", "hadoop:60000"); Cfg.set (" Hbase.rootdir "," hdfs://hadoop:9000/hbase "); Cfg.set (" Hbase.zookeeper.quorum "," Hadoop "); String tableName = "Hbase_test"; String columnfamily = "CF"; Create (TableName, columnfamily);p ut (tableName, "Row1", columnfamily, "C1", "Data"), scan ( TableName); get (TableName, "Row1");d elete (TableName);}}

  

HBase Javaapi Operation Example

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.