Как сохранить вложенный документ JSONArray в couchbase - PullRequest
0 голосов
/ 20 октября 2018

Я пытаюсь добавить JSONArray и некоторые другие поля в существующий документ json в couchbase, но получаю сообщение об ошибке при вызове couchbase.Пожалуйста, помогите!

Здесь я устанавливаю JSONArray для JsonBody, который затем устанавливается в тело Exchange (см. Ниже)

    JSONArray smsRequestArray = new JSONArray();
    for(String camelSmppId : camelSmppIdList) {
        JSONObject obj = new JSONObject();
        obj.put("smsRequestId", camelSmppId);
        obj.put("status", "InProgress");
        smsRequestArray.put(obj);
    }

jsonBody.put("smsRequest", smsRequestArray);
exchange.getIn().setBody(jsonBody.toString());

Теперь я хочу обновить документ Couchbaseс этим массивом json

JSONArray updateObj = new JSONArray();
JSONObject smsRequest = new JSONObject();
smsRequest.put(VALUE, (JSONArray) jsonBody.get("smsRequest"));
smsRequest.put(KEY, "smsRequest");
updateObj.put(smsRequest);

Но, когда приведенный ниже код вызывается для обновления документа на couchbase, я получаю сообщение об ошибке.

operationObj.put("upsert", updateObj);

JSONObject cbMutateDocument = new JSONObject();       

cbMutateDocument.put("operations", operationObj);

newExchange.setProperty(BODY_PARAMETERS, cbMutateDocument.toString());

RouteHelper.sendRequest(getProducerTemplate(exchange), "cbservice://mutate?key="+keyId+"&bucket=notification", newExchange);

Пожалуйста, дайте мне знать, что здесь не так.

Ошибка:

2018-10-19 02:57:47,674 | ERROR | nsumer[smsQueue] | CBConnectorEndPointProcessor     |  |  |  |  |  |  |  |  | 383 - bil-cbconnector - 1.0.0.SNAPSHOT | Exception processing request :

com.couchbase.client.java.error.TranscodingException: Couldn't encode MutationSpec #1 (DICT_UPSERT on smsRequest) in 130f0781-925f-461d-8fe5-e53f1ca032c9

                at com.couchbase.client.java.transcoder.subdoc.AbstractByteArrayFragmentTranscoder.doEncodeSingle(AbstractByteArrayFragmentTranscoder.java:65)

                at com.couchbase.client.java.transcoder.subdoc.AbstractFragmentTranscoder.encodeWithMessage(AbstractFragmentTranscoder.java:50)

                at com.couchbase.client.java.subdoc.AsyncMutateInBuilder$2.call(AsyncMutateInBuilder.java:1245)

                at com.couchbase.client.java.subdoc.AsyncMutateInBuilder$2.call(AsyncMutateInBuilder.java:1232)

                at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)

                at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)

                at rx.Observable.unsafeSubscribe(Observable.java:10327)

                at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)

                at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)

                at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)

                at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)

                at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)

                at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)

                at rx.Observable.subscribe(Observable.java:10423)

                at rx.Observable.subscribe(Observable.java:10390)

                at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443)

                at rx.observables.BlockingObservable.single(BlockingObservable.java:340)

                at com.couchbase.client.java.subdoc.MutateInBuilder.execute(MutateInBuilder.java:280)

                at com.couchbase.client.java.subdoc.MutateInBuilder.execute(MutateInBuilder.java:103)

                at com.amdocs.cb.component.CBConnectorEndPointProcessor.processRequest(CBConnectorEndPointProcessor.java:441)

                at com.amdocs.cb.component.CBConnectorEndPointProcessor.process(CBConnectorEndPointProcessor.java:244)

                at org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:103)

                at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71)

                at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:205)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:119)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:105)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at com.amdocs.bl.common.util.RouteHelper.sendRequest(RouteHelper.java:35)

                at com.amdocs.bil.notificationdispatcher.NotificationDispatchPostProcessor.process(NotificationDispatchPostProcessor.java:207)[398:bil-notification-dispatcher:1.0.0.SNAPSHOT]

                at com.amdocs.bil.notificationdispatcher.processors.MessageProcessor.processMessage(MessageProcessor.java:81)[398:bil-notification-dispatcher:1.0.0.SNAPSHOT]

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_66]

                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_66]

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_66]

                at java.lang.reflect.Method.invoke(Method.java:497)[:1.8.0_66]

                at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:177)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.Throttler.process(Throttler.java:164)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.directvm.DirectVmProcessor.process(DirectVmProcessor.java:55)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.directvm.DirectVmProducer.process(DirectVmProducer.java:63)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:205)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:119)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:105)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at com.amdocs.bil.notificationdispatcher.processors.MessageProcessor.process(MessageProcessor.java:58)[398:bil-notification-dispatcher:1.0.0.SNAPSHOT]

                at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.processNext(AsyncProcessorConverterHelper.java:87)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:82)[231:org.apache.camel.camel-core:2.17.0.redhat-630329]

                at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:112)[243:org.apache.camel.camel-jms:2.17.0.redhat-630329]

                at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:555)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:515)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:485)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1103)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1095)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:992)[242:org.apache.servicemix.bundles.spring-jms:3.2.18.RELEASE_1]

                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_66]

                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_66]

                at java.lang.Thread.run(Thread.java:745)[:1.8.0_66]

