溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

elasticsearch的jvm插件怎么使用

發(fā)布時間:2021-12-16 10:09:12 來源:億速云 閱讀:148 作者:iii 欄目:云計算

本篇內容介紹了“elasticsearch的jvm插件怎么使用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

    elasticsearch  的 jvm插件要實現(xiàn)Plugin接口,或者繼承子AbstractPlugin抽象類??梢詫崿F(xiàn) Module 和 Services 兩種組件,它們分別有 3個 生命周期 global , index ,shard 。

整體的項目結構是這樣的。

elasticsearch的jvm插件怎么使用

我們這里繼承子AbstractPlugin ,來增加我們的 TranslogRestModule 模塊

package org.elasticsearch.plugin.translog;
import java.util.Collection;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.plugins.AbstractPlugin;


public class TranslogRestPlugin extends AbstractPlugin{
    public String name() {
        return "translog-rest";
    }

    @Override
    public String description() {
        return "view translog";
    }

    @Override
    public Collection<Class<? extends Module>> modules() {
        Collection<Class<? extends Module>> modules = Lists.newArrayList();
        modules.add(TranslogRestModule.class);
        return modules;
    }
}

TranslogRestModule類 ,實例化我們的 TranslogRestHandler

package org.elasticsearch.plugin.translog;
import org.elasticsearch.common.inject.AbstractModule;

public class TranslogRestModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(TranslogRestHandler.class).asEagerSingleton();
	}

}

TranslogRestHandler是我們最主要的一個類 , 他負責處理 /_translog 這個REST請求,通過 TranslogStreams.readTranslogOperation 方法讀取 Translog 然后用 XContentBuilder 構造 JSON格式 ,返回給客戶端。

package org.elasticsearch.plugin.translog;
import java.io.FileInputStream;
import java.io.IOException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.translog.Translog;
import org.elasticsearch.index.translog.TranslogStreams;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;

public class TranslogRestHandler implements  RestHandler{
	private XContentBuilder builder;

	@Inject
    public TranslogRestHandler(RestController restController) {
        restController.registerHandler(GET, "/_translog", this);
    }
	public void buildSave(Translog.Index op){
		try {
			builder.startObject()
			.field("opType").value("save")
			.field("id").value(op.id())
			.field("type").value(op.type())
			.field("version").value(op.version())
			.field("routing").value(op.routing())
			.field("parent").value(op.parent())
			.field("timestamp").value(op.timestamp())
			.field("ttl").value(op.ttl())
			.field("version").value(op.version())
			.field("source").value(op.source().toUtf8())
			.endObject();  
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void buildCreate(Translog.Create op){
		try {
			builder.startObject()
			.field("opType").value("create")
			.field("id").value(op.id())
			.field("type").value(op.type())
			.field("version").value(op.version())
			.field("routing").value(op.routing())
			.field("parent").value(op.parent())
			.field("timestamp").value(op.timestamp())
			.field("ttl").value(op.ttl())
			.field("version").value(op.version())
			.field("source").value(op.source().toUtf8())
			.endObject();  
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void buildDelete(Translog.Delete op){
		try {
			builder.startObject()
			.field("opType").value("delete")
			.field("id").value(op.uid().text())
			.field("version").value(op.version())
			.endObject();  
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void buildDeleteByQuery(Translog.DeleteByQuery op){
		try {
			builder.startObject()
			.field("opType").value("deleteByQuery")
			.field("source").value(op.source().toUtf8())
			.endObject();  
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public XContentBuilder buildTranslog(String filePath,int size){
		 FileInputStream fs = null;
		 int count = 0;
	        try {
	            fs = new FileInputStream(filePath);
	            InputStreamStreamInput si = new InputStreamStreamInput(fs);
	            while (true) {
	            	if(count>=size){
	            		break;
	            	}
	                Translog.Operation operation;
	                try {
	                    int opSize = si.readInt();
//	                    System.out.println("opSize = "+opSize);
	                    operation = TranslogStreams.readTranslogOperation(si);
	                    switch (operation.opType()) {
                        case CREATE:
                            Translog.Create create = (Translog.Create) operation;
                            buildCreate(create);
                            break;
                        case SAVE:
                            Translog.Index index = (Translog.Index) operation;
                            buildSave(index);
                            break;
                        case DELETE:
                            Translog.Delete delete = (Translog.Delete) operation;
                            buildDelete(delete);
                            break;
                        case DELETE_BY_QUERY:
                            Translog.DeleteByQuery dbq = (Translog.DeleteByQuery) operation;
                            buildDeleteByQuery(dbq);
                            break;
                        default:
                            System.out.println("Invaid Operation Type");
                            break;
                        }
	                    
	                    count++;
	                    
	                }catch(Exception e){
	                	break;
	                }
	            }
	        }catch(Exception e){
	        	e.printStackTrace();
	        }finally{
	        	if(null!=fs){
	        		try {
						fs.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
	        	}
            }
	        return builder;
	
	}
    @Override
    public void handleRequest(final RestRequest request, final RestChannel channel) {
        String file = request.param("file");
        String sizeStr = request.param("size");
        int size = 10;
        if(null!=sizeStr){
        	size = Integer.parseInt(sizeStr);
        }
		try {
			builder = RestXContentBuilder.restContentBuilder(request);
			if(null!=file){
				builder.startArray();
				buildTranslog(file,size);
				builder.endArray();
			}else{
				builder.startObject()
						.field("success").value(false)
						.field("error").value("缺少參數(shù):file")
						.endObject();
			}
			channel.sendResponse(new XContentRestResponse(request,OK,builder));
		} catch (IOException e) {
			e.printStackTrace();
		}
        
    }
}

最后 es-plugin.properties 這個配置文件,是我們必須的一個類,它主要告訴ES, 哪個類是我們插件的實現(xiàn)類 ,對于site插件,提供插件的名稱和描述信息,細節(jié)可以參考 PluginsService類 和 loadPluginsFromClasspath 方法

plugin=org.elasticsearch.plugin.translog.TranslogRestPlugin

好了,現(xiàn)在可以去安裝我們的插件了。有2個辦法。

  1. 手動安裝,導出Jar包,復制到 /plugins/translogRest/TranslogRest.jar 下面。

  2. 命令安裝,plugin --install translogRest --url file:/d:/TranslogRest.jar

瀏覽器里或者curl

    http://localhost:9200/_translog?pretty=true

返回

{

  • success: false,

  • error: "缺少參數(shù):file"

}

表示我們的插件,已經(jīng)安裝成功。

“elasticsearch的jvm插件怎么使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

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

AI