Santhosh Kumar N
Santhosh Kumar N

Reputation: 43

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

Upvotes: 0

Views: 261

Answers (2)

Ram Ghadiyaram
Ram Ghadiyaram

Reputation: 29237

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.

Upvotes: 0

Binary Nerd
Binary Nerd

Reputation: 13937

If you look at the source code for HBaseConfiguration on github you can see what it does when it calls create().

public static Configuration create() {
    Configuration conf = new Configuration();
    // In case HBaseConfiguration is loaded from a different classloader than
    // Configuration, conf needs to be set with appropriate class loader to resolve
    // HBase resources.
    conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
    return addHbaseResources(conf);
  }

Followed by:

public static Configuration addHbaseResources(Configuration conf) {
    conf.addResource("hbase-default.xml");
    conf.addResource("hbase-site.xml");

    checkDefaultsVersion(conf);
    HeapMemorySizeUtil.checkForClusterFreeMemoryLimit(conf);
    return conf;
  }

So its loading the configuration from your HBase configuration files hbase-default.xml and hbase-site.xml.

Upvotes: 1

Related Questions