Java creates a table in the HBase Database

Source: Internet
Author: User
To create a data table 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:

To create a data table 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:

To create a data table 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:

Org. apache. hbase Hbase-client 1.1.0.1

After adding dependencies, we need to create the Configuration object and specify the core-site.xml and hbase-site.xml as the 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 "));

You also need to set the values of the hbase. zookeeper. quorum parameter and the hbase. zookeeper. property. clientPort parameter 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 ");

After the Configuration object is created, create a Connection object connected to the HBase database and obtain the Admin object through this object. It is responsible for creating data tables:

Connection connection = ConnectionFactory. createConnection (config); Admin admin Admin = connection. getAdmin ();

Once an Admin object is created, you 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 a data table with the "users" name exists. If the table does not exist, a new table is created. The column name includes the 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. conn EctionFactory;/*** @ 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 = nu Ll; 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 ();}}}}

Hadoop + HBase cloud storage creation summary PDF

Regionserver startup failed due to inconsistent time between HBase nodes

Hadoop + ZooKeeper + HBase cluster configuration

Hadoop cluster Installation & HBase lab environment setup

HBase cluster configuration based on Hadoop cluster'

Hadoop installation and deployment notes-HBase full distribution mode installation

Detailed tutorial on creating HBase environment for standalone Edition

HBase details: click here
HBase: click here

This article permanently updates the link address:

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.