|
To create a data table in HBase by Java, you first need to import 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 a dependency, we need to create a Configuration object and specify core-site.xml and hbase-site.xml as a resource file.
Configuration config = HBaseConfiguration.create ();
config.addResource (new Path ( "/ etc / hbase / conf / hbase-site.xml"));
config.addResource (new Path ( "/ etc / Hadoop / conf / core-site.xml"));
Also need to set parameters and values hbase.zookeeper.quorum hbase.zookeeper.property.clientPort parameters in the Configuration object, these values can also be found in 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");
After the Configuration object is created, and then create a connection to the Connection object HBase database, and get Admin objects of this object, it is responsible for implementing the creation of data tables:
Connection connection = ConnectionFactory.createConnection (config);
Admin admin = connection.getAdmin ();
Once you have created the Admin object can use the following code to create a data table:
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);
}
Whether there will be serious "users" name of the data table, if the table does not exist will create a new table, column names include: family name, contact information and personal information.
Complete the following procedures:
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) {
CreateTable 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 HTableDescriptor (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 ();
}
}
}
} |
|
|
|