Java creates tables in the HBase database

Source: Internet
Author: User

Java in HBase Database creation table Chszs, All rights reserved, without consent, not reproduced. Blogger Home: Http://blog.csdn.net/chszs

To create a data table in HBase through Java, you first need to import the Hbase-client.jar driver package. Dependencies can be added 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 the dependency, we need to create the configuration object and specify 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 config 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 connected to the HBase database and obtains the Admin object through this object, which is responsible for 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 data table 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 is severely present, and if the table does not exist, a new table is created, including: family name, contact information, and personal information.

The

Complete program 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 V        OID 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 HTABLEDESCRI                Ptor (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 (); }        }    }}

Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.

Java creates tables in the HBase database

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.