如何在Couchbase中上载JSONArray子文档

时间:2018-10-20 10:24:35

标签: java apache-camel couchbase n1ql

我正在尝试将JSONArray和其他一些字段向上插入到沙发床中的现有json文档中,但是在调用沙发床时出现错误。 请帮忙!

在这里,我正在设置将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());

现在,我要使用此json数组更新Couchbase文档

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

但是,当调用下面的代码来更新沙发上的文档时,我得到了一个错误。

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 :(得分:2)

Couchbase Java类型被命名为JsonDocument,JsonArray和JsonObject,而不是JSONDocument,JSONArray和JSONObject。看来您可能错误地使用了另一个JSON库?

答案 1 :(得分:0)

我找到了解决方法。

在创建文档时,我必须在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);
                    }