zkCli自动补全

栏目: 服务器 · 发布时间: 7年前

内容简介:zkCli自动补全

ZooKeeper 压缩包中提供了 bin/zkCli.sh ,可以连接 ZooKeeper 进行操作,比较方便的是,无论是对 命令 还是 路径zkCli 都能够进行自动补全。说到自动补全的库,在 tairclient 甘大神 使用 GNU Readline 实现了命令补全和历史命令记录。

原理

zkCli 中使用的自动补全的库是 JLineJLine官网 描述中有这样一句话:

JLine is a Java library for handling console input. It is similar in functionality to BSD editline and GNU readline.

zkCli 中对应的代码在 src/java/main/org/apache/zookeeper/JLineZNodeCompletor.java 中,可以看到这个类实现了 JLinejline.Completor 这个接口。

jline.Completor 这个接口中只定义了一个方法:

int complete(String var1, int var2, List var3);

实现类 JLineZNodeCompletor 中,对应的方法实现为:

public int complete(String buffer, int cursor, List candidates) {
    // Guarantee that the final token is the one we're expanding
    buffer = buffer.substring(0,cursor);
    String token = "";
    if (!buffer.endsWith(" ")) {
        String[] tokens = buffer.split(" ");
        if (tokens.length != 0) {
            token = tokens[tokens.length-1] ;
        }
    }

    if (token.startsWith("/")){
        return completeZNode( buffer, token, candidates);
    }
    return completeCommand(buffer, token, candidates);
}

可以看到,如果需要补全的内容是以 / 开头,就补全为 ZNode 的路径;否则,就补全为 zkCli 的命令。

补全 ZNode 的路径的时候, zkCli 直接调用 getChildren 方法获取对应 ZNode 的子节点,然后用 String.startsWith 去过滤。


以上所述就是小编给大家介绍的《zkCli自动补全》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Bandit Algorithms for Website Optimization

Bandit Algorithms for Website Optimization

John Myles White / O'Reilly Media / 2013-1-3 / USD 19.99

This book shows you how to run experiments on your website using A/B testing - and then takes you a huge step further by introducing you to bandit algorithms for website optimization. Author John Myle......一起来看看 《Bandit Algorithms for Website Optimization》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具