我开始环境是
linux上是6.6.5 pom也是6.6.5
按照学习视频的demo,他用的是4点几的solr,我换成了6点几的,没有CloudSolrServer 只有CloudSolrClient
然后看源码的构造方法,写出了第一版
@Test
public void testAddDocument() throws Exception {
//创建一个集群的连接,应该使用CloudSlorServer创建
CloudSolrClient cloudSolrClient = new CloudSolrClient("192.168.25.134:2181,192.168.25.134:2182,192.168.25.134:2183"); //zkHost:zookeeper的地址列表
//设置一个defaultCollection属性
cloudSolrClient.setDefaultCollection("collection2");//Solr管理界面Graph的那个collection
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.setField("id", "solrcloud01");
document.setField("item_title", "测试商品01");
document.setField("item_price", );
//把文档写入索引库
cloudSolrClient.add(document);
//提交
cloudSolrClient.commit();
cloudSolrClient.close();
}
java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.MDC
at org.apache.solr.common.SolrException.<init>(SolrException.java:)
at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:)
at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:)
at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:)
at org.apache.solr.common.cloud.ZkStateReader.<init>(ZkStateReader.java:)
at org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider.connect(ZkClientClusterStateProvider.java:)
at org.apache.solr.client.solrj.impl.CloudSolrClient.connect(CloudSolrClient.java:)
at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:)
at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:)
at com.fighting.solrj.TestSolrCloud.testAddDocument(TestSolrCloud.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
at java.lang.reflect.Method.invoke(Method.java:)
at org.junit.runners.model.FrameworkMethod$.runReflectiveCall(FrameworkMethod.java:)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:)
at org.junit.runners.ParentRunner$.run(ParentRunner.java:)
at org.junit.runners.ParentRunner$.schedule(ParentRunner.java:)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:)
at org.junit.runners.ParentRunner.access$(ParentRunner.java:)
at org.junit.runners.ParentRunner$.evaluate(ParentRunner.java:)
at org.junit.runners.ParentRunner.run(ParentRunner.java:)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:)
报错,说是slf4j的错误,然后百度,只有*的两个链接 ,而且没有解决方法
而且我也感觉不是slf4j的问题
然后继续百度 看看有没有其他方法
第二版
@Test
public void testAddDocument() throws Exception {
//创建一个集群的连接,应该使用CloudSlorServer创建
//CloudSolrClient cloudSolrClient = new CloudSolrClient("192.168.25.134:2181,192.168.25.134:2182,192.168.25.134:2183");
List<String> zkHosts = new ArrayList<String>();
zkHosts.add("192.168.25.134:2181");
zkHosts.add("192.168.25.134:2182");
zkHosts.add("192.168.25.134:2183"); CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder().withZkHost(zkHosts).build();
//zkHost:zookeeper的地址列表
//设置一个defaultCollection属性
cloudSolrClient.setDefaultCollection("collection2");//Solr管理界面Graph的那个collection
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.setField("id", "solrcloud01");
document.setField("item_title", "测试商品01");
document.setField("item_price", );
//把文档写入索引库
cloudSolrClient.add(document);
//提交
cloudSolrClient.commit();
cloudSolrClient.close();
}
还是上面那个报错
然后看了搜了搜开源项目 也没用连接集群的
网上只有一个版本是7.4.0 的
linux上solr集群的环境没变,把pom中solrj的版本给为7.4.0
使用他的方法
//创建一个集群的连接,应该使用CloudSlorServer创建
//CloudSolrClient cloudSolrClient = new CloudSolrClient("192.168.25.134:2181,192.168.25.134:2182,192.168.25.134:2183");
List<String> zkHosts = new ArrayList<String>();
zkHosts.add("192.168.25.134:2181");
zkHosts.add("192.168.25.134:2182");
zkHosts.add("192.168.25.134:2183");
Optional<String> zkChroot = Optional.of("/");
//CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder().withZkHost(zkHosts).build();
Builder builder = new CloudSolrClient.Builder(zkHosts, zkChroot);
CloudSolrClient cloudSolrClient = builder.build();
//zkHost:zookeeper的地址列表
//设置一个defaultCollection属性
cloudSolrClient.setDefaultCollection("collection2");//Solr管理界面Graph的那个collection
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.setField("id", "solrcloud01");
document.setField("item_title", "测试商品01");
document.setField("item_price", 123);
//把文档写入索引库
cloudSolrClient.add(document);
//提交
cloudSolrClient.commit();
cloudSolrClient.close();
没报错 查询也有数据了