Recent Posts

Wednesday, 15 March 2017

HBase Scan Tutorial


Scaning using HBase Shell
     Using the scan command, you can get the table data. Its syntax is as follows:
scan ‘<table name>’
Example
     The following example shows how to read data from a table using the scan command. Here we are reading the emp table.
hbase> scan 'emp'
ROW                           COLUMN + CELL

1 column = personal data:city, timestamp = 1217521845985, value = vijayawada

1 column = personal data:name, timestamp = 1217521785385, value = ashok

1 column = professional data:designation, timestamp = 121752125687,value = software engineer

1 column = professional data:salary, timestamp = 1217521902983, value = 25000

1 row(s) in 0.0640 seconds
Scanning Using Java API
package com.ashok.hbase;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

public class ScanTable {
   public static void main(String args[]) throws IOException{

      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable table = new HTable(config, "emp");

      // Instantiating the Scan class
      Scan scan = new Scan();

      // Scanning the required columns
      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("city"));

      // Getting the scan result
      ResultScanner scanner = table.getScanner(scan);

      // Reading values from scan result
      for (Result result = scanner.next(); result != null; result = scanner.next())

      System.out.println("Found row : " + result);
      //closing the scanner
      scanner.close();
   }
}

No comments:

Post a Comment