HBase two times Java API Development and demo

Source: Internet
Author: User
Tags zookeeper

1. Try the thrift Python/java as well as the HBase client API, with the following conclusions:the installation and release of 1.1 thrift may encounter unknown errors, and the version of Hbase.thrift is changing. The advantage code is simple and requires less content to package. 1.2 HBase Client API, requires a lot of jars, the release version of the capacity is also very large, after packaging nearly hundred trillion. The advantage is, clear, unambiguous.
2. It is recommended to use the HBase client API.
3. The following are technical details.
4. There is a machine/cluster that runs Hadoop and also runs the HBase cluster based on this Hadoop cluster, and also runs a zookeeper cluster, which we collectively refer to as a.
5. There is a cluster responsible for development, we write code on it, compile the code, run the code, we call it B.
6. On B, to modify the/etc/hosts, put the hostname and corresponding IP address of any of the zookeeper servers in a, because the HBase client needs to be connected to the zookeeper, To obtain hmast information for hbase---hbase cluster has multiple hmast, one is the primary hmast, the other is the standby hmaster, if the main hmaster hangs, the standby will be on top, avoiding a single point of failure.
7. Developed on B,In Elipse to build a Java project, add a Lib directory, a Hadoop on a, hbase, zookeeper all jar package, note, is all the jar package, all levels of subdirectories are counted, are copied to the Lib directory, about 130, 90M. Then, add them to the BuildPath.    The advantage of this is that you don't have to find out which class is in which package, life is short, don't waste your time here, it doesn't matter if you waste a bit of disk space. If hadoop,hbase, zookeeper is installed in a directory, you can use a shell statement: For I in ' find.      -name "*.jar";    Do CP $i ~/alljars;    Done Then the jar package under the Alljars is copied to the B's Lib directory, added to the build path, the person familiar with Eclipse knows, not elaborate.
8. Write a simplest hbase demo, check to see if a table exists in HBase, and create it if it doesn't exist. -----------------------------------------package Hbasedemo;

Import java.io.IOException;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.client.HBaseAdmin;
Import Org.apache.hadoop.hbase.HTableDescriptor;
Import Org.apache.hadoop.hbase.HColumnDescriptor;
Import Org.apache.hadoop.hbase.TableName;

public class Main {

public static void Main (string[] args) throws ioexception{
Configuration hbase_conf = new configuration ();
//Brianxxxxoooo is a hostname of any ZOOKEEPR server in a, must be in the/etc/host IP address of B.
Hbase_conf.set ("Hbase.zookeeper.quorum", "brianxxxxoooo");
Zookeeper default port Hbase_conf.set ("Hbase.zookeeper.property.clientPort", "2181");
Configuration conf = hbaseconfiguration.create (hbase_conf);

String tablename= "scores";
String[] Familys = {"Grade", "course"};

Hbaseadmin admin = new hbaseadmin (conf);
if (admin.tableexists (tablename)) {
SYSTEM.OUT.PRINTLN ("Table exist, return!");
Return
}

Htabledescriptor td = New Htabledescriptor (tablename.valueof (TableName));
for (int i = 0; i < familys.length; i++) {
Td.addfamily (New Hcolumndescriptor (Familys[i]));
}
Admin.createtable (TD);
SYSTEM.OUT.PRINTLN ("CREATE TABLE" +tablename+ "OK.");
}
}
-----------------------------------------
9. Note that the HBase client version changes a lot, the specific API calls to be based on the version, sometimes need to refer to multiple versions. For example, a 0.96.x htabledescripter is closer to http://hbase.apache.org/apidocs/index.html, Instead of 0.94 API, but hbaseadmin in 0.94 of the API is there, in 2.0.0 but not, and the official website does not have 0.96 API, very confusing, it is estimated that the situation will continue for some time.
10. More detailed examples------------------------------------------the package hbasedemo;

Import java.io.IOException;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.*;
Import org.apache.hadoop.hbase.client.*;
Import org.apache.hadoop.hbase.util.Bytes;

public class Main {

public static void Main (string[] args) throws ioexception{
Configuration hbase_conf = new configuration ();
Hbase_conf.set ("Hbase.zookeeper.quorum", "brianvxxxxooooo");
Hbase_conf.set ("Hbase.zookeeper.property.clientPort", "2181");
Configuration conf = hbaseconfiguration.create (hbase_conf);

String tablename= "scores";
String[] Familys = {"Grade", "course"};

Hbaseadmin admin = new hbaseadmin (conf);
if (admin.tableexists (tablename)) {
System.out.println ("Table exist!");
}else{
Htabledescriptor td = New Htabledescriptor (tablename.valueof (TableName));
for (int i = 0; i < familys.length; i++) {
Td.addfamily (New Hcolumndescriptor (Familys[i]));
}
Admin.createtable (TD);
SYSTEM.OUT.PRINTLN ("CREATE TABLE" +tablename+ "OK.");
}

htable table = new htable (conf, "scores");
Put put = new put (Bytes.tobytes ("Row1"));

Create
Put.add (Bytes.tobytes ("Grade"), Bytes.tobytes ("G1"), Bytes.tobytes (781));
Put.add (Bytes.tobytes ("Grade"), Bytes.tobytes ("G2"), Bytes.tobytes ("This is Test");
Table.put (Put);

Read
Get get = new Get (Bytes.tobytes ("Row1"));
Get.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("G1"));
Result result = Table.get (get);
Byte[] val = result.getvalue (Bytes.tobytes ("Grade"), Bytes.tobytes ("G1"));
System.out.println (Bytes.toint (Val));

}
}
------------------------------------------
A variety of other operations are similar and are no longer listed.

HBase two times Java API Development and demo

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.