我无法在Solr中建立查询

时间:2015-03-31 09:20:50

标签: solr solrj solr4 solrcloud

我正在尝试在solr中形成一个用于数据导入的查询,但无法这样做。

我需要形成以下查询: http://salsa23q-XXX-08.XXX.XXX.com:8080/solr/#/geoloc_replica1/dataimport/?command=full-import&clean=true

我正在尝试的代码:

public class SolrJDB
{
    public static String url = "http://salsa23q-XXX-08.XXX.XXX.com:8080/solr:8080/solr";
    public static SolrServer localserver;
    public static CloudSolrServer cloudserver;// = new CloudSolrServer("url");
    public static SolrQuery que;
    public static SolrInputDocument doc;
    public static SolrDocumentList list;
    public static QueryResponse response;

    public static String serverurl = "http://salsa23q-XXX-08.XXX.XXX.com:8080/solr";

public static void main(String[] args) throws MalformedURLException, SQLException, SolrServerException {
        try{
            System.out.println("+++++++++++++      Starting here       +++++++++++++++++++++");
            //Cloud Server
            String url = "salsa23q-XXX-08.XXX.XXX.com:8080/solr";
            cloudserver = new CloudSolrServer(url);
            SolrQuery parameters = new SolrQuery();
            parameters.set("qt","/geoloc_replica1");
            parameters.set("qt","//dataimport");
            parameters.set("command","full-import");
            System.out.println("Query to be Executed ============"+parameters.toString());
            QueryResponse response = cloudserver.query(parameters);
            SolrDocumentList list = response.getResults();
        }
        catch(SolrServerException e){
            System.out.println(e.toString());
            e.printStackTrace();
        }
        catch(Exception e){
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}


I am getting following error:

    org.apache.solr.client.solrj.SolrServerException: Error executing query
        at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:98)
        at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
        at SolrJDB.main(SolrJDB.java:37)
    Caused by: java.lang.RuntimeException
        at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:115)
        at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:83)
        at org.apache.solr.common.cloud.ZkStateReader.<init>(ZkStateReader.java:138)
        at org.apache.solr.client.solrj.impl.CloudSolrServer.connect(CloudSolrServer.java:140)
        at org.apache.solr.client.solrj.impl.CloudSolrServer.request(CloudSolrServer.java:165)
        at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
        ... 2 more

2 个答案:

答案 0 :(得分:0)

您正在混淆代码中的内容。您想要执行运行DataImportHandler的请求。但是你正在构建一个SolrQuery。后者代表搜索请求。

通过Java API运行DataImportHandler所需的是UpdateRequest

public int runDataImportHandler() throws Exception {

    // fill in the parameters you want to run your import with
    ModifiableSolrParams tmpParams = new ModifiableSolrParams();
    tmpParams.set("command", "full-import");
    tmpParams.set("clean", true);
    tmpParams.set("commit", true);
    tmpParams.set("optimize", true);

    // create the update request
    UpdateRequest tmpRequest = new UpdateRequest("/dataimport");
    tmpRequest.setParams(tmpParams);

    SolrServer tmpServer = getServer();
    tmpRequest.process(tmpServer);

    ModifiableSolrParams tmpStatusParams = new ModifiableSolrParams();
    tmpStatusParams.set("command", "status");
    String tmpStatus = "busy";
    int tmpProcessed = 0;
    do {
        System.out.println("waiting for import to finish, status was " + tmpStatus);
        Thread.sleep(500);

        UpdateRequest tmpStatusRequest = new UpdateRequest("/dataimport");
        tmpStatusRequest.setParams(tmpStatusParams);
        UpdateResponse tmpStatusResponse = tmpStatusRequest.process(tmpServer);
        tmpStatus = tmpStatusResponse.getResponse().get("status").toString();

        Map tmpMessages = (Map) tmpStatusResponse.getResponse().get("statusMessages");
        System.out.println("import status is " + tmpStatus);
        if (tmpMessages.get("Total Documents Processed") != null) {
            tmpProcessed = Integer.valueOf(tmpMessages.get("Total Documents Processed").toString());
        }
    } while ("busy".equals(tmpStatus));

    System.out.println("import done");

    return tmpProcessed;
}

答案 1 :(得分:0)

正如我可以看到您的代码,异常与CloudSolrServer实例的创建方式有关: -

要创建CloudSolrServer的实例,您需要将Solr置于云模式,此处使用的URL将是zookeeper地址。 CloudSolrServer创建zkStateReader,它获取它查询的实时节点和集合。

以下是为dataimport创建代码的确切方法: -

  public static void main(String[] args) throws SolrServerException, IOException {

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(UpdateParams.COLLECTION, "collection1");
    params.set(CommonParams.QT, "/dataimport");
    params.set("command", "full-import");
    params.set("claen", true);
    params.set(UpdateParams.COMMIT, true);
    params.set(UpdateParams.OPTIMIZE, true);
    String url = "localhost:9983"; /*Zookeeper Address*/
    CloudSolrServer cloudserver = new CloudSolrServer(url, true);
    cloudserver.setDefaultCollection("test");/*This is necessary in case if you are not specifying any collection name in dataimport*/
    cloudserver.query(params);
}