2

Does anyone have any advice, or know of any best practice for where and how in a Java project to write and store javascript map/reduce functions for use with a MongoDB database?

The criteria I'm looking for are:

When writing and editing the functions I'd like the benefit of syntax highlighting and error checking provided by an IDE (I'm using Eclipse).

I don't want to have to copy the functions to a different location when I'm finished editing them, if possible.

I'd rather store the functions in the source code than the DB itself, for ease of reference, version control, etc.

Any examples of how you've solved this problem would be great.

EDIT: I'm not sure I've explained myself properly, so here's another go:

I'm not asking about basic resource management. What I'm after is, if possible, a working environment that allows me the benefits of my IDE while editing the functions 'in place'. You'll have to excuse me if I'm missing something blindingly obvious.

What I want to avoid are things such as you might see with SQL, such as when its stored as a String in a class file:

private static final String MAP_FUNC = 
    "function() { " +
    "   emit(this.id, {total : this.total}); " +
    "};";

or in a java properties file:

map.func=function() {
\       emit(this.id, {total : this.total});
\   };

where you have to enter a load of extraneous characters such as "s and \s. You have to write it somewhere else, then copy and paste it in and add these characters in (or you might have a tool that does it for you - you still have to do it).

Since javascript is validated by Eclipse when it's written in .js files, I don't want to have to do any of this. I'd like to store the functions in .js files in such a way as they are ready for easy consumption by a map/reduce call when needed.

Does anyone do this, or similar? My initial idea was just to create a single .js file for each function, however a .js file with nothing but an anonymous function in it fails validation in eclipse, which makes it pointless - you have to assign it to a var - which means its not in the correct form for consumption by map/reduce. I suppose I could create files of just the content of the functions? But all this sounds a bit messy, and I was hoping someone else might have come across the problem, and have a neat solution.

2 Answers 2

2

The convention I use and I've seen in most open source projects (including Spring et al) is to put all non-java files under a resources directory, under a descriptive directory:

module-root/
    src/
        main/
            java/
                (Java sources go here)
            resources/
                log4j.xml
                spring/ (spring xmls go here)
                sql/ (sql scripts go here)
                mongo/ (<-- seems like a good place for mongoDB functions)
        test/
            unit/
                java/ (unit test java sources go here)
                resources/ (unit test specific resources - usually none)
            integration/
                java/ (integration test java sources go here)
                resources/
                    spring/ (usually an ITestAssembly.xml goes here)

The buildfile then packs the resources into a resource jar, which gets deployed along wiht all the other jars.

Sign up to request clarification or add additional context in comments.

Comments

0

We place the JS files as application resources in the (Eclipse) project and then upon application initialisation those files are read by the Java apps (either web app or daemons in our case), stored in a singleton management class and then used as needed when invoking, say, a map/reduce.

It's relatively straightforward resource management.

1 Comment

Maybe I'm missing something obvious here, or maybe I didn't phrase my question well enough. What I'm looking for is an easy way to create and edit map/reduce functions, getting all the benefits of an IDE, and being able to save them 'in place' to be used as a resource by the application. Straightforward resource management I understand - what I'm after is streamlining the process of creating and editing those resources. For example, each function could be saved in a separate .js file. But (in Eclipse) the javascript validation won't allow a file that consists of: function() { ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.