Caused by: com.couchbase.client.deps.com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.codehaus.jettison.json.JSONArray and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3631)

                at com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:3022)

                at com.couchbase.client.java.transcoder.subdoc.JacksonFragmentTranscoder.writeValueAsBytes(JacksonFragmentTranscoder.java:80)

                at com.couchbase.client.java.transcoder.subdoc.AbstractByteArrayFragmentTranscoder.doEncodeSingle(AbstractByteArrayFragmentTranscoder.java:63)

                ... 83 more

Ответы [ 2 ]

0 голосов
/ 11 ноября 2018

Я нашел решение.

Мне пришлось создать пустой массив в документе Couchbase во время создания документа.Только тогда этот массив можно изменить с помощью операции ' arrayAppend '.

Во время создания

JSONObject cbDocument = new JSONObject(messageBody);
cbDocument.put("smsRequest", new JSONArray());
exchange.setProperty("bodyParameters", cbDocument.toString());
RouteHelper.sendRequest(producerTemplate, "cbservice://create?bucket=notification", exchange);

Во времяобновления

List<JSONObject> operations = new ArrayList<JSONObject>();

JSONArray smsRequestArray = (JSONArray)jsonBody.get("smsRequest");
                for(int i=0; i<smsRequestArray.length(); i++) {
                    JSONArray arrayAppendOperationArray = new JSONArray();
                    JSONObject smsArrayAppendOperationObject = new JSONObject();

                    JSONObject smsRequest = new JSONObject();
                    smsRequest.put(VALUE, smsRequestArray.optJSONObject(i));
                    smsRequest.put(KEY, "smsRequest");
                    arrayAppendOperationArray.put(smsRequest);
                    smsArrayAppendOperationObject.put("arrayAppend", arrayAppendOperationArray);
                    operations.add(smsArrayAppendOperationObject);
                }

JSONObject cbMutateDocument = new JSONObject();

    for(int i=0; i < operations.size(); i++) {
                        cbMutateDocument.put("operations", operations.get(i));
                        newExchange.setProperty(BODY_PARAMETERS, cbMutateDocument.toString());  
                        RouteHelper.sendRequest(getProducerTemplate(exchange), "cbservice://mutate?key="+keyId+"&bucket=notification", newExchange);
                    }
0 голосов
/ 20 октября 2018

Типы Java Couchbase называются JsonDocument, JsonArray и JsonObject, а не JSONDocument, JSONArray и JSONObject.Похоже, вы по ошибке используете другую библиотеку JSON?

...