【发布时间】:2017-04-28 06:51:00
【问题描述】:
我有许多具有以下属性的大型一维 hdf5 数据集:
- 初始化大小 = (5201,),
- maxshape = (6000000,),
- dtype ='float32'
- 块 = (10000,)
- 压缩=“gzip”
- 路径示例:文件["Group"]["1"]["Group1"]["2"]["Dataset"]
我想将它们移动到 PostgreSQL 中,我处理了数据库的结构和插入数据,但是每次填充需要大约 650 秒的 72.4mb hdf5 文件,有人可以给我一个提示/建议如何提高性能?
我现在拥有的:
def fill_database(self, dog):
if isinstance(dog, h5py.Dataset):
name = dog.name.split('/')
table_name = '{}_{}'.format(name[3], name[5])
data = dog.value.astype(int).tolist()
self.cur.execute('CREATE TABLE IF NOT EXISTS {} (cur_id INT PRIMARY KEY , data INT[]);'.format(table_name))
self.cur.execute('INSERT INTO {} VALUES (%s, %s)'.format(table_name), (name[2], data))
if isinstance(dog, h5py.Group):
for k, v in dict(dog).items():
self.fill_database(v)
我尝试了什么:
import psycopg2
import h5py
from itertools import islice
with h5py.File('full_db.hdf5') as hdf5file:
with psycopg2.connect(database='hdf5', user='postgres', password='pass', port=5432) as conn:
cur = conn.cursor()
cur.execute('drop table if EXISTS mytable;')
cur.execute('create table mytable (data INT[]);')
chunksize = 10000
t = iter(hdf5file["Group"]["1"]["Group1"]["2"]["Dataset"][:].astype(int))
rows = islice(t, chunksize)
while rows:
statement = "INSERT INTO mytable(data) VALUES {}".format(rows) # I stuck here
cur.execute(statement)
rows = islice(t, chunksize)
conn.commit()
我还尝试在 PostgreSQL 中使用 LIMIT 和许多其他方式做一些事情,但我没有成功。
我认为有些问题可能是因为数据库中的数组,我将它们用于以后更方便的输出。
【问题讨论】:
标签: python postgresql psycopg2 hdf5 h5py