开发者

Where to write and store mongoDB map/reduce functions in java project

开发者 https://www.devze.com 2023-03-28 23:12 出处:网络
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?

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消