標籤: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