Forum

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

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

    Avikumar
    Participant

    HBase – Create Data:
    Inserting Data using HBase Shell
    This chapter demonstrates how to create data in an HBase table. To create data in an HBase table, the following commands and methods are used:

    put command,

    add() method of Put class, and

    put() method of HTable class.

    Using put command, you can insert rows into a table. Its syntax is as follows:

    put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’

    Inserting the First Row:-
    hbase(main):005:0> put ’emp’,’1′,’personal data:name’,’raju’
    0 row(s) in 0.6600 seconds
    hbase(main):006:0> put ’emp’,’1′,’personal data:city’,’hyderabad’
    0 row(s) in 0.0410 seconds
    hbase(main):007:0> put ’emp’,’1′,’professional
    data:designation’,’manager’
    0 row(s) in 0.0240 seconds
    hbase(main):007:0> put ’emp’,’1′,’professional data:salary’,’50000′
    0 row(s) in 0.0240 seconds

    hbase(main):022:0> scan ’emp’

    Inserting Data Using Java API:-
    You can insert data into Hbase using the add() method of the Put class. You can save it using the put() method of the HTable class. These classes belong to the org.apache.hadoop.hbase.client package. Below given are the steps to create data in a Table of HBase.

    Step 1:Instantiate the Configuration Class
    Configuration conf = HbaseConfiguration.create();
    Step 2:Instantiate the HTable Class
    HTable hTable = new HTable(conf, tableName);
    Step 3: Instantiate the PutClass
    Put p = new Put(Bytes.toBytes(“row1”));
    Step 4: Insert Data
    p.add(Bytes.toBytes(“coloumn family “), Bytes.toBytes(“column
    name”),Bytes.toBytes(“value”));
    Step 5: Save the Data in Table
    hTable.put(p);
    Step 6: Close the HTable Instance
    hTable.close();

    HBase – Update Data:-

    Updating Data using HBase Shell
    .put ‘table name’,’row ’,’Column family:column name’,’new value’
    hbase(main):002:0> put ’emp’,’row1′,’personal:city’,’Delhi’
    0 row(s) in 0.0400 seconds

    Updating Data Using Java API:
    You can update the data in a particular cell using the put() method. Follow the steps given below to update an existing cell value of a table.

    Step 1: Instantiate the Configuration Class
    Configuration conf = HbaseConfiguration.create();
    Step 2: Instantiate the HTable Class
    HTable hTable = new HTable(conf, tableName);
    Step 3: Instantiate the Put Class
    Put p = new Put(Bytes.toBytes(“row1”));
    Step 4: Update an Existing Cell
    p.add(Bytes.toBytes(“coloumn family “), Bytes.toBytes(“column
    name”),Bytes.toBytes(“value”));
    p.add(Bytes.toBytes(“personal”),
    Bytes.toBytes(“city”),Bytes.toBytes(“Delih”));
    Step 5: Save the Data in Table
    hTable.put(p);
    Step 6: Close HTable Instance
    hTable.close();

    HBase – Read Data:
    Reading Data using HBase Shell
    get ’<table name>’,’row1’

    Reading a Specific Column
    Given below is the syntax to read a specific column using the get method.
    hbase> get ‘table name’, ‘rowid’, {COLUMN ⇒ ‘column family:column name ’}
    hbase(main):015:0> get ’emp’, ‘row1’, {COLUMN ⇒ ‘personal:name’}

    1 row(s) in 0.0080 seconds
    Reading Data Using Java API
    To read data from an HBase table, use the get() method of the HTable class. This method requires an instance of the Get class. Follow the steps given below to retrieve data from the HBase table.

    Step 1: Instantiate the Configuration Class
    Configuration conf = HbaseConfiguration.create();
    Step 2: Instantiate the HTable Class
    HTable hTable = new HTable(conf, tableName);
    Step 3: Instantiate the Get Class
    Get get = new Get(toBytes(“row1”));
    Step 4: Read the Data
    get.addFamily(personal)
    To get all the columns from a specific column family, use the following method.
    get.addColumn(personal, name)
    Step 5: Get the Result
    Result result = table.get(g);
    Step 6: Reading Values from the Result Instance
    byte [] value = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“name”));
    byte [] value1 = result.getValue(Bytes.toBytes(“personal”),Bytes.toBytes(“city”));

    HBase – Delete Data:-
    Deleting a Specific Cell in a Table
    Using the delete command, you can delete a specific cell in a table. The syntax of delete command is as follows:

    delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’

    Deleting All Cells in a Table
    Using the “deleteall” command, you can delete all the cells in a row. Given below is the syntax of deleteall command.

    deleteall ‘<table name>’, ‘<row>’,

    Deleting Data Using Java API:-
    You can delete data from an HBase table using the delete() method of the HTable class. Follow the steps given below to delete data from a table.
    Step 1: Instantiate the Configuration Class
    Configuration conf = HbaseConfiguration.create();
    Step 2: Instantiate the HTable Class
    HTable hTable = new HTable(conf, tableName);
    Step 3: Instantiate the Delete Class
    Delete delete = new Delete(toBytes(“row1”));
    Step 4: Select the Data to be Deleted
    delete.deleteColumn(Bytes.toBytes(“personal”), Bytes.toBytes(“name”));
    delete.deleteFamily(Bytes.toBytes(“professional”));
    Step 5: Delete the Data
    table.delete(delete);
    Step 6: Close the HTableInstance
    table.close();

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




cf22

Your Name (required)

Your Email (required)

Subject

Phone No

Your Message

Cart

  • No products in the cart.