Development of HBase Java client under Windows7+eclipse environment

Source: Internet
Author: User
Tags log4j

This article shows how to build a client-side development of hbase through eclipse in Windows environment
  1. to build an HBase cluster, refer to: Configuration of HBASE0.98.10-HADOOP2 cluster under Centos
  2. Creating a Maven project in eclipse
  3. Put the Hbase-site.xml file of the cluster under the classes directory of the project

  4. Configure the operating system's C:\windows\system32\drivers\etc file to configure the HBase cluster's IP and domain name in the file
    192.168.40.108   hadoop108192.168.40.148   hadoop148 192.168.40.104   hadoop104 192.168.40.107   hadoop107 192.168.40.105   hadoop105
  5. write maven's Pom.xml file, depending on the content below
    <dependencies><dependency><groupId>org.apache.avro</groupId><artifactId>avro< /artifactid><version>1.7.7</version></dependency><dependency><groupid> Org.apache.avro</groupid><artifactid>avro-tools</artifactid><version>1.7.7</version ></dependency><dependency><groupId>org.apache.avro</groupId><artifactId> Avro-maven-plugin</artifactid><version>1.7.7</version></dependency><dependency> <groupid>org.apache.avro</groupid><artifactid>avro-compiler</artifactid><version >1.7.7</version></dependency><dependency><groupId>org.apache.hbase</groupId> <artifactid>hbase-client</artifactid><version>0.98.8-hadoop1</version></dependency ><dependency><groupid>org.apache.hbase</groupid><artifactid>hbase</artifactid ><version>0.90.2</vErsion></dependency><dependency><groupid>org.apache.hadoop</groupid><artifactid >hadoop-core</artifactId><version>1.2.1</version></dependency><dependency> <groupid>junit</groupid><artifactid>junit</artifactid><version>3.8.1</version ><scope>test</scope></dependency></dependencies>


  6. 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 ();}}}
  7. Run the program with the output as follows:
    Hadoop107,hadoop108,hadoop104log4j:warn No Appenders could is found for logger ( org.apache.hadoop.metrics2.lib.MutableMetricsFactory). Log4j:warn Please initialize the log4j system properly.log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig For more info.table already exists!insert recored zkb to T Able scores ok.insert recored zkb to table scores Ok.insert recored ZKB to table scores Ok.insert recored zkb to Table SCO Res ok.insert recored baoniu to table scores Ok.insert recored baoniu to table scores ok.===========get one record======== ZKB course:1425258910718 90zkb course:art 1425258910727 87zkb course:math 1425258910722 97zkb grade:1425258910705 5===== ======show all Record========baoniu course:math 1425258910734 89baoniu grade:1425258910730 4zkb course:1425258910718 90 ZKB course:art 1425258910727 87zkb course:math 1425258910722 97zkb grade:1425258910705 5===========del one record======= =del recored baoniu ok.zkb course:1425258910718 90zkb course:art 1425258910727 87zkb course:math 1425258910722 97zkb grade:1425258910705 5===========show all record========zkb course:14252589107 90ZKB course:art 1425258910727 87zkb course:math 1425258910722 97zkb grade:1425258910705 5


Development of HBase Java client under Windows7+eclipse environment

Related Article

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.