在java项目中编写和存储mongoDB map / reduce函数的位置 [英] Where to write and store mongoDB map/reduce functions in java project

查看:427
本文介绍了在java项目中编写和存储mongoDB map / reduce函数的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何建议,或者知道Java项目在何处以及如何编写和存储用于MongoDB数据库的javascript map / reduce函数的最佳实践?



我正在寻找的标准是:



在编写和编辑函数时,我希望IDE提供语法高亮和错误检查的好处(我正在使用Eclipse)。



如果可能的话,我不想在完成编辑后将函数复制到其他位置。 / p>

为了便于参考,版本控制等,我宁愿将函数存储在源代码中,而不是数据库本身。



你如何解决这个问题的任何例子都会很棒。



编辑:我不确定我是否已正确解释自己,所以这里是另一个去:



我不是在询问基本的资源管理。如果可能的话,我所追求的是一个工作环境,它可以让我在编辑就地功能时获得IDE的好处。如果我遗漏了一些令人眼花缭乱的东西,你将不得不原谅我。



我想避免的事情是你可能会看到的SQL,比如它的时候作为String存储在类文件中:

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

或在java属性文件中:

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

你必须输入一些无关的字符,比如s和\。你有把它写在其他地方,然后复制并粘贴进去并添加这些字符(或者你可能有一个工具为你做 - 你仍然必须这样做)。



由于Eclipse在用.js文件编写时验证了javascript,我不想做任何这样的事情。我想将这些函数存储在.js文件中,以便它们准备就绪在需要时可以通过map / reduce调用轻松消费。



有没有人这样做,或类似?我最初的想法只是为每个函数创建一个.js文件但是,一个只有匿名函数的.js文件在eclipse中验证失败,这使得它毫无意义 - 你必须将它分配给一个var - 这意味着它不是正确的形式供map / reduce使用。我想我可以创建仅包含函数内容的文件吗?但这一切听起来有点乱,我希望其他人可能遇到过这个问题,并且有一个简洁的解决方案。

解决方案

我使用的约定和我在大多数开源项目(包括Spring等人)中看到的是将所有非-java文件位于资源目​​录下的描述性目录下:

  module-root / 
src /
main /
java /
(Java源码到这里)
资源/
log4j.xml
spring /(spring xmls go here)
sql / (sql脚本到这里)
mongo /(< - 似乎是mongoDB函数的好地方)
test /
unit /
java /(单元测试java源码go这里)
资源/(单元测试特定资源 - 通常没有)
集成/
java /(集成测试java源码到这里)
资源/
spring /(通常一个ITestAssembly.xml在这里)

然后,构建文件将资源打包到资源jar中,该资源jar将与所有其他jar一起部署。


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.

这篇关于在java项目中编写和存储mongoDB map / reduce函数的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