You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/05/17 15:46:04 UTC

[jira] [Commented] (CAMEL-10728) Camel MongoDB Multiple Insert issue

    [ https://issues.apache.org/jira/browse/CAMEL-10728?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16014271#comment-16014271 ] 

ASF GitHub Bot commented on CAMEL-10728:
----------------------------------------

GitHub user NKame opened a pull request:

    https://github.com/apache/camel/pull/1701

    CAMEL-10728 fix handling of BasicDBList and allow better use of multi insert

    Context: camel-mongodb, multi insert. Handle the case where the input is a BasicDBList. Adds a configuration header to tell the component to avoid trying to convert the body to a DBObject when the user knows a multi insert is needed ("CamelMongoDbMultiInsert").
    
    See
    https://issues.apache.org/jira/browse/CAMEL-10504
    https://issues.apache.org/jira/browse/CAMEL-10728

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/NKame/camel master2

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/camel/pull/1701.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1701
    
----
commit 579a149fc1ce6cdee7331e89622ddfee74dcbf82
Author: Damien Bonvillain <d....@groupeonepoint.com>
Date:   2017-05-17T15:44:16Z

    CAMEL-10728 fix handling of BasicDBList and allow better use of multi insert.
    
    Context: camel-mongodb, multi insert. Handle the case where the input is a BasicDBList. Adds a configuration header to tell the component to avoid trying to convert the body to a DBObject when the user knows a multi insert is needed ("CamelMongoDbMultiInsert").
    
    See
    https://issues.apache.org/jira/browse/CAMEL-10504
    https://issues.apache.org/jira/browse/CAMEL-10728

----


> Camel MongoDB Multiple Insert issue
> -----------------------------------
>
>                 Key: CAMEL-10728
>                 URL: https://issues.apache.org/jira/browse/CAMEL-10728
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-mongodb
>    Affects Versions: 2.18.1
>            Reporter: Ankur Saxena
>            Priority: Minor
>
> I think there is a problem with multi-insert on MongoDB component as it fails to gracefully handle a BasicDBList. The logic below always sets the SingleInsert flag to true if the type converter is able to convert the exchange to a DBObject interface. This is implemented by BasicDBList and BasicDBObject.
> Further details below :
> I am trying to do a multiple insert using the camel mongo db component.
> My Pojo representation is :
> {code:java}
>  Person {
>  String firstName;
>  String lastName;
>  }
> {code}
> I have a processor which constructs a valid List of Person pojo and is a valid json structure. When this list of Person is sent to the mongodb producer , on invocation of createDoInsert the type conversion to BasicDBObject fails. This piece of code below looks to be the problem. Should it have more fall backs / checks in place to attempt the list conversion down further below as it fails on the very first cast itself. Debugging the MongoDbProducer the exchange object being received is a BasicDBList which implements DBObject. This causes the singleInsert flag to remain set at true which fails the insertion below as we get a BasicDBList instead of a BasicDBObject :
> {code:java}
>    if(singleInsert) {
>         BasicDBObject insertObjects = (BasicDBObject)insert;
>         dbCol.insertOne(insertObjects);
>         exchange1.getIn().setHeader("CamelMongoOid", insertObjects.get("_id"));
>     }
> {code}
> The Camel MongoDbProducer code fragment
> {code:java}
> private Function<Exchange, Object> createDoInsert() {
>     return (exchange1) -> {
>         MongoCollection dbCol = this.calculateCollection(exchange1);
>         boolean singleInsert = true;
>         Object insert = exchange1.getIn().getBody(DBObject.class);
>         if(insert == null) {
>             insert = exchange1.getIn().getBody(List.class);
>             if(insert == null) {
>                 throw new CamelMongoDbException("MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>");
>             }
>             singleInsert = false;
>             insert = this.attemptConvertToList((List)insert, exchange1);
>         }
>         if(singleInsert) {
>             BasicDBObject insertObjects = (BasicDBObject)insert;
>             dbCol.insertOne(insertObjects);
>             exchange1.getIn().setHeader("CamelMongoOid", insertObjects.get("_id"));
>         } else {
>             List insertObjects1 = (List)insert;
>             dbCol.insertMany(insertObjects1);
>             ArrayList objectIdentification = new ArrayList(insertObjects1.size());
>             objectIdentification.addAll((Collection)insertObjects1.stream().map((insertObject) -> {
>                 return insertObject.get("_id");
>             }).collect(Collectors.toList()));
>             exchange1.getIn().setHeader("CamelMongoOid", objectIdentification);
>         }
>         return insert;
>     };
> }
> {code}
> My route is as below :
> {code:xml}
>     <route id="uploadFile">
>     <from uri="jetty://http://0.0.0.0:9886/test"/>
>     <process ref="fileProcessor"/>
>     <unmarshal>
>         <csv>
>             <header>fname</header>
>             <header>lname</header>
>         </csv>
>     </unmarshal>
>     <process ref="mongodbProcessor" />
>     <to uri="mongodb:mongoBean?database=axs175&amp;collection=insurance&amp;operation=insert" />
> {code}
> and the MongoDBProcessor constructing the List of Person Pojo
> {code:java}
> @Component
> public class MongodbProcessor implements Processor {
> @Override
> public void process(Exchange exchange) throws Exception {
>     ArrayList<List<String>> personlist = (ArrayList) exchange.getIn().getBody();
>     ArrayList<Person> persons = new ArrayList<>();
>     for(List<String> records : personlist){
>         Person person = new Person();
>         person.setFname(records.get(0));
>         person.setLname(records.get(1));
>         persons.add(person);
>     }
> exchange.getIn().setBody(persons);
> }
> } 
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)