使用java代碼對zookeeper叢集中的solrCloud資料進行CURD

來源:互聯網
上載者:User

標籤:end   solrcloud   response   throw   commit   tde   資料   pac   pen   

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);        }    }

 

使用java代碼對zookeeper叢集中的solrCloud資料進行CURD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.