我正在寻找满足这些要求的Java NoSQL数据库:
true
以匹配文档。答案 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。