【问题标题】:Which NoSQL database for Java needs the least amount of setup?哪个适用于 Java 的 NoSQL 数据库需要最少的设置?
【发布时间】:2012-03-15 08:51:52
【问题描述】:

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

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

【问题讨论】:

  • 您可能希望指定您希望使用查询语言或 API 来查询 json 文档,因为如果查看答案似乎并不明显。

标签: java nosql


【解决方案1】:

对于最后可能的设置,您可以使用纯 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

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

【讨论】:

  • 我需要能够存储任何类型的 JSON。我的文档只是部分结构化,因此基于列/模式的方法没有意义。
  • 恕我直言,最简单的方法是将文档存储在文件中。它们可以像你喜欢的那样非结构化。由于您的数据集很小,我会在重新启动时将它们全部加载到内存中。您可以在内存中索引它们或强力搜索,但仍然非常快。您应该能够在 0.1 秒内扫描 10 MB 的文本。
【解决方案2】:

我已经成功使用了 H2 DB。非常快速且易于使用。应该符合你的要求,这里是feature comparison matrix

【讨论】:

  • 好吧,我想我可以通过将数据存储到 H2 CLOB 列中来编写自己的 NoSQL 数据库:-/
猜你喜欢
  • 2012-04-18
  • 2017-05-08
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多