Hadoop-06-using eclipse to develop hbase programs

Source: Internet
Author: User

configuration steps for developing HBase programs using Eclipse

1. Create a new generic Java project.

2. --java build path--libraries--add external jars, add hadoop installation directory hbase-0.90.5.jar hbase-0.90.5-tests.jar, hbase installation directory lib All in the directory jar file.

3. Create a new conf directory under the project root directory and copy the conf directory under the hbase installation directory . Hbase-site.xml to the folder.

4. in the project's properties --java build Path--libraries--add class folder, the newly created conf The directory is added in . The development environment is now complete.

5. Create a new Java class and add the following code:

Package com.hadoop.hbase.test;

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.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 {

public static void Main (string[] args) throws Exception {

Configuration config = hbaseconfiguration.create ();

CREATE table

Hbaseadmin admin = new hbaseadmin (config);

Htabledescriptor HTD = new Htabledescriptor ("Student");

Hcolumndescriptor HCD = new Hcolumndescriptor ("address");

Hcolumndescriptor hcd2 = new Hcolumndescriptor ("info");

Htd.addfamily (HCD);

Htd.addfamily (HCD2);

Admin.createtable (HTD);

byte[] TableName = Htd.getname ();

htabledescriptor[] tables = Admin.listtables ();

if (tables.length! = 1 && bytes.equals (TableName, Tables[0].getname ())) {

throw new Exception ("Failed CREATE TABLE!!!");

}

Run some operations--a put, a get, a scan--against the table

htable table = new htable (config, tableName);

byte[] Row1 = bytes.tobytes ("John");

Put p1 = new put (ROW1);

byte[] Databytes = bytes.tobytes ("Address");

P1.add (Databytes, Bytes.tobytes ("City"), Bytes.tobytes ("WuHan"));

P1.add (Databytes, Bytes.tobytes ("province"), Bytes.tobytes ("Hubei"));

Table.put (p1);

Get g = new Get (ROW1);

Result result = Table.get (g);

SYSTEM.OUT.PRINTLN ("Result:" + result);

Scan scan = new scan ();

Resultscanner scanner = Table.getscanner (scan);

for (Result Scannerresult:scanner) {

System.out.println ("Scan:" + scannerresult);

}

Drop the table

Admin.disabletable (TableName);

Admin.deletetable (TableName);

SYSTEM.OUT.PRINTLN ("----done----");

}

}

6. Enter the hbase shelland use the list command to see the newly created table, using scan ' Student ' You can see the inserted data.

At this point, the first hbase program development is completed.

Hadoop-06-using eclipse to develop hbase programs

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.