Java Operations HBase Sample

Source: Internet
Author: User

HBase Installation Method Please refer to: hbase-0.94 installation Method Specific explanation

HBase frequently used shell commands please refer to the shell command sample that HBase often uses


Java operation HBase, creating a Java project in eclipse. Import the jar package and the Lib folder of the HBase installation file root folder into the project, and then you will be able to write Java code to manipulate HBase.

The following code gives a simple demo 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 ();}}}






Java Operations HBase Sample

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.