This article takes HBase 0.90.2 as an example of how to use the Java language for HBase client programming in Windows systems, Eclipse IDE integration environments, including building tables, deleting tables, inserting records, deleting records, querying in various ways, and so on.
1. Preparatory work
1, download after the installation of JDK package (here is the use of jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008);
2, download Eclipse, extract to the local (here is the use of eclipse-java-helios-sr2-win32);
3, download HBase package, unzip the installation package to the local (here is the use of hbase-0.90.2).
2. Build the development environment
1. Run eclipse, create a new Java project "Hbaseclient", right-click the project root directory, select "Properties"-> "Java build Path"-> "Library"-> "Add External JARs ", add all jar packages under Hbase-0.90.2.jar, Hbase-0.90.2-tests.jar, and Lib subdirectories under the HBase root directory to the classpath of this project.
2, follow the actions in step 1 to add the HBase profile hbase-site.xml that you are connecting to the classpath of this project, as shown below as an example of a configuration file:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs:// hostname:9000/hbase</value>
</property>
<property>
<name> hbase.cluster.distributed</name>
<value>true</value>
</property>
< property>
<name>hbase.zookeeper.quorum</name>
<value>*.*.*.*, *.*.*.*, *.*.*.* </value>
</property>
<property skipindoc= "true" >
<name> hbase.defaults.for.version</name>
<value>0.90.2</value>
</property>
</configuration>
3, the following can be in the eclipse environment for hbase programming.
3. HBase Basic Operation code Example
3.1 Initialization configuration
privatestatic Configuration conf =null;
/**
* Initialization configuration *
/static {
conf = hbaseconfiguration.create ();
}
3.2 Creating a Table
/**
* CREATE TABLE Operation
* @throws ioexception
/publicvoid createtable (String tablename, string[] cfs) 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 < cfs.length; i++) {
tabledesc.addfamily (new Hcolumndescriptor (cfs[i));
}
Admin.createtable (TABLEDESC);
SYSTEM.OUT.PRINTLN (the table was created successfully!) ");
}
}