HBase Basic API (Java) operations (add and revise)

Source: Internet
Author: User
Tags zookeeper

Package hbaseexec2;/* * Create a students table and perform related operations */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.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.util.bytes;public class HBaseJavaAPI {// Declare static config private static configuration conf = null;static {conf = Hbaseconfiguration.create (); Conf.set (" Hbase.zookeeper.quorum "," 192.168.6.91 "); Conf.set (" Hbase.zookeeper.property.clientPort "," 2181 ");} Determine if a table exists pRivate Static Boolean isexist (String tableName) throws IOException {hbaseadmin hadmin = new Hbaseadmin (conf); return hadmin . tableexists (tableName);} Create database table public static void CreateTable (String tableName, string[] columnfamilys) throws Exception {// Create a new database administrator hbaseadmin hadmin = new Hbaseadmin (conf), if (Hadmin.tableexists (TableName)) {System.out.println ("table" + Tablename+ "already exists! "); System.exit (0);} else {//Create a description of the students table Htabledescriptor Tabledesc = new Htabledescriptor (tableName);//Add the column family for in the description for (String Columnfamily:columnfamilys) {tabledesc.addfamily (new Hcolumndescriptor (columnfamily));} Build the table hadmin.createtable (TABLEDESC) According to the configured description; SYSTEM.OUT.PRINTLN ("CREATE TABLE" +tablename+ "Success!");}} Delete database table public static void Deletetable (String tableName) throws Exception {//Create a new database administrator hbaseadmin hadmin = new HBASEADMI N (Conf), if (Hadmin.tableexists (tableName)) {//Close a table hadmin.disabletable (tableName); hadmin.deletetable (TableName); System.out.println ("Delete table" +tablename+ "Success!" ");} else {System.out.println ("Deleted Tables" +tabLename+ "does not exist! "); System.exit (0);}} Add a data public static void AddRow (String tableName, String row,string columnfamily, string column, String value) throws E xception {htable table = new htable (conf, tableName); Put put = new put (bytes.tobytes (row));//Specify Rows//Parameters: Column family, column, Value Put.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), Bytes.tobytes (value)); Table.put (put);} Delete a (row) data public static void Delrow (string tableName, String row) throws Exception {htable table = new htable (conf, tabl ename);D elete del = new Delete (bytes.tobytes (Row)); Table.delete (DEL);}  Delete multiple data public static void Delmultirows (String tableName, string[] rows) throws Exception {htable table = new Htable (conf, TableName); list<delete> dellist = new arraylist<delete> (); for (String row:rows) {Delete del = new Delete (Bytes.tobytes ( Row));d Ellist.add (del);} Table.delete (dellist);} Gets a data public static void GetRow (string tableName, String row) throws Exception {htable table = new htable (conf, Tablena ME); Get get = new GeT (bytes.tobytes (row)); Result result = Table.get (get);//output, raw method returns all KeyValue array for (KeyValue RowKV:result.raw ()) {System.out.print ("line name:" + N EW String (Rowkv.getrow ()) + ""); System.out.print ("timestamp:" + rowkv.gettimestamp () + ""); System.out.print ("Column family name:" + New String (rowkv.getfamily ()) + ""); System.out.print ("Column name:" + New String (Rowkv.getqualifier ()) + ""); System.out.println ("Value:" + New String (Rowkv.getvalue ()));}} Get all data public static void Getallrows (String tableName) throws Exception {htable table = new htable (conf, tableName); Scan scan = new scan (); Resultscanner results = Table.getscanner (scan);//output result for (result result:results) {for (KeyValue RowKV:result.raw ()) { System.out.print ("line name:" + New String (Rowkv.getrow ()) + ""); System.out.print ("timestamp:" + rowkv.gettimestamp () + ""); System.out.print ("Column family name:" + New String (rowkv.getfamily ()) + ""); System.out.print ("Column name:" + New String (Rowkv.getqualifier ()) + ""); System.out.println ("Value:" + New String (Rowkv.getvalue ()));}}} Main function public static VOID Main (string[] args) {try {String tableName = "student";//First step: Create database table: "Student" string[] Columnfamilys = {"Info", "Cour    Se "}; Hbasejavaapi.createtable (TableName, Columnfamilys);///Second step: Add data to data table//Add first row of data if (Isexist (TableName)) { Hbasejavaapi.addrow (TableName, "zpc", "info", "Age", "20"); Hbasejavaapi.addrow (TableName, "zpc", "info", "Sex", "boy"); Hbasejavaapi.addrow (TableName, "zpc", "Course", "China", "97"); Hbasejavaapi.addrow (TableName, "zpc", "Course", "math", "128"); Hbasejavaapi.addrow (TableName, "zpc", "course", "Chinese", "85");//Add a second row of data Hbasejavaapi.addrow (TableName, "Henjun", " Info "," Age "," 19 "); Hbasejavaapi.addrow (TableName, "<span style=" font-family:arial, Helvetica, Sans-serif; " >henjun</span> "," info "," Sex "," boy "); Hbasejavaapi.addrow (TableName, "Henjun", "Course", "China", "90"); Hbasejavaapi.addrow (TableName, "Henjun", "Course", "math", "120"); Hbasejavaapi.addrow (TableName, "Henjun", "Course", "中文版", "90");//Add third row of data Hbasejavaapi.addrow (TableName, "NiaopeNg "," info "," Age "," 18 "); Hbasejavaapi.addrow (TableName, "<span style=" font-family:arial, Helvetica, Sans-serif; " >niaopeng</span> "," info "," Sex "," girl "); Hbasejavaapi.addrow (TableName, "Niaopeng", "Course", "China", "100"); Hbasejavaapi.addrow (TableName, "Niaopeng", "Course", "math", "100"); Hbasejavaapi.addrow (TableName, "Niaopeng", "course", "Chinese", "99");//Step three: Get a data System.out.println ("************ * * Obtain a (ZPC) data ************* "); Hbasejavaapi.getrow (TableName, "zpc");//Fourth step: Get All Data System.out.println ("************** Get All Data ***************"); Hbasejavaapi.getallrows (tableName);///Fifth step: Delete a data System.out.println ("************ Delete A (zpc) data ************"); Hbasejavaapi.delrow (TableName, "zpc"); Hbasejavaapi.getallrows (tableName);///Sixth step: Delete multiple data System.out.println ("************** Delete multiple data ***************"); String rows[] = new string[] {"Qingqing", "Xiaoxue"}; Hbasejavaapi.delmultirows (tableName, rows); Hbasejavaapi.getallrows (tableName);///Seventh Step: Delete Database System.out.println ("*************** Delete database table **************"); Hbasejavaapi.deletetable (TableName); SYSTEM.OUT.PRINTLN (Does the table "+tablename+" exist? "+isexist (TableName));} else {System.out.println (tableName + "This database table does not exist!) ");}} catch (Exception e) {e.printstacktrace ();}}}

HBase Basic API (Java) operations (add and revise)

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.