溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ES插件開發(fā)之--如何添加自己的動(dòng)態(tài)設(shè)置項(xiàng)

發(fā)布時(shí)間:2020-08-02 02:09:02 來源:網(wǎng)絡(luò) 閱讀:2298 作者:sbp810050504 欄目:大數(shù)據(jù)

ES中,有一類參數(shù)是可以動(dòng)態(tài)調(diào)整的,比如副本數(shù)量: number_of_replicas。
在插件開發(fā)中,如何添加自己的自定義參數(shù)呢?
在插件的入口,添加onModule(ClusterModule module)即可。

public class ShgyPlugin extends Plugin {
    @Override
    public String name() {
        return "shgy-plugin";
    }

    @Override
    public String description() {
        return "shgy plugin ";
    }

    public void onModule(ClusterModule module){

        module.registerIndexDynamicSetting("index.custom_setting", new Validator() {
            @Override
            public String validate(String setting, String value, ClusterState clusterState) {
                if (value == null) {
                    throw new NullPointerException("value must not be null");
                }
                return null;
            }
        });
    }
}

編譯代碼,安裝插件后,使用如下的腳本測(cè)試:

curl -X PUT "localhost:9200/twitter/_settings" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "custom_setting" : 2
    }
}'

curl -XGET 'http://localhost:9200/twitter/_settings?pretty'

在代碼中使用參數(shù),一般是在TransportAction中使用, 代碼片段如下:

  ClusterState clusterState = clusterService.state();
  clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);

  String concreteSingleIndex = indexNameExpressionResolver.concreteSingleIndex(clusterState, request);

  IndexMetaData indexMeta = clusterState.getMetaData().index(concreteSingleIndex);
  int sectionCnt = indexMeta.getSettings().getAsInt("index.custom_settings",-1);

即通過clusterService獲取到clusterState, 然后獲取到IndexMetaData, 然后獲取到Settings。

自定義動(dòng)態(tài)參數(shù), 配合templates的使用,就不需要頻繁手動(dòng)創(chuàng)建索引了。 這個(gè)知識(shí)點(diǎn)應(yīng)該歸納到 ES插件開發(fā)的一部分。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI