将Matlab浮点数组插入postgresql float []列

ala*_*rs2 2 database arrays postgresql matlab jdbc

我正在使用JDBC通过Matlab访问postgresql数据库,并且在尝试插入我希望存储为数组而不是单个值的值数组时已经挂起.我正在使用的Matlab代码如下:

insertCommand = 'INSERT INTO neuron (classifier_id, threshold, weights, neuron_num) VALUES     (?,?,?,?)';
statementObject = dbhandle.prepareStatement(insertCommand);
statementObject.setObject(1,1);
statementObject.setObject(2,output_thresholds(1));
statementObject.setArray(3,dbHandle.createArrayOf('"float8"',outputnodes(1,:)));
statementObject.setObject(4,1);

statementObject.execute;
close(statementObject);
Run Code Online (Sandbox Code Playgroud)

除了处理数组的行之外,一切都正常运行.对象outputnodes是一个<5x23>双矩阵,所以我试图将第一个<1x23>放入我的表中.

我已经为'"float8"'createArrayof调用的部分尝试了几种不同的名称和引号组合,但我总是得到这个错误:

??? Java exception occurred:
org.postgresql.util.PSQLException: Unable to find server array type for provided name     "float8".
at org.postgresql.jdbc4.AbstractJdbc4Connection.createArrayOf(AbstractJdbc4Connection.java:82)
at org.postgresql.jdbc4.Jdbc4Connection.createArrayOf(Jdbc4Connection.java:19)

Error in ==> Databasetest at 22
statementObject.setArray(3,dbHandle.createArrayOf('"float8"',outputnodes(1,:)));
Run Code Online (Sandbox Code Playgroud)

Pet*_*nov 5

JDBC连接器对阵列的性能

我想要指出的是,在必须导出包含数组的大量数据的情况下,JDBC可能不是最佳选择.首先,由于将原生Matlab数组转换为org.postgresql.jdbc.PgArray对象而导致的开销,其性能下降.其次,这可能导致Java堆内存不足(并且简单地增加Java堆内存大小可能不是万能的).这两点可以在下面的图片中看到,它说明了datainsertMatlab Database Toolbox中的方法的性能(它完全通过直接的JDBC连接与PostgreSQL一起工作):

数组的性能

蓝色图表显示batchParamExec来自PgMex库的命令性能(有关详细信息,请参阅https://pgmex.alliedtesting.com/#batchparamexec).红色图的端点对应于传递到数据库中的某个最大数据量,datainsert没有任何错误.大于该最大值的数据卷会导致"Java堆内存不足"问题(Java堆大小在图的顶部指定).有关实验的更多详细信息,请参阅以下 文章,其中包含数据插入的完整基准测试结果.

重做的例子

可以看出,基于libpq(官方C应用程序程序员与PostgreSQL的接口)的PgMex具有更高的性能,并且能够处理至少高达2Gb以上的卷.使用这个库中的代码可以被重写如下(我们假设下面所有被标注的参数<>迹象正确填写,该表neuron已经存在于数据库中,并有场classifier_idint4,thresholdfloat8,weightsfloat8[]neuron_numint4和,最后,该变量classfierIdVec,output_thresholds,outputnodesneuronNumVec是已定义的,并且在下面的代码中的注释示出的尺寸的数值阵列;在壳体中的类型的表中的字段的不同需要适当地修复代码的最后一个命令):

% Create the database connection
dbConn = com.allied.pgmex.pgmexec('connect',[...
    'host=<yourhost> dbname=<yourdb> port=<yourport> '...
    'user=<your_postgres_username> password=<your_postgres_password>']);

insertCommand = ['INSERT INTO neuron '...
    '(classifier_id, threshold, weights, neuron_num) VALUES ($1,$2,$3,$4)'];
SData = struct();
SData.classifier_id = classifierIdVec(:); % [nTuples x 1]
SData.threshold = output_thresholds(:); % [nTuples x 1]
SData.weights = outputnodes; % [nTuples x nWeights]
SData.neuron_num = neuronNumVec; % [nTuples x 1]
com.allied.pgmex.pgmexec('batchParamExec',dbConn,insertCommand,...
     '%int4 %float8 %float8[] %int4',SData);
Run Code Online (Sandbox Code Playgroud)

应该注意的是,outputnodes不需要沿着单独阵列上的行切割,因为后者具有相同的长度.在具有不同大小的不同元组的数组的情况下,必须将它们作为列单元阵列传递,每个单元包含用于每个元组的自己的数组.

编辑:目前PgMex有免费的学术许可.