这是我的代码:
import org.apache.solr.client.solrj.*
import org.apache.solr.client.solrj.impl.*;
import org.apache.solr.client.solrj.impl.HttpClientUtil.*;
import org.apache.solr.client.solrj.response.*;
import org.apache.solr.common.*;
import java.io.*;
public class Response{
public static void main(String[] args) throws SolrServerException, IOException
{
/*String host = "localhost:9983",localkey = (""+Math.random());
Search srch = new Search(host,localkey, "Search");
Update updt = new Update(host,localkey, "Update");
srch.start();
updt.start();*/
String zkHost = "http://localhost:9983";
CloudSolrClient server = new CloudSolrClient(zkHost);
server.setDefaultCollection("Test");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1234");
doc.addField("name", "A lovely summer holiday");
server.add(doc);
server.commit();
}
}
My Solr Cloud在端口8983,7575,8984,7576上运行,Zookeeper在9983上运行。 运行代码时出现以下错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
Exception in thread "main" org.apache.solr.common.SolrException: Unable to create HttpClient instance.
at org.apache.solr.client.solrj.impl.HttpClientUtil$HttpClientFactory.createHttpClient(HttpClientUtil.java:393)
at org.apache.solr.client.solrj.impl.HttpClientUtil.createClient(HttpClientUtil.java:124)
at org.apache.solr.client.solrj.impl.CloudSolrClient.<init>(CloudSolrClient.java:189)
at Response.main(Response.java:16)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.apache.solr.client.solrj.impl.HttpClientUtil$HttpClientFactory.createHttpClient(HttpClientUtil.java:391)
... 3 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.CloseableHttpClient.<init>(CloseableHttpClient.java:58)
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:287)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:128)
at org.apache.http.impl.client.SystemDefaultHttpClient.<init>(SystemDefaultHttpClient.java:116)
... 8 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
有人可以告诉我如何通过吗?我无法弄明白。
答案 0 :(得分:0)
您有记录器和 http连接问题。并且您必须将以下依赖项添加到项目pom.xml中以解决此问题。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
希望它会对你有所帮助:)。