所以我有以下代码应该将值批量插入Postgres:
config const batchsize = 10000;
proc bulkInsertSparseMatrix(con:Connection, aTable: string, fromField: string, toField: string, weightField: string, A) {
const q = "INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s);";
var cur = con.cursor();
var count = 0;
var buffer: batchsize*(string, string, string, string, int, int, real);
for ij in A.domain {
buffer[count] = (aTable, fromField, toField, weightField, ij(1), ij(2), A(ij));
count += 1;
if count == batchsize {
cur.execute(q, buffer);
count = 0;
}
}
cur.execute(q, buffer[0..#count]);
}
所以基本上是一个用户定义长度的元组缓冲区,其中我嵌套了值元组。缓冲区的填充由count
跟踪,并且条目定期提交,最后部分转储为完整性。没有什么花哨。这种嵌套元组方法的原因是CDO库的.execute()
方法是针对嵌套元组的格式化的,我大致遵循对此question的答案中的建议。
运行以下测试时出现问题:
config const DB_HOST: string = "localhost";
config const DB_USER: string = "postgres";
config const DB_NAME: string = "testing";
config const DB_PWD: string = "noether";
var con = PgConnectionFactory(host=DB_HOST, user=DB_USER, database=DB_NAME, passwd=DB_PWD);
config const batchsize: int = 3;
var fromField = "fromID",
toField = "toID",
wField = "w",
wTable = "tester";
var X = Matrix(
[1.0,0.0,1.0,0.0],
[1.0,0.0,1.0,1.0]);
bulkInsertSparseMatrix(con, aTable=wTable, fromField=fromField, toField=toField, weightField=wField, A=X);
我收到以下错误:
bulk_insert-test.chpl:36: error: unresolved call 'bulkInsertSparseMatrix(Connection, aTable=string, fromField=string, toField=string, weightField=string, A=[domain(2,int(64),false)] real(64))'
我认为这看起来像是一个类型错误,但我不知道我犯了什么罪。一些指导或指向更好的方法将不胜感激!