I need to populate PG with a tsv file ~ 1.5 G. I plan to use streaming and pg-copy-stream and it worked for direct copy. Then I need to do some transformation and added a through pipe and it failed. I guess it is probably a buffer problem and someone must have done this.
The origin tsvfile.txt has the format
V1\tV2\tV3\tV4\n
V2\tV2\tV3\tV4\n
The code is
var fs = require('fs'), pg = require('pg'), es = require('es'), pgs = require('pg-copy-stream');
var filename = 'tsvfile.txt';
var pgkey = 'somepgkey';
pg.connect(pgkey, function(err, client, done){
var query = client.query(pgs.from('COPY table1 (C1, C2, C3, C4) FROM STDIN'));
var fstream = fs.createReadStream(filename);
fstream.pipe(es.split())
.pipe(es.mapSync(function(line){
var midline = line.split('\t').map(sometransform()).join('\t');
return midline + '\n';
//not sure \n is necessary here
}).pipe(query)
.on('end', done)
.on('err', somethingelse)
})
The error I got was
error: extra data after last expected column
, but works fine if I remove the first two pipes.