在 CDP 上使用 HBase Java 客户端通过 Knox 连接 HBase

问题描述

我需要使用 HBase Java 客户端通过 Knox 连接到 HBase。我有以下诺克斯详细信息

Knox_Url: https://knox-host:port/gateway/cdp-proxy-api/hbase
Username: knox_user_name
Password: knox_password

使用以下代码,我可以添加 URL,但无法添加凭据。

URL url = new URL(Knox_Url);
Configuration conf = HBaseConfiguration.create();
conf.addResource(URL);

Connection con = ConnectionFactory.createConnection(conf);

我看过其他 StackOverflow 问题,但他们都提到了要在配置中设置的以下属性

 public void setUp() throws IOException {
        config = HBaseConfiguration.create();
        config.set("zookeeper.znode.parent","/hbase-unsecure");
        config.set("hbase.zookeeper.quorum",ZOOKEEPER_QUORUM);
        config.set("hbase.zookeeper.property.clientPort","2181");
        config.set("hbase.cluster.distributed","true");
        connection = ConnectionFactory.createConnection(config);
    }

我的问题是有没有办法使用 Knox 网关详细信息连接到 HBase 并检索数据?

解决方法

我们可以使用 RestTemplate 连接到 HBase。

配置

public RestTemplate create(){
    return new RestTemplateBuilder()
              .basicAuthentication(user,password)
              .setConnectTimeout(Duration.ofSeconds(60))
              .setReadTimeout(Duration.ofSeconds(60))
              .build();
}

使用

String url = "https://host:port/gateway/cdp-proxy-api/hbase";
String response = config.create().getForEntity(url,String.class).getBody();