Solr CloudSolrClient:原因:java.lang.ClassCastException:java.lang.String无法转换为java.util.Map

时间:2019-06-19 13:56:42

标签: java solr solrj solrcloud solr4

我正在将CloudSolrClient与Spring boot Solr 1.5.18结合使用.RELEASE和Solr版本是Solr 4.10.3。

  

SolrClient solrClient =新   CloudSolrClient.Builder()。withZkHost(zkHosts).withZkChroot(solrProperties.getSolrZookeeperLocation())。build();

当我使用CDH 5.16.1默认提供的Solr时,即Solr 4.10.3 保存文档时出现以下错误:

org.springframework.data.solr.UncategorizedSolrException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map; nested exception is org.apache.solr.client.solrj.SolrServerException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:168)
at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:223)
at com.solix.emailarchiving.email.solr.search.SolrTemplateWrapper.saveBeans(SolrTemplateWrapper.java:33)
at com.solix.emailarchiving.email.solr.search.EmailSearchRepositoryImpl.saveEmails(EmailSearchRepositoryImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy153.saveEmails(Unknown Source)
at com.solix.emailarchiving.email.EmailSolrServiceImpl.saveEmails(EmailSolrServiceImpl.java:24)
at com.solix.emailarchiving.email.EmailSolrServiceImpl$$FastClassBySpringCGLIB$$40f18538.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
at com.solix.emailarchiving.annotation.ServiceTransactionAspect.beforeMethod(ServiceTransactionAspect.java:46)
at sun.reflect.GeneratedMethodAccessor138.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.solix.emailarchiving.email.EmailSolrServiceImpl$$EnhancerBySpringCGLIB$$a84565b1.saveEmails(<generated>)
at com.solix.emailarchiving.hbasetosolrsync.EmailsSync.sendEmailsToSolr(EmailsSync.java:157)
at com.solix.emailarchiving.hbasetosolrsync.EmailsSync.syncMailsFromHbaseToSolr(EmailsSync.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:750)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.solix.emailarchiving.hbasetosolrsync.EmailsSync$$EnhancerBySpringCGLIB$$2714482.syncMailsFromHbaseToSolr(<generated>)
at com.solix.emailarchiving.hbasetosolrsync.HBaseToSolrEmailSyncConsumer.processEmailIdModels(HBaseToSolrEmailSyncConsumer.java:129)
at com.solix.emailarchiving.hbasetosolrsync.HBaseToSolrEmailSyncConsumer.hbaseToSolrSyncListen(HBaseToSolrEmailSyncConsumer.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114)
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:248)
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:80)
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:51)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1001)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:981)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:932)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:801)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:689)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.solr.client.solrj.SolrServerException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1246)
at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1073)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:160)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:106)
at org.springframework.data.solr.core.SolrTemplate.lambda$saveBeans$3(SolrTemplate.java:223)
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:164)
... 66 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at org.apache.solr.common.cloud.DocRouter.getRouteField(DocRouter.java:53)
at org.apache.solr.common.cloud.CompositeIdRouter.sliceHash(CompositeIdRouter.java:46)
at org.apache.solr.common.cloud.HashBasedRouter.getTargetSlice(HashBasedRouter.java:38)
at org.apache.solr.client.solrj.request.UpdateRequest.getRoutes(UpdateRequest.java:262)
at org.apache.solr.client.solrj.impl.CloudSolrClient.directUpdate(CloudSolrClient.java:771)
at org.apache.solr.client.solrj.impl.CloudSolrClient.sendRequest(CloudSolrClient.java:1263)
at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1134)
... 71 more

当我深入研究谷歌搜索后,我发现这个问题与动物园管理员 clusterstate.json

中的 router 字段有关。

关于clusterstate.json中我的收藏的信息是

