了解 HBase Java 客户端

Understanding HBase Java Client

几天前我开始使用 Hbase 并浏览了所有 material 在线内容。

我已经安装并配置了 HBase,shell 命令工作正常。

我得到了一个 Java 客户端从 HBase Table 获取数据的例子,它成功执行了,但我不明白它是如何工作的?在代码中我们没有提到Hbase服务器的端口,主机?它如何能够从 table?

获取数据

这是我的代码:

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);         
  }

}

输出如下:

Output:
name: raju city: hyderabad

如果您在 github 上查看 HBaseConfiguration 的源代码,您可以看到它在调用 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);
  }

其次是:

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

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

因此它从您的 HBase 配置文件 hbase-default.xmlhbase-site.xml.

加载配置

我同意 Binary Nerds 的回答

添加一些更有趣的信息以便更好地理解。

您的问题:

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?

因为你在集群中执行这个程序

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

所有集群属性都将在集群内部进行处理..因为你在集群中并且你正在执行 hbase java 客户端程序..

现在像下面这样尝试(在 windows 上以不同的方式从远程计算机 eclipse 执行相同的程序,以找出您之前和现在所做的不同之处)。

  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");
         }

希望这能帮助您理解。