Вопрос

I am trying to run mapReduce using Spring Data Mongo as per the documentation

http://docs.spring.io/spring-data/data-mongodb/docs/1.0.0.M5/reference/html/

I have run into trouble loading the javascript files. When I include my map.js and reduce.js files the newlines are there for example \n\r\t etc. I used this online tool http://jscompress.com/ to compress the file into one line. The Files are then loaded but I get an error:

org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed: Error [ns doesn't exist],

Caused by: com.mongodb.CommandFailureException: { "serverUsed" : "/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "ns doesn't exist"} at com.mongodb.CommandResult.getException(CommandResult.java:71) at com.mongodb.CommandResult.throwOnError(CommandResult.java:110) at org.springframework.data.mongodb.core.MongoTemplate.handleCommandError(MongoTemplate.java:1814) ... 32 more

I have tried loading the files as strings and escaping the javascript. The same javascript works well using the mongo driver ie.

MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("product_data");
DBCollection catalogData = db.getCollection("catalogData");
MapReduceCommand cmd = new MapReduceCommand(catalogData, stringMap, stringReduce, "map_reduce_java_test", MapReduceCommand.OutputType.REPLACE, null); 
catalogData.mapReduce(cmd);

and this does not work:

mongoOperations.mapReduce("catalogData", stringMap, stringReduce, new MapReduceOptions().outputCollection("map_reduce_java_test")
        .finalizeFunction(stringFinalize).outputTypeReplace(),
    CatalogItem.class);

any help or ideas are appreciated.

Это было полезно?

Решение

I had a quick look at this. Unfortunately I couldn't reproduce your problem. Which Version of Spring Data MongoDB are you using? You are refering to a rather old version of Spring Data MongoDB the current version is 1.4.1: http://docs.spring.io/spring-data/data-mongo/docs/1.4.1.RELEASE/reference/html/mongo.core.html#mongo.mapreduce

The M/R code for the pure MongoDriver and Spring Data MongoDb doesn't seem to match, e.g. you are not using the finalizeFunction. Would you mind writing a test case that reproduces the issue?

You can use the MapReduceTests for that: https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java#L58

This is what I tried:

@Test
public void shoudSupportRunningCompressedJavaScriptAsMapReduceCommands() {

    createMapReduceData();

    MapReduceResults<ValueObject> results = mongoTemplate.mapReduce(
            "jmr1",
            mapFunction,
            reduceFunction,
            new MapReduceOptions().outputCollection("map_reduce_java_test").outputTypeReplace()
                    .finalizeFunction("function(key,reducedValue){return reducedValue}"), ValueObject.class);

    assertThat(results, is(notNullValue()));

    Map<String, Float> m = copyToMap(results);

    assertEquals(1, m.get("a").intValue());
    assertEquals(2, m.get("b").intValue());
    assertEquals(2, m.get("c").intValue());
    assertEquals(1, m.get("d").intValue());
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top