Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous SolrJ-使用ContentStreamUpdateRequest异步索引文档_Asynchronous_Solr_Indexing_Request_Solrj - Fatal编程技术网

Asynchronous SolrJ-使用ContentStreamUpdateRequest异步索引文档

Asynchronous SolrJ-使用ContentStreamUpdateRequest异步索引文档,asynchronous,solr,indexing,request,solrj,Asynchronous,Solr,Indexing,Request,Solrj,我正在使用SolrJ API 4.8将丰富的文档索引到solr。但是我想异步索引这些文档。我创建的函数同步发送文档,但我不知道如何将其更改为异步。有什么想法吗 功能: public Boolean indexDocument(HttpSolrServer server, String PathFile, InputReader external) { ContentStreamUpdateRequest up = new ContentStreamUpdateRequest

我正在使用SolrJ API 4.8将丰富的文档索引到solr。但是我想异步索引这些文档。我创建的函数同步发送文档,但我不知道如何将其更改为异步。有什么想法吗

功能:

public Boolean indexDocument(HttpSolrServer server, String PathFile, InputReader external)
{  

        ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");

        try {
                up.addFile(new File(PathFile), "text");
        } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;
        }

        up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);

        try {
                server.request(up);
        } catch (SolrServerException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;

        } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;   
        }
        return true;
}

Solr server:Version4.8

听起来您可能想看看如何使用ExecutorService和FutureTask来实现这一点:

private static HttpSolrServer server;
private static int threadPoolSize = 4;  //Set this to something appropiate for your environment

public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
    ArrayList<FutureTask<Boolean>> taskList = new ArrayList<FutureTask<Boolean>>();
    ArrayList<String> paths = new ArrayList<String>();
    //Initialize your list of paths here

    for (String path : paths) {
        FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new IndexDocumentTask(path));
        taskList.add(futureTask);
        executor.execute(futureTask);
    }

    for (int i = 0; i < taskList.size(); i++) {
        FutureTask<Boolean> futureTask = taskList.get(i);

        try {
            System.out.println("Index Task " + i + (futureTask.get() ? " finished successfully." : " encountered an error."));
        } catch (ExecutionException e) {
            System.out.println("An Execution Exception occurred with Index Task " + i);
        } catch (InterruptedException e) {
            System.out.println("An Interrupted Exception occurred with Index Task " + i);
        }
    }

    executor.shutdown();
}

static class IndexDocumentTask implements Callable<Boolean> {

    private String pathFile;

    public IndexDocumentTask(String pathFile) {
        this.pathFile = pathFile;
    }

    @Override
    public Boolean call() {
        return indexDocument(pathFile);
    }

    public Boolean indexDocument(String pathFile) {
        ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");

        try {
            up.addFile(new File(pathFile), "text");
        } catch (IOException e) {
            Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
            return false;
        }

        up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);

        try {
            server.request(up);
        } catch (SolrServerException e) {
            Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
            return false;

        } catch (IOException e) {
            Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
            return false;
        }
        return true;
    }
}
私有静态HttpSolrServer服务器;
私有静态int threadPoolSize=4//将此设置为适合您的环境的值
公共静态void main(字符串[]args){
ExecutorService executor=Executors.newFixedThreadPool(threadPoolSize);
ArrayList taskList=新建ArrayList();
ArrayList路径=新的ArrayList();
//在此处初始化路径列表
用于(字符串路径:路径){
FutureTask FutureTask=新的FutureTask(新索引文档任务(路径));
任务列表。添加(未来任务);
执行人。执行(未来任务);
}
对于(int i=0;i
这是未经测试的代码,所以我不确定这样调用
server.request(up)
是否是线程安全的。我认为只使用一个HttpSolrServer实例更简单,但您也可以在每个任务中创建新的HttpSolrServer实例

如果愿意,您可以扩展IndexDocumentTask以实现可调用的
,这样您就可以检索要编制索引的文档的文件名以及索引是否成功

尽管我不认为一次向Solr服务器发送多个请求会有问题,但您可能希望限制您的请求,以免Solr服务器过载