您好,登錄后才能下訂單哦!
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ā)的一部分。
免責(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)容。