HBase Installation Method Please refer to: hbase-0.94 installation Method detailed
HBase Common shell commands please refer to: hbase Common shell Command example
Java operation HBase, creating a Java project in Eclipse, importing the jar package from the HBase installation file root and the jar package in the Lib directory into the project, then you can write Java code to manipulate HBase. The following code gives a simple example
/** * @date 2015-07-23 21:28:10 * @author sgl */package com.songguoliang.hbase;import java.io.ioexception;import Java.uti L.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 Hbasetest {//Declaration static configuration, Hbaseconfigurationprivate static config Conf=null;static{conf=hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.quorum", "sdw1,sdw2");} /** * Create a table * @date 2015-07-23 21:44:32 * @author SGL * @param tableName table name * @param columnfamilys Column family * @throws ioexception */public static void Createt Able (String Tablename,string[]columnfamilys) throws Ioexception{hbaseadmin admin =new hbaseadmin (conf); if ( Admin.tableexists (TableName)) {System.out.println ("Table already exists!");} Else{htabledescriptor tabledesc=new Htabledescriptor (tableName); for (int i=0;i<columnfamilys.length;i++) { Tabledesc.addfamily (New Hcolumndescriptor (Columnfamilys[i])); Admin.createtable (TABLEDESC); SYSTEM.OUT.PRINTLN ("CREATE TABLE" +tablename+ "success!");}} /** * Add a record * @date 2015-07-23 22:16:30 * @author SGL * @param tableName table name * @param rowKey Line Kin * @param family Column Family * @para M Qualifier Qualifiers * @param value value * @throws ioexception */public static void AddRecord (String tablename,string Rowkey,strin G family,string qualifier,string value) throws ioexception{htable table =new htable (conf, tableName); Put Put=new put (bytes.tobytes (RowKey));p Ut.add (bytes.tobytes (family), Bytes.tobytes (qualifier), Bytes.tobytes (value)); Table.put (put); System.out.println ("Insert Record" +rowkey+ "to Table" +tablename+ "Success");} /** * Delete a row of records * @date 2015-07-23 22:17:53 * @author SGL * @param tableName table name * @param rowKey Health * @throws IOException */PU Blic static void DeleteRecord (String tablename,string rowKey) throws Ioexception{htable table=new htable (conf, tableName ); List<delete>list=new arraylist<delete> ();D elete delete=new Delete (Rowkey.getbytes ()); List.add (delete) ; Table.delete (list); System.out.println ("Delete record" + rowkey+ "success!");} /** * Get a row of records * @date 2015-07-23 22:21:33 * @author SGL * @param tableName table name * @param rowKey Line Jian * @throws IOException */p ublic static void Getonerecord (String tablename,string rowKey) throws Ioexception{htable table=new htable (conf, TableName); Get Get=new Get (Rowkey.getbytes ()); Result Rs=table.get (Get), for (KeyValue Kv:rs.raw ()) {System.out.print (New String (Kv.getrow ()) + ""); System.out.print (New String (kv.getfamily ()) + ":" ); System.out.print (New String (Kv.getqualifier ()) + ""); System.out.print (Kv.gettimestamp () + ""); System.out.println (New String (Kv.getvalue ())); }}/** * Get all data * @date 2015-07-23 22:26:19 * @author SGL * @param tableName table name * @throws IOException */public static void Getallrecord (String tableName) throws Ioexception{htable table=new htable (conf, tableName); Scan scan=new scan (); Resultscanner Scanner=table.getscanner (scan); for (Result Result:scanner) {for (KeyValue Kv:result.raw ()) { System.out.print (New String (Kv.getrow ()) + ""); System.out.print (New String (kv.getfamily ()) + ":"); System.out.print (New String (Kv.getqualifier ()) + ""); System.out.print (Kv.gettimestamp () + ""); System.out.println (New String (Kv.getvalue ())); }}}/** * Delete a table * @date 2015-07-23 22:29:35 * @author SGL * @param tableName table name * @throws IOException */public Static void Deletetable (String tableName) throws Ioexception{hbaseadmin admin=new hbaseadmin (conf); Admin.disabletable ( TableName); admin.deletetable (TableName); System.out.println ("Delete table" +tablename+ "success!");} public static void Main (string[] args) {try {String tablename= "scores"; String[]columnfamilys={"Grade", "course"}; Hbasetest.createtable (TableName, Columnfamilys); Hbasetest.addrecord (TableName, "SGL", "Grade", "", "5"); Hbasetest.addrecord (TableName, "SGL", "Course", "", "90"); Hbasetest.addrecord (TableName, "SGL", "Course", "math", "97"); Hbasetest.addrecord (TableName, "SGL", "course", "Art", "87"); Hbasetest.addrecord (TableName, "Guoguo", "Grade", "", "4"); Hbasetest.addrecord (TableName, "Guoguo", "Course", "math", "89"); System.out.println ("********get one record*********"); Hbasetest.getonerecord (TableName, "SGL"); System.out.println ("********get all record*********"); Hbasetest.getallrecord (TableName); System.out.println ("********delete one record*********"); Hbasetest.deleterecord (tablename, "Guoguo"); Hbasetest.getallrecord (TableName); System.out.println ("********delete table*********"); Hbasetest.deletetable (tableName);} catch (IOException e) {e.printstacktrace ();}}}
Copyright NOTICE: This article is the original blogger article, reproduced please indicate this article link.
Java Operation HBase Example