solrcloud和solr在集群中建立索方法是什么

56次阅读
没有评论

共计 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 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计2833字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)