【问题标题】:Slow data retrieval in h5py dataset of key-value strings键值字符串的 h5py 数据集中的数据检索缓慢
【发布时间】:2020-04-26 04:28:40
【问题描述】:

给定以下 h5py 文件root -> group1 -> million key,val pairs:
检索任意数量的数据集(偶数 1 个)需要很长时间(约 10 秒),我想知道是否可以以不同的方式插入它们以控制该行为(我假设缓存对于我的用例来说太大了,但默认大小为 1MB)
行为如下:

script A

hdf5 = h5py.File(path_to_h5py, libver='latest',mode='a')
hdf5_dataet = hdf5.create_group(name_of_dataset)
for key, val in tqdm(dataset.items()):
    hdf5_dataet.create_dataset(json.dumps(key),data=json.dumps(val))

script B

f = h5py.File(path_to_h5py,'r')
data = f[name_of_dataset]
key_example = next(data.__iter__()) ---------> This takes 10 seconds

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

    HDF5 不像 Python 字典那样使用键/值对。数据结构更像 NumPy 数组。我不知道你最终想要做什么。 Script B 有一个更简单的迭代器。试试这个:

    h5f = h5py.File(path_to_h5py,'r')
    data = h5f[name_of_dataset]
    for key_example in data:
        print (key_example)
    

    于 2020 年 4 月 25 日添加简单测试以检查 I/O 性能:

    import h5py
    import time
    
    upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    lower = 'abcdefghijklmnopqrstuvwxyz'
    nums = '0123456789'
    
    with h5py.File('SO_61417130.h5','w') as h5w:
    
        nrows = 16
        nrpts = 100
    
        #vstr_dt = h5py.string_dtype(encoding='utf-8') # for h5py 2.10.0
        vstr_dt = h5py.special_dtype(vlen=str)   # for h5py 2.9.0
        vstr_ds = h5w.create_dataset('testvstrs', (nrpts*nrows,1), dtype=vstr_dt )
        print (vstr_ds.dtype, ":", vstr_ds.shape)    
    
        rcnt = 0
        for cnt1 in range(nrpts) :
            for cnt2 in range(nrows) :
                vstr_ds[rcnt]= ((cnt2+1)*(upper+lower+nums))
                rcnt +=1
    
        print (vstr_ds.dtype, ":", vstr_ds.shape)    
        print ('done writing')
    
        start = time.clock()
        for cnt in range(-nrows,0,1) :
            find_str = vstr_ds[cnt] 
            print (len(find_str[0]))
    
        print ('Elapsed time =', (time.clock() - start) )  
    

    【讨论】:

    • 访问第一个元素仍然需要大约 10 秒。这是唯一的问题,我假设从数据集中获取时间会快 100 倍
    • 读数据基本上是I/O。数据集和 row/key_example 有多大?多少内存?
    • 128GB 内存,每行大小小于 1MB(指向 ~1000 个字符的字符串),正如我所说,这没有意义:/
    • 如果我理解,您正在为每个 json 键创建 1 个数据集。内容是来自 json 值的字符串。只有 1 个字符串/行,对吗?它们是固定长度还是可变长度?如果您不知道,请检查 dtype。还要检查数据集块大小。
    • 是的,你是对的。数据集块大小是每个数据集对吗?因此,如果我有 1 个字符串/行,那么默认的块大小 (1MB) 不适合我吗?它们不是固定大小的,而且相对较长。
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2021-11-28
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多