Forum

This topic contains 0 replies, has 1 voice, and was last updated by  Avikumar 2 years, 3 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1275 Reply

    Avikumar
    Participant

    HBase – Create Table:

    Creating a Table using HBase Shell
    You can create a table using the create command, here you must specify the table name and the Column Family name. The syntax to create a table in HBase shell is shown below.

    create ‘<table name>’,’<column family>’

    You can create this table in HBase shell as shown below.

    hbase(main):002:0> create ’emp’, ‘personal data’, ’professional data’
    And it will give you the following output.

    0 row(s) in 1.1300 seconds
    => Hbase::Table – emp
    Verification
    You can verify whether the table is created using the list command as shown below. Here you can observe the created emp table.

    hbase(main):002:0> list
    TABLE
    emp
    2 row(s) in 0.0340 seconds

    Creating a Table Using java API:-

    You can create a table in HBase using the createTable() method of HBaseAdmin class. This class belongs to the org.apache.hadoop.hbase.client package. Given below are the steps to create a table in HBase using java API.

    Configuration conf = HBaseConfiguration.create();
    HBaseAdmin admin = new HBaseAdmin(conf);

    Step2: Create TableDescriptor

    //creating table descriptor
    HTableDescriptor table = new HTableDescriptor(toBytes(“Table name”));

    //creating column family descriptor
    HColumnDescriptor family = new HColumnDescriptor(toBytes(“column family”));

    //adding coloumn family to HTable
    table.addFamily(family);

    Step 3: Execute through Admin

    admin.createTable(table);

    Listing a Table using HBase Shell:

    hbase(main):001:0 > list
    hbase(main):001:0> list
    TABLE
    emp
    Here you can observe a table named emp.

    Listing Tables Using Java API
    Follow the steps given below to get the list of tables from HBase using java API.

    Step 1

    //creating a configuration object
    Configuration conf = HBaseConfiguration.create();

    //Creating HBaseAdmin object
    HBaseAdmin admin = new HBaseAdmin(conf);

    //Getting all the list of tables using HBaseAdmin object
    HTableDescriptor[] tableDescriptor = admin.listTables();
    Step 2
    You can get the length of the HTableDescriptor[] array using the length variable of the HTableDescriptor class. Get the name of the tables from this object using getNameAsString() method. Run the ‘for’ loop using these and get the list of the tables in HBase.

    HBase – Disabling a Table:

    Disabling a Table using HBase Shell
    To delete a table or change its settings, you need to first disable the table using the disable command. You can re-enable it using the enable command.

    Given below is the syntax to disable a table:

    disable ‘emp’
    Example
    Given below is an example that shows how to disable a table.

    hbase(main):025:0> disable ’emp’
    0 row(s) in 1.2760 seconds
    Verification
    After disabling the table, you can still sense its existence through list and exists commands. You cannot scan it. It will give you the following error.

    hbase(main):028:0> scan ’emp’
    ROW COLUMN + CELL
    ERROR: emp is disabled.
    is_disabled
    This command is used to find whether a table is disabled. Its syntax is as follows.

    hbase> is_disabled ‘table name’
    The following example verifies whether the table named emp is disabled. If it is disabled, it will return true and if not, it will return false.

    hbase(main):031:0> is_disabled ’emp’
    true
    0 row(s) in 0.0440 seconds
    disable_all

    hbase> disable_all ‘r.*’
    hbase(main):002:07> disable_all ‘raj.*’
    raja
    rajani
    rajendra
    rajesh
    raju
    Disable the above 5 tables (y/n)?
    y
    5 tables successfully disabled

    Disable a Table Using Java API:

    Step 1
    Instantiate HBaseAdmin class as shown below.

    // Creating configuration object
    Configuration conf = HBaseConfiguration.create();

    // Creating HBaseAdmin object
    HBaseAdmin admin = new HBaseAdmin(conf);

    Step 2
    Verify whether the table is disabled using isTableDisabled() method as shown below.

    Boolean b = admin.isTableDisabled(“emp”);

    Step 3
    If the table is not disabled, disable it as shown below.

    if(!b){
    admin.disableTable(“emp”);
    System.out.println(“Table disabled”);
    }

    HBase – Enabling a Table:

    Enabling a Table using HBase Shell
    Syntax to enable a table:

    enable ‘emp’
    Example
    Given below is an example to enable a table.

    hbase(main):005:0> enable ’emp’
    0 row(s) in 0.4580 seconds

    is_enabled
    This command is used to find whether a table is enabled. Its syntax is as follows:

    hbase> is_enabled ‘table name’
    The following code verifies whether the table named emp is enabled. If it is enabled, it will return true and if not, it will return false.

    hbase(main):031:0> is_enabled ’emp’
    true
    0 row(s) in 0.0440 seconds

    Enable a Table Using Java API

    Step1
    Instantiate HBaseAdmin class as shown below.

    // Creating configuration object
    Configuration conf = HBaseConfiguration.create();

    // Creating HBaseAdmin object
    HBaseAdmin admin = new HBaseAdmin(conf);

    Step 2
    Verify whether the table is enabled using isTableEnabled() method as shown below.

    Boolean bool = admin.isTableEnabled(“emp”);

    Step 3
    If the table is not disabled, disable it as shown below.

    if(!bool){
    admin.enableTable(“emp”);
    System.out.println(“Table enabled”);
    }

    HBase – Describe & Alter:-

    describe
    This command returns the description of the table. Its syntax is as follows:

    hbase> describe ‘table name’
    Given below is the output of the describe command on the emp table.

    hbase(main):006:0> describe ’emp’
    DESCRIPTION
    ENABLED

    ’emp’, {NAME ⇒ ‘READONLY’, DATA_BLOCK_ENCODING ⇒ ‘NONE’, BLOOMFILTER
    ⇒ ‘ROW’, REPLICATION_SCOPE ⇒ ‘0’, COMPRESSION ⇒ ‘NONE’, VERSIONS ⇒
    ‘1’, TTL true

    alter:-
    Alter is the command used to make changes to an existing table. Using this command, you can change the maximum number of cells of a column family, set and delete table scope operators, and delete a column family from a table.

    hbase> alter ‘t1’, NAME ⇒ ‘f1′, VERSIONS ⇒ 5
    In the following example, the maximum number of cells is set to 5.

    hbase(main):003:0> alter ’emp’, NAME ⇒ ‘personal data’, VERSIONS ⇒ 5
    Updating all regions with the new schema…
    0/1 regions updated.
    1/1 regions updated.
    Done.
    0 row(s) in 2.3050 seconds

    Setting Read Only

    hbase>alter ‘t1′, READONLY(option)

    hbase(main):006:0> alter ’emp’, READONLY
    Updating all regions with the new schema…
    0/1 regions updated.
    1/1 regions updated.
    Done.
    0 row(s) in 2.2140 seconds

    Removing Table Scope Operators

    hbase> alter ‘t1’, METHOD ⇒ ‘table_att_unset’, NAME ⇒ ‘MAX_FILESIZE’
    Deleting a Column Family

    hbase> alter ‘ table name ’, ‘delete’ ⇒ ‘ column family ’

    Adding a Column Family Using Java API:-

    You can add a column family to a table using the method addColumn() of HBAseAdmin class. Follow the steps given below to add a column family to a table.

    Step 1
    Instantiate the HBaseAdmin class.

    // Instantiating configuration object
    Configuration conf = HBaseConfiguration.create();

    // Instantiating HBaseAdmin class
    HBaseAdmin admin = new HBaseAdmin(conf);

    Step 2

    // Instantiating columnDescriptor object

    HColumnDescriptor columnDescriptor = new
    HColumnDescriptor(“contactDetails”);

    Step 3

    // Adding column family
    admin.addColumn(“employee”, new HColumnDescriptor(“columnDescriptor”));

Viewing 1 post (of 1 total)
Reply To: HBase – Create Table
Your information:




cf22

Your Name (required)

Your Email (required)

Subject

Phone No

Your Message

Cart

  • No products in the cart.