Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Thursday, 26 April 2012

Spring-MongoDB: debugging map\reduce on ubuntu.

while using Spring-data-MongoDB on ubuntu system, with default mongo installation you migh be experiencing troubles with error messages, as they might look similar to this:
InvalidDataAccessApiUsageException: Command execution failed:  Error [db assertion failure], Command = { "mapreduce" : ... }
The error message will include whole command, an will give you no clues about what actually went wrong. Try executing following command from terminal and repeat issue:
 tail -f /var/log/mongodb/mongodb.log 
trace information will get printed to log and you'll see it in terminal.  You should be looking for messages as following:

 JS Error: TypeError: myVar has no properties nofile_b:69
You can also add print to log in map\reduce functions with print("...") method invocation.
print('variable state is : ' + myVar) ;

 

Tuesday, 24 April 2012

Spring-Data-MongoDB passing scope variables to map\reduce

Spring-Data-MongoDB passing scope variables to map\reduce:

It's becoming a little bit tricky if you want to pass something like wrapper that contains Map of Map's as a parameter to map\reduce.

Anyhow, If you'll find yourself in trouble passing parameters to map\reduce here is one way to handle it:


MapReduceOptions options = MapReduceOptions.options();
Map vars = new HashMap();
        try {
            ObjectMapper mapper = new ObjectMapper();
            DBObject dbo = new BasicDBObject(mapper.convertValue(myobject, Map.class));
            vars.put("myparamName",dbo);
        } catch (Exception e) {
            logger.error(e);
        }
        options.scopeVariables(vars);
this way scope parameter will be passed as a pure JSON map object, which will be automatically mapped to js object by MongoDB.

ObjectMapper is a Jackson framework class.

Saturday, 21 April 2012

JSON serialization of JS objects with Spring.

JSON serialization of JS objects with Spring.

Problem: pass JS object from spring MVC enabled backend to HTML page, use eval (...) on object and invoke function that have been passed from backend.

Solution: By default Spring framework is using Jackson to serialize Java objects. During that serialization all members are passed as strings, while we'd like to have data structure as following:

{
    member1 : "value1",
    action : function() {
             ...
    }
}
with such an object it would be possible to do the following to invoke function:
 obj.action()
 In order to do that one would have to implement custom serializer class

public class MySerializer extends JsonSerializer<MyTO> {
    public void serialize(MyTO value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeObjectField("member1", value.getMember1());
        jgen.writeFieldName("action");
        jgen.writeRaw(":" + value.getAction());
    }
}

Done. use eval on front end to convert JSON into actual JS object and enjoy.