共計 2833 個字符,預計需要花費 8 分鐘才能閱讀完成。
本篇內容介紹了“solrcloud 和 solr 在集群中建立索方法是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1 通過通過 zookeeper 通道建立索引
public static void main(String[] args) throws IOException, SolrServerException {
// 注意,zkHost 在 windows 偽分布式與 hadoop 分布式設置不一樣
//windows 偽分布式 zkHost 最后不需要加 /solr,linux 的 hadoop 中需要添加 /solr
String zkHost = node1:2181,node2:2181,node3:2181/solr
String defaultCollection = collection1
CloudSolrServer server = new CloudSolrServer(zkHost);
server.setDefaultCollection(defaultCollection);
for (int i = 0; i 1000; ++i) {SolrInputDocument doc = new SolrInputDocument();
doc.addField( cat , book
doc.addField(id , book- + i);
doc.addField(name , The Legend of Po part + i);
server.add(doc);
if (i % 100 == 0)
server.commit(); // periodically flush
server.commit();}
2 注意,CDH 中,可以看 zookeeper 的設置,最大鏈接數為 60,所以對于 zookeeper 一般用單例
public class myCloudSolrServer {
// solrServer
public static CloudSolrServer solrServer;
// 效率不高的方法
// public static synchronized CloudSolrServer getSolrServer() {// if (solrServer == null) {
// try {
// solrServer = new CloudSolrServer(Const.ZK_HOST + /solr
// final int zkClientTimeout = 20000; // 心跳 20 秒
// final int zkConnectTimeout = 10000; // 設置鏈接主機超時(單位毫秒)// solrServer.setDefaultCollection(Const.defaultCollection);
// solrServer.setZkClientTimeout(zkClientTimeout);
// solrServer.setZkConnectTimeout(zkConnectTimeout);
// } catch (Exception e) {// e.printStackTrace();
// }
// }
// return solrServer;
// }
// 支持大并發方法
public static CloudSolrServer getSolrServer() { if (solrServer == null) { synchronized (myCloudSolrServer.class) { if (solrServer == null) {
try {
solrServer = new CloudSolrServer(Const.ZK_HOST + /solr
final int zkClientTimeout = 20000; // 心跳 20 秒
final int zkConnectTimeout = 10000; // 設置鏈接主機超時(單位毫秒) solrServer.setDefaultCollection(Const.defaultCollection);
solrServer.setZkClientTimeout(zkClientTimeout);
solrServer.setZkConnectTimeout(zkConnectTimeout);
} catch (Exception e) { e.printStackTrace();
}
}
}
}
return solrServer;
}
}
3 急群中可以指定到某個分片
public static void main(String[] args) throws IOException, SolrServerException {
String url= node1:8983/solr/core_shard1_replica1
String defaultCollection = collection1
HttpSolrServer server = new HttpSolrServer (url);
server.setDefaultCollection(defaultCollection);
for (int i = 0; i 1000; ++i) {SolrInputDocument doc = new SolrInputDocument();
doc.addField( cat , book
doc.addField(id , book- + i);
doc.addField(name , The Legend of Po part + i);
server.add(doc);
if (i % 100 == 0)
server.commit(); // periodically flush
server.commit();}
注意,在建立索引的時候,不要每一個就去 commit,為了提高效率,一般用
Collection SolrInputDocument docs = new ArrayList SolrInputDocument
for(){ SolrInputDocument doc = new SolrInputDocument();
docs.add(doc);
}
server.add(docs);
server.commit();
其實 server.commit() 效率也不高,一般用軟提交
“solrcloud 和 solr 在集群中建立索方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!
正文完