Eclipse Remote Connection HBase

Source: Internet
Author: User
Tags rand zookeeper

Create a new Map/reduce project under Eclipse and add the following jar to the build path:

Program code:

 Packagethathbase;Importjava.io.IOException;ImportJava.util.Random;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.hbase.HBaseConfiguration;ImportOrg.apache.hadoop.hbase.HColumnDescriptor;ImportOrg.apache.hadoop.hbase.HTableDescriptor;Importorg.apache.hadoop.hbase.client.Connection;Importorg.apache.hadoop.hbase.client.ConnectionFactory;Importorg.apache.hadoop.hbase.client.HBaseAdmin;Importorg.apache.hadoop.hbase.client.HTable;ImportOrg.apache.hadoop.hbase.client.Put; Public classHellohbase {StaticConfiguration conf; Statichbaseadmin Admin; StaticConnection Conn; StaticHtabledescriptor Tabledescriptor; Statichtable table; StaticPut PutRow1; StaticPut PutRow2; StaticRandom Rand =NewRandom (25);  Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stubinit ();        CreateTable ();    Inserttable (); }        Private Static voidInit ()throwsException {conf=hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.property.clientPort", "2181"); Conf.set ("Hbase.zookeeper.quorum", "Master,slave03,slave04"); Conf.set ("Hbase.master", "master:60000"); Conn=connectionfactory.createconnection (conf); Admin=(Hbaseadmin) conn.getadmin (); } @SuppressWarnings ("Deprecation")    Private Static voidCreateTable ()throwsexception{if(!admin.tableexists ("scores") ) {Tabledescriptor=NewHtabledescriptor ("scores". GetBytes ()); Tabledescriptor.addfamily (NewHcolumndescriptor ("fam1"));            Admin.createtable (Tabledescriptor); }        Else{System.out.println ("Table already exists!"); } table=NewHtable (conf, "scores"); } @SuppressWarnings ("Deprecation")    Private Static voidInserttable ()throwsioexception{PutRow1=NewPut ("Row1". GetBytes ()); PutRow2=NewPut ("Row2". GetBytes ());  for(inti = 0; i<1000; i++){            if(Rand1 () = = 1) {Putrow1.add ("Fam1". GetBytes (), "col1". GetBytes (), String.valueof (Rand2 ()). GetBytes ());                Table.put (PUTROW1); System.out.println (i+ ": Insert into col1."); }            Else{Putrow2.add ("Fam1". GetBytes (), "col2". GetBytes (), String.valueof (Rand2 ()). GetBytes ());                Table.put (PUTROW2); System.out.println (i+ ": Insert into Col2."); }        }    }        Private Static intRand1 () {intr = Rand.nextint (100); if(R < 50)            return1; Else            return2; }        Private Static intRand2 () {intr = Rand.nextint (10000); returnR; }}

The above program calls HBase API, implements a new table, and randomly inserts data into the table.

Eclipse Remote Connection HBase

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.