gpt4 book ai didi

java - 哪种 Java 的 NoSQL 数据库需要的设置最少?

转载 作者:行者123 更新时间:2023-12-02 08:44:29 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - 哪种 Java 的 NoSQL 数据库需要的设置最少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9716454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com