Edit Java source code
Package Com.eric.hbase;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.masternotrunningexception;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.util.bytes;public class BaseOperation {private Static final String table_name = "demo_table";p ublic static Configuration conf = null;public htable TABLE = null;p ublic hbaseadmin admin = null;static {conf = Hbaseconfiguration.create (); System.out.println (Conf.get ("Hbase.zookeeper.quorum"));} /** * Create a table */public static void Creattable (String tableName, string[] familys) throws Exception {hbaseadmin admin = new HB Aseadmin (conf), if (Admin.tableexists (TableName)) {System.out.println ("Table already exists!");} else { Htabledescriptor Tabledesc = new Htabledescriptor (tableName); for (int i = 0; i < familys.length; i++) {Tabledesc.addfam ily (New Hcolumndescriptor (Familys[i])); Admin.createtable (TABLEDESC); SYSTEM.OUT.PRINTLN ("CREATE TABLE" + TableName + "OK.");}} /** * Delete table */public static void Deletetable (String tableName) throws Exception {try {hbaseadmin admin = new Hbaseadmin (conf ); admin.disabletable (tableName); admin.deletetable (TableName); System.out.println ("Delete table" + TableName + "OK."); catch (Masternotrunningexception e) {e.printstacktrace ();} catch (Zookeeperconnectionexception e) {e.printstacktrace ( );}} /** * Insert a row of records */public static VoiD AddRecord (String tableName, String rowkey,string Family, string qualifier, String value) throws Exception {try {htable T able = 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 recored" + RowKey + "to table" + TableName + "OK."); catch (IOException e) {e.printstacktrace ();}} /** * Delete a row of records */public static void Delrecord (String tableName, String RowKey) throws IOException {htable table = new Htable ( conf, tableName); List List = new ArrayList ();D elete del = new Delete (Rowkey.getbytes ()); List.add (del); Table.delete (list); System.out.println ("del recored" + RowKey + "OK."); /** * Find a row of records */public static void Getonerecord (String tableName, String RowKey) throws IOException {htable table = new Htab Le (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 ()));}} /** * Show all data */public static void Getallrecord (String tableName) {try {htable table = new htable (conf, tableName); Scan s = new scan (); Resultscanner SS = Table.getscanner (s); for (Result R:ss) {for (KeyValue Kv:r.raw ()) {System.out.print (new String (kv.ge Trow ()) + ""); 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 ()));}}} catch (IOException e) {e.printstacktrace ();}} public static void Main (string[] agrs) {try {String tablename = "scores"; String[] Familys = {"Grade", "course"}; Baseoperation.creattable (tablename, familys);//Add Record Zkbbaseoperation.addrecord (tablename, "ZKB", "Grade", "", " 5 "); Baseoperation.addrecord (tablename, "ZKB", "Course", "", "90"); Baseoperation.addrecord (tablename, "ZKB", "Course", "math", "97"); Baseoperation.addrecord (tablename, "ZKB", "course", "Art", "the");//Add Record Baoniubaseoperation.addrecord ( TableName, "Baoniu", "Grade", "", "4"); Baseoperation.addrecord (tablename, "Baoniu", "Course", "math", "89"); System.out.println ("===========get one record========"); Baseoperation.getonerecord (tablename, "ZKB"); System.out.println ("===========show all record========"); Baseoperation.getallrecord (tablename); System.out.println ("===========del one record========"); Baseoperation.delrecord (tablename, "Baoniu"); Baseoperation.getallrecord (tablename); System.out.println ("===========show all record========"); Baseoperation.getallrecord (tablename);} catch (Exception e) {e.printstacktrace ();}}}