Python 的快速键值磁盘存储

问题描述 投票:0回答:3

我想知道是否有一个带有 Python 绑定的快速磁盘键值存储,它支持对单独键的数百万次读/写调用。我的问题涉及计算一个非常大的语料库(维基百科)中的单词共现,并不断更新共现计数。这涉及使用 64 位密钥和 64 位值读取和写入约 3 亿个值 70 次。

我还可以将我的数据表示为尺寸为 ~ 2M x 2M 的上三角稀疏矩阵。

到目前为止我已经尝试过:

  • Redis(64GB 内存不够大)
  • TileDB SparseArray(无法添加到值)
  • Sqlite(太慢了)
  • LMDB(批处理 3 亿个读/写事务需要多个小时才能执行)
  • Zarr(基于坐标的更新超级慢)
  • Scipy .npz(加法部分不能将矩阵保存在内存中)
  • 具有映射坐标和数据的稀疏 COO(添加矩阵时 RAM 使用量很大)

现在唯一运行良好的解决方案是 LMDB,但运行时间约为 12 天,这似乎不合理,因为我感觉我没有处理那么多数据。使用 .npz 将子矩阵(具有约 300M 值)保存到磁盘几乎是即时的。

有什么想法吗?

python arrays sparse-matrix key-value on-disk
3个回答
1
投票

你可能想看看我的项目。

pip install rocksdict

这是一个基于RockDB的快速磁盘键值存储,它可以将任何python对象作为值。我认为它可靠且易于使用。它具有与 GDBM 相当的性能,但与仅适用于 Linux 上的 python 的 GDBM 相比,它是跨平台的。

https://github.com/Congyuwang/RocksDict

下面是演示:

from rocksdict import Rdict, Options

path = str("./test_dict")

# create a Rdict with default options at `path`
db = Rdict(path)

db[1.0] = 1
db[1] = 1.0
db["huge integer"] = 2343546543243564534233536434567543
db["good"] = True
db["bad"] = False
db["bytes"] = b"bytes"
db["this is a list"] = [1, 2, 3]
db["store a dict"] = {0: 1}

import numpy as np
db[b"numpy"] = np.array([1, 2, 3])

import pandas as pd
db["a table"] = pd.DataFrame({"a": [1, 2], "b": [2, 1]})

# close Rdict
db.close()

# reopen Rdict from disk
db = Rdict(path)
assert db[1.0] == 1
assert db[1] == 1.0
assert db["huge integer"] == 2343546543243564534233536434567543
assert db["good"] == True
assert db["bad"] == False
assert db["bytes"] == b"bytes"
assert db["this is a list"] == [1, 2, 3]
assert db["store a dict"] == {0: 1}
assert np.all(db[b"numpy"] == np.array([1, 2, 3]))
assert np.all(db["a table"] == pd.DataFrame({"a": [1, 2], "b": [2, 1]}))

# iterate through all elements
for k, v in db.items():
    print(f"{k} -> {v}")

# batch get:
print(db[["good", "bad", 1.0]])
# [True, False, 1]
 
# delete Rdict from dict
del db
Rdict.destroy(path)

1
投票

看看 Plyvel,它是 LevelDB 的 python 接口。

我几年前就成功使用过,现在这两个项目貌似都还活跃。我自己的用例是存储数以百万计的键值对,我更关注读取性能,但它看起来也针对写入进行了优化。


-1
投票

PySpark 在这里更有用。

PairFunction<String, String, String> keyData =
  new PairFunction<String, String, String>() {
  public Tuple2<String, String> call(String x) {
    return new Tuple2(x.split(" ")[0], x);
  }
};

JavaPairRDD pairs = lines.mapToPair(keyData); https://www.oreilly.com/library/view/learning-spark/9781449359034/ch04.html

© www.soinside.com 2019 - 2024. All rights reserved.