阅读背景:

solr java api 使用solrj操作zookeeper集群中的solrCloud中的数据

来源:互联网 

1 导入相关的pom依赖

<dependencies>
      <dependency>
          <groupId>org.apache.solr</groupId>
          <artifactId>solr-solrj</artifactId>
          <version>4.10.2</version>
      </dependency>
       <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging-api</artifactId>
          <version>1.1</version>
      </dependency>
  </dependencies>

2 编写代码,完成CURD

    //添加索引
    @Test
    public void createIndexToSolrCloud() throws IOException, SolrServerException {
        //创建连接solrCloud的服务对象
        //String zkHost: 需要传递zookeeper集群的地址
        String zkHost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181";
        CloudSolrServer solrServer = new CloudSolrServer(zkHost);

        //设置连接哪个solr的索引库
        solrServer.setDefaultCollection("collection2");
        //可选的参数
        //设置连接zookeeper的时间
        solrServer.setZkClientTimeout(5000);
        //设置获取和solr的连接的时间
        solrServer.setZkConnectTimeout(5000);

        //执行获取连接
        solrServer.connect();

        //添加索引操作
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "1");
        document.addField("name", "hello solrCloud");
        solrServer.add(document);

        //执行提交
        solrServer.commit();
    }


    //删除索引
    @Test
    public void deleteIndex() throws IOException, SolrServerException {
        //创建连接solrCloud的服务对象
        //String zkHost: 需要传递zookeeper集群的地址
        String zkHost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181";
        CloudSolrServer solrServer = new CloudSolrServer(zkHost);
        //设置连接哪个solr的索引库
        solrServer.setDefaultCollection("collection2");
        //可选的参数
        //设置连接zookeeper的时间
        solrServer.setZkClientTimeout(5000);
        //设置获取和solr的连接的时间
        solrServer.setZkConnectTimeout(5000);
        //执行获取连接
        solrServer.connect();

        //删除索引
        solrServer.deleteById("1");

        //执行提交
        solrServer.commit();
    }

    //查询
    @Test
    public void queryIndex() throws SolrServerException {
        //创建连接solrCloud的服务对象
        //String zkHost: 需要传递zookeeper集群的地址
        String zkHost = "192.168.44.28:2181,192.168.44.29:2181,192.168.44.30:2181";
        CloudSolrServer solrServer = new CloudSolrServer(zkHost);
        //设置连接哪个solr的索引库
        solrServer.setDefaultCollection("collection2");
        //可选的参数
        //设置连接zookeeper的时间
        solrServer.setZkClientTimeout(5000);
        //设置获取和solr的连接的时间
        solrServer.setZkConnectTimeout(5000);
        //执行获取连接
        solrServer.connect();

        //添加查询操作
        SolrQuery query = new SolrQuery("*:*");
        QueryResponse response = solrServer.query(query);

        SolrDocumentList documents = response.getResults();
        for (SolrDocument document : documents) {
            Object id = document.get("id");
            Object name = document.get("name");
            System.out.println(id + "--" + name);
        }
    }

 


分享到: