Scan(Read) a Hbase Table data using Java

Category : Hadoop | Sub Category : Apache HBase | By Prasad Bonam Last updated: 2020-10-17 15:47:29 Viewed : 472


Scan(Read) a Hbase Table  data using Java

1.        Create a maven project


Dependency : add the below dependency in pom.xml

 

  <!--
https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->

                    <dependency>

                              <groupId>org.apache.hbase</groupId>

                              <artifactId>hbase-client</artifactId>

                              <version>1.2.0</version>

                    </dependency>

  2.       Scan a Hbase Table  :

Table Name: ReadHBaseData.java
 package com.runnerdev;

 

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.HTable;

 import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.util.Bytes;

import org.apache.hadoop.hbase.client.Result;

 

/**

 * Delete data from Hbase in java *

  

 */

public class ReadHBaseData {

 

          static String hbase_host = "10.19.85.110";

          static String hbase_Port = "2191";

 

          public static void main(String[] args) {

                    try {

                              Configuration conf = HBaseConfiguration.create();

                              conf.set("hbase.zookeeper.quorum", hbase_host);

                              conf.set("hbase.zookeeper.clientPort", hbase_Port);

                              System.out.println("Conf::" + conf);

                              HTable hTable = new HTable(conf, "Employee");// Instantiating HTable class

//                           Put p = new Put(Bytes.toBytes("9"));                      

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

                              scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"));// Scan the required columns

                              scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("location"));

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

                              // Reading values from scan result

                              for (Result result = scanner.next(); result != null; result = scanner.next())

                                        System.out.println("Reuslt data : " + result);

                              scanner.close();// closing the scanner

 

                    } catch (IOException e) {

                              e.printStackTrace();

                    }

 

          }

 

} 

 Run above program as follows:


Search
Related Articles

Leave a Comment: