gpt4 book ai didi

java - 创建连接到 HBaseTestingUtility 的 HBaseAdmin

转载 作者:行者123 更新时间:2023-12-01 13:48:59 26 4
gpt4 key购买 nike

我正在编写一段代码来操作存储在 HBase 中的数据。我还想为这段代码编写一个测试。我想在测试中使用 HBaseTestingUtility,因此,在我的 @BeforeClass 中,我创建 HBaseTestingUtility 的新实例并启动迷你集群:

    @BeforeClass
public static void setUpClass() throws Exception {
utility = new HBaseTestingUtility();
utility.startMiniCluster();
}

效果很好。但是,我无法在正在测试的代码中连接到此嵌入式集群。在我的代码中:

Configuration config = HBaseConfiguration.create();

try (HBaseAdmin admin = new HBaseAdmin(config))
{
//code which manipulates the data
}

不幸的是,当创建新的 HBaseAdmin 时,我收到 ConnectionError 异常:

2013-11-21 11:20:35,778 WARN [main-SendThread(0:0:0:0:0:0:0:1:2181)] zookeeper.ClientCnxn (ClientCnxn.java:run(1089)) - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:692) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1068)

当我尝试另一个方法来创建 HBaseAdmin 时:

try (HBaseAdmin admin = new HBaseAdmin(HBaseCacheTest.utility.getConfiguration()))
{
//code which manipulates the data
}

它可以工作(请注意,这里我通过 HBaseCacheTest.utility 访问 HBaseTestingUtility 的实例)。显然这不是一个好方法,因为我不想拥有依赖于测试代码的生产代码。

我认为在这里可行的一种方法是在我的生产类中设置配置并在我的测试中使用此设置方法。但是,我相信应该有另一种方法来连接到使用 HBaseTestingUtility 创建的嵌入式迷你集群。

有什么想法吗?

最佳答案

您不应创建新的 Configuration 对象,而应使用 HBaseTestingUtility 提供的对象:

HBaseAdmin admin = testingUtility.getHBaseAdmin();

此外,如果您只需要配置,则可以使用这个:

testingUtility.getConfiguration()

刚刚在我的 UT 中重新检查了它。我正在使用 HBase 0.96-hadoop2

希望这有帮助:)

关于java - 创建连接到 HBaseTestingUtility 的 HBaseAdmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com