gpt4 book ai didi

java - 高性能的小数据本地数据存储

转载 作者:行者123 更新时间:2023-11-30 09:28:04 25 4
gpt4 key购买 nike

所以,这是我的场景。

我有数百万个事件进入,我不想将这些数据直接扔到我的数据库中。我想要一个“批处理”操作,其中 Java 代码保存进入的事件,直到它达到阈值(比如每 10 秒),然后它们对主数据库进行批量插入。

我还需要容错,因为如果机器崩溃,我不想丢失数据。我正在考虑使用 hsqldb 来保存这些事件(大约 10k)10 秒。

有什么建议吗?

最佳答案

如果您想要每秒处理数百万个整数并保持持久性,您可以尝试。 Java Chronicle您可以让不同的进程使用数据,因此如果您的程序终止,数据仍将写入数据库。 (此外,您的主进程也不会因为必须执行数据库更新而变慢)它还支持通过 TCP 复制到多台机器。

基于此测试的简单示例 HERE

 // create a Chronicle for reading or writing.
String basePath = TMP + File.separator + "deleteme.ict";
IndexedChronicle tsc = new IndexedChronicle(basePath);

// create a handle to excerpts in the chronicle.
Excerpt excerpt = tsc.createExcerpt();

// add 1024 entries.
int counter = 1;
for (int i = 0; i < 1024; i++) {
excerpt.startExcerpt(129);
for (int j = 0; j < 128; j += 8)
excerpt.writeLong(counter++);
excerpt.write(-1);
excerpt.finish();
}

// somewhere else read the file
int counter2 = 1;
Excerpt excerpt2 = tsc.createExcerpt();
while (excerpt2.nextIndex()) {
for (int j = 0; j < 128; j += 8) {
long actual = excerpt2.readLong();
long expected = counter2++;
if (expected != actual)
assertEquals(expected, actual);
}
assertEquals(-1, excerpt2.readByte());
excerpt2.finish();
}
assertEquals(counter, counter2);

这使您可以在它们可用时进行批处理,以最大限度地降低未添加到数据库的风险。当 nextIndex() 返回 false 时,您提交该批处理的事务,稍等片刻,然后重复。

关于java - 高性能的小数据本地数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14222839/

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