"88_Exchange12_2":{
    "shards":{"shard1":{
        "range":"80000000-7fffffff",
        "state":"active",
        "replicas":{"core_node1":{
            "state":"active",
            "core":"88_Exchange12_2_shard1_replica1",
            "node_name":"SEZ-DESK-047-Linux:8983_solr",
            "base_url":"http://SEZ-DESK-047-Linux:8983/solr",
            "leader":"true"}}}},
    "maxShardsPerNode":"1",
    "router":"compositeId",
    "replicationFactor":"1",
    "autoAddReplicas":"false",
    "routerSpec":{"name":"compositeId"}},

路由器字段中的值应类似于“ router”:{“ name”:“ compositeId”}。但是,如果存储不正确,则会导致上述ClassCastException异常。

但是当我使用相同版本的Apache Solr 4.10.3(不是CDH Solr 4.10.3)时,“ router”设置了正确的值。

  "92_S2R1_35":{
    "replicationFactor":"1",
    "shards":{
      "shard1":{
        "range":"80000000-ffffffff",
        "state":"active",
        "replicas":{"core_node2":{
            "core":"92_S2R1_35_shard1_replica1",
            "base_url":"http://172.52.110.44:8983/solr",
            "node_name":"172.52.110.44:8983_solr",
            "state":"down",
            "leader":"true"}}},
      "shard2":{
        "range":"0-7fffffff",
        "state":"active",
        "replicas":{}}},
    "router":{"name":"compositeId"},
    "maxShardsPerNode":"1",
    "autoAddReplicas":"false",
    "autoCreated":"true"}

Q1。如何解决这个问题。创建Collection时,可以通过Java代码以编程方式设置路由器吗? 下面是我创建集合的方法:

SolrZkClient solrZkClient=null;
    try {

        String solrZkUrl= solrProperties.getSolrZookeerHost()+solrProperties.getSolrZookeeperLocation();
        solrZkClient = new SolrZkClient(solrZkUrl, 10000);

        Resource resource = new ClassPathResource(solrConfig);
        java.nio.file.Path path = Paths.get(resource.getURI());

        solrZkClient.upConfig(path, solrConfigName);
        LOG.info("Solr Config created with name : \"" + solrConfigName+ "\"");
    }catch (IOException e) {
        LOG.error("Something went Wrong while creating SolrConfig for tenantId: "+ tenantId , e);               
    }finally {
        if (solrZkClient != null) {
            solrZkClient.close();
        }
    }

    int numShards=(solrProperties.getNumShards()<=0)?1:solrProperties.getNumShards();
    int numReplicas=(solrProperties.getNumReplicas()<=0)?1:solrProperties.getNumReplicas();

    SolrClient solrClient=null;
    try {
        Create collection = CollectionAdminRequest.createCollection(collectiomName, solrConfigName, numShards, numReplicas);

        //          collection.setMaxShardsPerNode(-1);
        solrClient = solrConectionUtils.solrClient(tenantId);
        CollectionAdminResponse response = collection.process(solrClient);
        if (!response.isSuccess()) {
            LOG.error(response.getErrorMessages().toString());
            throw new ApplicationException(ExceptionCodeType.SOLR_COLLECTION_CREATION_FAILED);
        } else {
            LOG.info("Collection created with name : \"" + collection.getCollectionName()+ "\"");
        }
    } catch (SolrException | SolrServerException e) {
        LOG.error("Something went wrong while creating collection");                


    }finally {
        if (solrClient != null) {
            solrClient.close();
        }
    }

Q2。在CDH的clusterstate.json中,我们可以看到另外一个名为'routerSpec'的字段,该字段具有正确的值,但是我认为它没有被使用。有没有办法将此字段用作路由器并完成工作?

1 个答案:

答案 0 :(得分:1)

在Solr 4.5.0中,确实将router属性从字符串更改为映射,但为避免这种情况,向solrj客户端添加了向后兼容的读取代码。我不知道您为什么在4.10.3中看到此错误,4.10.3中的getRouterField方法是https://github.com/apache/lucene-solr/blob/d52936c3c3f92aa3d04eaeeab3472a6e259be7b6/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java#L52,它检查路由器对象是否为字符串并返回null。如果没有其他问题,也许您可​​以在zookeeper中手动修改群集状态对象,以将路由器更改为json映射。