哪个NoSQL数据库需要最少的Java设置?

时间:2012-03-15 08:51:53

标签: java nosql

我正在寻找满足这些要求的Java NoSQL数据库:

  • 完全嵌入(无需启动外部服务器)
  • 无需特殊设置;理想情况下,只需给它一个工作目录的路径即可。
  • 支持无模式或部分模式:用户必须能够向任何文档添加/删除特殊字段
  • 支持存储任何JSON文档(我认为这是给定的)
  • 数据库大小约为1-10MB
  • 查询将是JavaScript代码,返回true以匹配文档。
  • 在紧要关头,我想听听你的个人意见与你的选择一起工作是多么“轻松”

2 个答案:

答案 0 :(得分:3)

对于最后一次可能的设置,您可以使用普通Java完成所有操作。就个人而言,这是最容易学习/维护的。

您是否可以包含一些使NoSQL库必不可少的要求?

public class FileSystemNoSQL {
    private final File basePath;
    private final Map<String, String> documents = new TreeMap<String, String>();

    public FileSystemNoSQL(File basePath) {
        this.basePath = basePath;
        basePath.mkdirs();

        try {
            for (File file : basePath.listFiles()) {
                documents.put(file.getName(), FileUtils.readFileToString(file));
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    public String get(String key) {
        return documents.get(key);
    }

    public void put(String key, String content) {
        try {
            FileUtils.write(new File(basePath, key), content);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        documents.put(key, content);
    }

    public Map<String, String> findKeyContains(String text) {
        Map<String, String> set = new TreeMap<String, String>();
        for(Map.Entry<String, String> entry: documents.entrySet())
            if (entry.getKey().contains(text))
                set.put(entry.getKey(), entry.getValue());
        return set;
    }

    public Map<String, String> findContains(String text) {
        Map<String, String> set = new TreeMap<String, String>();
        for(Map.Entry<String, String> entry: documents.entrySet())
            if (entry.getKey().contains(text) || entry.getValue().contains(text))
                set.put(entry.getKey(), entry.getValue());
        return set;
    }

    public static void main(String ... args) {
        char[] spaces = new char[10240];
        Arrays.fill(spaces, ' ');
        String blank10k = new String(spaces);

        // build a database
        long start1 = System.nanoTime();
        FileSystemNoSQL fileSystemNoSQL1 = new FileSystemNoSQL(new File(System.getProperty("java.io.tmpdir"), "no-sql"));
        for(int i=0;i<1000;i++) {
            fileSystemNoSQL1.put("key: "+i, "value: "+i + blank10k);
        }
        long time1 = System.nanoTime() - start1;
        System.out.printf("Took %.3f seconds to build a database of 10 MB%n", time1 / 1e9);

        // reload the database
        long start2 = System.nanoTime();
        FileSystemNoSQL fileSystemNoSQL2 = new FileSystemNoSQL(new File(System.getProperty("java.io.tmpdir"), "no-sql"));
        long time2 = System.nanoTime() - start2;
        System.out.printf("Took %.3f seconds to load a database of 10 MB%n", time2/1e9);

        // perform queries
        long start3 = System.nanoTime();
        for(int i=0;i<1000;i++) {
            Map<String, String> contains = fileSystemNoSQL1.findKeyContains("key: " + i);
            if (contains.size() < 1) throw new AssertionError();
        }
        long time3 = System.nanoTime() - start3;
        System.out.printf("Took %.3f seconds to scan the keys of a database of 10 MB%n", time3/1e9);

        long start4 = System.nanoTime();
        for(int i=0;i<1000;i++) {
            Map<String, String> contains = fileSystemNoSQL1.findContains("value: " + i + ' ');
            if (contains.size() != 1) throw new AssertionError();
        }
        long time4 = System.nanoTime() - start4;
        System.out.printf("Took %.3f seconds to brute force scan of a database of 10 MB%n", time4/1e9);
    }
}

打印

Took 0.171 seconds to build a database of 10 MB
Took 0.088 seconds to load a database of 10 MB
Took 0.030 seconds to scan the keys of a database of 10 MB
Took 3.872 seconds to brute force scan of a database of 10 MB

进行暴力扫描是最糟糕的情况。您可以非常轻松地构建特定于应用程序的索引,这可以将时间缩短到亚毫秒级。

答案 1 :(得分:0)

我成功使用了H2 DB。非常快速且易于使用。它应符合您的要求,此处为feature comparison matrix