了解hbase java客户端

kknvjkwl  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(376)

几天前我开始使用hbase,浏览了所有的在线资料。
我已经安装和配置了hbase和shell命令,运行正常。
我得到了一个java客户机的例子,从hbase表中获取数据,它成功地执行了,但我不明白它是如何工作的?在代码中我们没有提到端口,主机的hbase服务器?它如何从表中提取数据?
这是我的密码:

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

hfwmuf9z

hfwmuf9z1#

如果你看源代码 HBaseConfiguration 在github上,您可以看到它调用 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.xml 以及 hbase-site.xml .

2ul0zpep

2ul0zpep2#

我同意二元书呆子的回答
添加一些更有趣的信息以便更好地理解。
你的问题是:
我不明白它是怎么工作的?在代码中我们没有提到端口,主机的hbase服务器?它如何从表中提取数据?
因为您在集群中执行这个程序

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

希望这能让你明白。

相关问题