Java creates tables in the HBase database Author: Chszs, All rights reserved, without the consent, may not reprint. Bo main home: Http://blog.csdn.net/chszs
To create a datasheet in HBase through Java, you first need to import the Hbase-client.jar driver package. You can add dependencies in the project pom.xml configuration file:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client< artifactid>
<version>1.1.0.1</version>
<dependency>
After adding dependencies, we need to create configuration objects and specify Core-site.xml and Hbase-site.xml as resource files.
Configuration config = hbaseconfiguration.create ();
Config.addresource (New Path ("/etc/hbase/conf/hbase-site.xml"));
Config.addresource (New Path ("/etc/hadoop/conf/core-site.xml"));
You also need to set the values of the Hbase.zookeeper.quorum and Hbase.zookeeper.property.clientPort parameters in the configuration object, which can also be found in the Hbase-site.xml configuration file:
Configuration config = hbaseconfiguration.create ();
Config.set ("Hbase.zookeeper.quorum", "127.0.0.1");
Config.set ("Hbase.zookeeper.property.clientPort", "2181");
When the configuration object is created, it then creates a connection object that connects to the HBase database and obtains the Admin object from this object, which is responsible for implementing the operation of creating the data table:
Connection Connection = connectionfactory.createconnection (config);
Admin admin = connection.getadmin ();
Once you have created the Admin object, you can create a datasheet from the following code:
String tablename = "Users";
if (!admin.istableavailable (tablename.valueof (tablename)) {
Htabledescriptor hbasetable = new HTableDescriptor ( Tablename.valueof (tablename));
Hbasetable.addfamily (New Hcolumndescriptor ("name"));
Hbasetable.addfamily (New Hcolumndescriptor ("Contact_info"));
Hbasetable.addfamily (New Hcolumndescriptor ("Personal_info"));
Admin.createtable (hbasetable);
}
A data table with the "users" name seriously exists, and a new table is created if the table does not exist, including family name, contact information, and personal information.
The complete procedure is as follows:
Package Com.wordpress.khodeprasad;
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.TableName;
Import Org.apache.hadoop.hbase.client.Admin;
Import org.apache.hadoop.hbase.client.Connection;
Import Org.apache.hadoop.hbase.client.ConnectionFactory; /** * @author Prasad khode * * */public class CreateTable {public static void main (string[] args) {Createtab
Le object = new CreateTable ();
Object.createtable ();
public void CreateTable () {Configuration config = hbaseconfiguration.create ();
Config.set ("Hbase.zookeeper.quorum", "127.0.0.1");
Config.set ("Hbase.zookeeper.property.clientPort", "2181");
Connection Connection = null;
admin admin = null;
try {connection = connectionfactory.createconnection (config); Admin = Connection.getadmin ();
String tablename = "Users"; if (!admin.istableavailable (tablename.valueof (tablename)) {Htabledescriptor hbasetable = new HTABLEDESCR
Iptor (tablename.valueof (tablename));
Hbasetable.addfamily (New Hcolumndescriptor ("name"));
Hbasetable.addfamily (New Hcolumndescriptor ("Contact_info"));
Hbasetable.addfamily (New Hcolumndescriptor ("Personal_info"));
Admin.createtable (hbasetable);
} catch (Exception e) {e.printstacktrace ();
Finally {try {if (admin!= null) {admin.close ();
} if (connection!= null &&!connection.isclosed ()) {connection.close ();
} catch (Exception E2) {e2.printstacktrace ();
}
}
}
}