Solr之java操作集群 -yellowcong
来源:互联网 发布:苹果电脑mac怎么开机 编辑:程序博客网 时间:2024/06/08 16:35
通过Java来操作Solr的集群,可以说和单机的模式都差不多,只是连接的对象是zookeeper服务器了,而不是直接连接的solr服务器。通过CloudSolrServer 连连接服务器,设定操作的集合Collection。然后其他的就和单机的一样了,问题点是,集群的,不能直接将所有的数据都查出,。。。尴尬了
给集群的Solr添加索引
public static void index() throws Exception { // 路径是zookeeper的服务的路径 CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); //添加集合 for(int i=0;i<100;i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", UUID.randomUUID().toString()); doc.addField("username", "yellowcong"+i); doc.addField("content", "我是逗比聪"+i); doc.addField("title", "逗比聪"+i); server.add(doc); } //提交事物 server.commit(); System.out.println("------------------------添加索引---------------------");}
删除索引
public static void deleteAll() throws Exception { CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); //清空数据 server.deleteByQuery("*:*"); //提交事物 server.commit(); System.out.println("------------------------数据清空---------------------");}
查询索引
SolrQuery .setRows(Integer.MAX_VALUE) ,这个有问题,会报错
而且集群了,由于是单机的环境,所以查询起来,特别的慢。。。。
public static void query() throws Exception { CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); SolrQuery query = new SolrQuery(); query.add("q","title:逗"); query.setStart(0); //query.setRows(Integer.MAX_VALUE); query.setRows(10); QueryResponse resp = server.query(query); //ArrayList<SolrDocument> SolrDocumentList docs = resp.getResults(); System.out.printf("查询到了%d条数据\r\n",docs.getNumFound());; for(SolrDocument doc:docs) { String id = doc.get("id").toString(); String username = doc.get("username").toString(); String content = doc.get("content").toString(); String title = doc.get("title").toString(); System.out.printf("%s:%s:%s:%s\r\n",id,username,content,title); }}
查询结果
添加到spring
<!-- 集群版 --> <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <constructor-arg name="zkHost" value="192.168.66.100:2181,192.168.66.100:2182,192.168.66.100:2183"></constructor-arg> <property name="defaultCollection" value="collection3"></property> </bean>
够着函数注入host
属性注入
完整代码
package com.yellowcong.day12_06;import java.util.UUID;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.impl.CloudSolrServer;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.SolrInputDocument;/** * * 作者:yellowcong <br/> * 日期:2017/12/06 <br/> * 時間:16:15:39 <br/> * 描述: */public class Demo { // 创建一个和solr集群的连接 // 参数就是zookeeper的地址列表,使用逗号分隔 private static final String ZK_HOST = "192.168.66.100:2181,192.168.66.100:2182,192.168.66.100:2183"; public static void main(String[] args) throws Exception { //删除所有索引 //deleteAll(); //添加索引 //index(); //查询数据 query() ; } /** * 作者:yellowcong <br/> * 日期:2017/12/06 <br/> * 時間:16:34:07 <br/> * 描述:删除所有索引 * @throws Exception */ public static void deleteAll() throws Exception { CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); //清空数据 server.deleteByQuery("*:*"); //提交事物 server.commit(); System.out.println("------------------------数据清空---------------------"); } /** * 作者:yellowcong <br/> * 日期:2017/12/06 <br/> * 時間:16:31:53 <br/> * 描述:检索数据 * @throws Exception */ public static void query() throws Exception { CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); SolrQuery query = new SolrQuery(); query.add("q","title:逗"); query.setStart(0); //query.setRows(Integer.MAX_VALUE); query.setRows(10); QueryResponse resp = server.query(query); //ArrayList<SolrDocument> SolrDocumentList docs = resp.getResults(); System.out.printf("查询到了%d条数据\r\n",docs.getNumFound());; for(SolrDocument doc:docs) { String id = doc.get("id").toString(); String username = doc.get("username").toString(); String content = doc.get("content").toString(); String title = doc.get("title").toString(); System.out.printf("%s:%s:%s:%s\r\n",id,username,content,title); } } /** * 作者:yellowcong <br/> * 日期:2017/12/06 <br/> * 時間:16:25:43 <br/> * 描述:添加索引 * @throws Exception */ public static void index() throws Exception { // 路径是zookeeper的服务的路径 CloudSolrServer server = new CloudSolrServer(ZK_HOST); // 设置默认的collection server.setDefaultCollection("collection3"); //添加集合 for(int i=0;i<100;i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", UUID.randomUUID().toString()); doc.addField("username", "yellowcong"+i); doc.addField("content", "我是逗比聪"+i); doc.addField("title", "逗比聪"+i); server.add(doc); } //提交事物 server.commit(); System.out.println("------------------------添加索引---------------------"); }}
错误合集
No live SolrServers available to handle this request
导致这个问题的原因是,我设定查询数据为所有数据,所导致的。。。SolrQuery .setRows(Integer.MAX_VALUE),不能直接查询所有数据,这点请注意。
Exception in thread "main" org.apache.solr.client.solrj.SolrServerException: No live SolrServers available to handle this request:[http://192.168.66.100:8083/solr/collection3_shard1_replica1, http://192.168.66.100:8081/solr/collection3_shard1_replica2, http://192.168.66.100:8082/solr/collection3_shard1_replica3] at org.apache.solr.client.solrj.impl.LBHttpSolrServer.request(LBHttpSolrServer.java:337) at org.apache.solr.client.solrj.impl.CloudSolrServer.request(CloudSolrServer.java:623) at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90) at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301) at com.yellowcong.day12_06.Demo.query(Demo.java:76)
阅读全文
0 0
- Solr之java操作集群 -yellowcong
- Solr之集群安装-yellowcong
- Solr之集群管理Collection-yellowcong
- Solr之入门案例-yellowcong
- Solr之单机安装-yellowcong
- Solr之日志配置-yellowcong
- Solr之分页查询-yellowcong
- Redis之集群安装-yellowcong
- Redis之集群删除-yellowcong
- Zookeeper之集群搭建-yellowcong
- Solr之IKAnalyzer中文分词器-yellowcong
- Solr之高亮显示-yellowcong
- Solr之基于注解开发-yellowcong
- Solr之定时重建索引-yellowcong
- Solr之ZooKeeper管理配置文件-yellowcong
- Hadoop之Java通过URL操作HDFS-yellowcong
- Java之Javassist对类的常用操作小结-yellowcong
- ActiveMQ之集群(主从)搭建-yellowcong
- Url-pattern
- iOS开发官方中文网址:
- 最新国际互联网线路分布
- git的学习笔记1
- 霓虹灯闪烁
- Solr之java操作集群 -yellowcong
- LeetCode 538. Convert BST to Greater Tree (Easy)
- Android中获取圆角图片和圆形图片的三种方式
- 大家好这是我的csdn博客
- ios-枚举类型
- 后端技术栈
- Docker学习笔记(2)--Docker常用命令
- 。。。。
- httpServletRequest