Last blog said how to build a HBase environment, today to talk about how to use the HBase client API to manipulate the data in HBase. Create a project
First create a Maven project, and then add the dependencies for the HBase client API, as follows:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactid>hbase-client</ artifactid>
<version>2.0.0-alpha4</version>
</dependency>
Add configuration file
Add the Hbase-site.xml file in the resources directory of the Maven project, the HBase client API defaults to the system classpath to find the file to load the connection information.
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:// /apps/hbase-2.0.0-beta-1/data/hbase</value>
</property>
<property>
<name> Hbase.zookeeper.property.datadir</name>
<value>/apps/hbase-2.0.0-beta-1/data/zookeeper</ value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>bd1,bd2,bd3</value>
</property>
<property>
<name> hbase.cluster.distributed</name>
<value>true</value>
</property>
</ Configuration>
Creating Connection Objects
To manipulate HBase data tables, and similar to JDBC programming, you need to create a HBase Connection object and then manipulate it through this Connection object, which you need to close after the operation completes.
Initializes the Configuration object
Configuration config = hbaseconfiguration.create () According to the Hbase-site.xml file;
Initializes the Connection object
Connection Connection = connectionfactory.createconnection (config) according to the Configuration object;
Operation data
//...
Connection.close ();
Table Management
In HBase, to manage tables, you need to implement them through the Org.apache.hadoop.hbase.client.Admin class, and you can get an Admin object instance through the Connection.getadmin () method. View Table
In HBase, the table's descriptive information is stored in the Org.apache.hadoop.hbase.client.TableDescriptor class, so we can get HBase by Admin.listtabledescriptors () Describes information for all tables in. You can then view or modify the definition of a table by using some of the methods of the Org.apache.hadoop.hbase.client.TableDescriptor class. Finally, remember to call Admin.close () to close the operation.
private void Listtable () throws IOException {
Admin admin = connection.getadmin ();
try {
list<tabledescriptor> tabledescriptors = admin.listtabledescriptors ();
for (Tabledescriptor tabledescriptor:tabledescriptors) {
tablename tablename = Tabledescriptor.gettablename ();
System.out.println ("Table:" + tablename);
System.out.println ("\texists:" + admin.tableexists (tablename));
System.out.println ("\tenabled:" + admin.istableenabled (tablename));
}
finally {
admin.close ();
}
}
Create a table
Creating tables is done primarily through the tabledescriptor and Columnfamilydescriptor classes, and the Tabledescriptor and Columnfamilydescriptor objects need to be created using the Tabledescriptorbuilder.newbuilder (...) and Columnfamilydescriptorbuilder.newbuilder (...) methods to create. The following code shows how to create a user table that contains base and address two column families.
private void CreateTable () throws IOException {
Admin admin = connection.getadmin ();
try {
Tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder (tablename.valueof ("user"))
. Addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (bytes.tobytes ("base")). Build ())
. addcolumnfamily ( Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes ("Address")). Build ()
.
Admin.createtable (TABLEDESC);
} finally {
admin.close ();
}
}
Delete Table
The deletion of a table in HBase needs to be implemented through the admin.disabletable () and Admin.deletetable () methods, as follows:
private void Deletetable () throws IOException {
Admin admin = connection.getadmin ();
try {
admin.disabletable (tablename.valueof ("user"));
Admin.deletetable (tablename.valueof ("user"));
} finally {
admin.close ();
}
}
Complete Code
Finally, take a look at the complete example code for the table management operation in HBase.
Package my.hbasestudy;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.TableName;
Import org.apache.hadoop.hbase.client.*;
Import org.apache.hadoop.hbase.util.Bytes;
Import java.io.IOException;
Import Java.util.Iterator;
Import java.util.List;
public class Testapi {private static final String table_name = "user";
private static final String column_family_base = "BASE";
private static final String column_family_address = "Address";
private static final String Column_username = "USERNAME";
private static final String Column_password = "PASSWORD";
private static final String Column_home = "Home";
private static final String Column_office = "OFFICE";
Private Connection Connection;
public static void Main (string[] args) throws Exception {Configuration config = hbaseconfiguration.create (); Connection Connection = connectionfactory.createconnection (config);
Testapi t = new Testapi (connection);
T.listtable ();
T.createtable ();
T.listtable ();
T.deletetable ();
Connection.close ();
Public Testapi (Connection Connection) {this.connection = Connection;
private void Listtable () throws IOException {Admin admin = connection.getadmin ();
try {list<tabledescriptor> tabledescriptors = admin.listtabledescriptors (); for (Tabledescriptor tabledescriptor:tabledescriptors) {tablename TableName = Tabledescriptor.gettablen
Ame ();
System.out.println ("Table:" + tablename);
System.out.println ("\texists:" + admin.tableexists (tablename));
System.out.println ("\tenabled:" + admin.istableenabled (tablename));
finally {admin.close (); } private void CreateTable () throws IOException {Admin admin = connection. Getadmin ();
try {tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder (tablename.valueof (table_name))
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_base)). Build ())
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_address)). Build ())
. build ();
Admin.createtable (TABLEDESC);
finally {admin.close ();
} private void Deletetable () throws IOException {Admin admin = connection.getadmin ();
try {admin.disabletable (tablename.valueof (table_name));
Admin.deletetable (TABLENAME.VALUEOF (table_name));
finally {admin.close ();
}
}
}