Search code examples
hadoopmapreducehbasehortonworks-data-platformbigdata

Understanding HBase Java Client


I started Hbase few days back and going through all the material of online.

I have installed and configured HBase and shell commands are working fine.

I got an example of Java client to get data from HBase Table and it executed successfully but I could not understand how it is working? In the code nowhere we have mentioned the port, host of Hbase server? How it able to fetch the data from table?

This is my code:

public class RetriveData {

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

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

      // Instantiating HTable class
      @SuppressWarnings({ "deprecation", "resource" })
      HTable table = new HTable(config, "emp");

      // Instantiating Get class
      Get g = new Get(Bytes.toBytes("1"));

      // Reading the data
      Result result = table.get(g);

      // Reading values from Result class object
      byte [] value = result.getValue(Bytes.toBytes("personal data"),Bytes.toBytes("name"));

      byte [] value1 = result.getValue(Bytes.toBytes("personal data"),Bytes.toBytes("city"));

      // Printing the values
      String name = Bytes.toString(value);
      String city = Bytes.toString(value1);

      System.out.println("name: " + name + " city: " + city);         
  }

}

The output looks like:

Output:
name: raju city: hyderabad


Solution

  • I agree with Binary Nerds answer

    adding some more interesting information for better understanding.

    Your Question :

    I could not understand how it is working? In the code nowhere we have mentioned the port, host of Hbase server? How it able to fetch the data from table?

    Since you are executing this program in cluster

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

    all the cluster properties will be taken care from inside the cluster.. since you are in cluster and you are executing hbase java client program..

    Now try like below (execute same program in different way from remote machine eclipse on windows to find out difference of what you have done earlier and now).

      public static Configuration configuration; // this is class variable
            static { //fill clusternode1,clusternode2,clusternode3 from your cluster 
                configuration = HBaseConfiguration.create();
                 configuration.set("hbase.zookeeper.property.clientPort", "2181");
                 configuration.set("hbase.zookeeper.quorum",
                 "clusternode1,clusternode2,clusternode3");
                 configuration.set("hbase.master", "clusternode1:600000");
             }
    

    Hope this heps you to understand.