Comments (9)
Check https://www.postgresql.org/docs/9.2/static/sql-copy.html for documentation of the COPY command. Default delimiter is tab so you should be ok.
you problem probably comes from the fact that the last line should not terminate with a newline (otherwise, postgres thinks you want to add one more row, and is lost)
this is wrong :
var rowText = arr.join('\t') + '\n'
because it always add a '\n'
var rowText = '\n' + arr.join('\t')
would be better, but not for the first line where the '\n' should not be added
from node-pg-copy-streams.
I think there is a mismatch of things in your approach.
you should enter 'through2.obj' with row level data.
var started = false;
var internmap = through2.obj(function(arr, enc, cb) {
var rowText = (started ? '\n' : '') + arr.join('\t');
started = true;
//console.log(rowText)
cb(null, rowText);
})
interndataset.forEach(function(r) {
internmap.write(r);
})
internmap.end();
then make sure your table has 2 columns.
from node-pg-copy-streams.
Hi @jeronew,
thanks for heads up i was referring to old writable repo.
https://github.com/brianc/node-pg-writable/blob/master/index.js
i am quiet nascent in terms of modifying stream and also Copy command
Also even if i remove \n from below codebase:-
var rowText = arr.join('\t') + '\n'
to
var rowText = arr.join('\t')
the chunks from on data callback from internmap.pipe(stream)
is below and it is not getting inserted into database
Any pointers on same !
from node-pg-copy-streams.
you need the '\n' between each rows.
from node-pg-copy-streams.
hi @jeromew ,
extremely sorry missed your post content with newline '\n'
also even after appending newline first
var rowText = '\n' + arr.join('\t')
nothing is happening
I think there is some issue with my below code
internmap.write(data);
internmap.pipe(stream);
my overall code snippet
var sqlcopysyntax = 'COPY srt(starttime) FROM STDIN ';
var stream = client.query(copyFrom(sqlcopysyntax));
console.log(data);
var internmap = through2.obj(function(arr, enc, cb) {
var rowText = '\n' + arr.join('\t')
cb(null, rowText)
})
internmap.write(data)
internmap.pipe(stream);
stream.on('finish', function() {
resolve(0);
})
stream.on('error', function(err) {
console.log(err)
resolve(0);
})
internmap.on('finish', function() {
resolve(0);
})
internmap.on('data', function(chunk) {
console.log(chunk);
})
internmap.on('error', function(err) {
console.log(err)
resolve(0);
})
})
})
}
any pointers
thanks for patience !
from node-pg-copy-streams.
should be something like
var started = false;
var internmap = through2.obj(function(arr, enc, cb) {
var rowText = (started ? '\n' : '') + arr.join('\t');
started = true;
cb(null, rowText);
})
- maybe remove
internmap.on('data', function(chunk) {
console.log(chunk);
})
because this sometimes is greedy and consumes data before the stream pipeline is operational (cf documentation on flowing vs non-flowing streams)
-
also where do you
end
the stream ?internmap.write(data)
vs
internmap.write(data)
internmap.end()
is the stream is not ended, you cannot be sure data is flushed.
from node-pg-copy-streams.
Hi @jeromew
thank you so much i forgot to flush after write !
however , when i do that, I get below error
updated code snippet with sample dataset
var sqlcopysyntax = 'COPY intern(starttime) FROM STDIN ';
var stream = client.query(copyFrom(sqlcopysyntax));
console.log(sqlcopysyntax)
var interndataset = [
['0.382'],
['5.918'],
['19.554'],
['24.151'],
['25.507'],
['27.734'],
['27.734'],
['29.336'],
['31.71'],
];
var started = false;
var internmap = through2.obj(function(arr, enc, cb) {
var rowText = (started ? '\n' : '') + arr.join('\t');
started = true;
//console.log(rowText)
cb(null, rowText);
})
internmap.write(interndataset);
internmap.end();
internmap.pipe(stream);
internmap.on('data', function(chunk) {
console.log(chunk);
})
stream.on('finish', function() {
resolve(0);
})
stream.on('error', function(err) {
console.log(err)
resolve(0);
})
internmap.on('finish', function() {
resolve(0);
})
internmap.on('error', function(err) {
console.log(err)
resolve(0);
})
my postgres table ddl
CREATE TABLE public.intern (
starttime varchar NULL
)
WITH (
OIDS=FALSE
) ;
i think i might have some issues with array interndataset , since now it is single column moving forward it would multiple columns with ofcourse copy statement to two columns
var interndataset = [
['0.382','5.918'],
['5.918','5.918'],
['19.554','5.918'],
['24.151','5.918'],
['25.507','5.918'],
['27.734','5.918'],
['27.734','5.918'],
['29.336','5.918'],
['31.71','5.918'],
];
any pointers
from node-pg-copy-streams.
Hi @jeromew ,
got it working for single column entry ,now stuck up with multiple column entry
necessary modification in code snippets!
from
var rowText = (started ? '\n' : '') + arr.join('\t');
to this
var rowText = '\n' + arr.map( function( item ){ return ('\n' +item.join('\t')) } ).join('');
this works for single column inserts, but working on multi column insert like for
e.g -
var interndataset = [
['1', '4'],
['6', '12.074'],
['13.138', '16.183'],
['17.226', '21.605'],
['22.606', '24.733'],
['24.816', '27.027'],
];
gives
COPY intern (starttime, endtime) FROM STDIN
1 4
6 12.074
13.138 16.183
17.226 21.605
22.606 24.733
24.816 27.027
var sqlcopysyntax = 'COPY srt (starttime, endtime) FROM STDIN with delimiter \t ';
gives
error: syntax error at end of input
and below
var sqlcopysyntax = 'COPY srt (starttime, endtime) FROM STDIN with delimiter \'\\t\' ';
gives
error: COPY delimiter must be a single one-byte character
when i place
var sqlcopysyntax = 'COPY srt (starttime, endtime) FROM STDIN delimiters E\'\\t\'';
instead of this
var sqlcopysyntax = 'COPY intern(starttime) FROM STDIN ';
Any pointers on how to modify for more than one columns.
Any delimiter setting or code snippets changes ?
Thanks for your understanding and patience !
from node-pg-copy-streams.
hi @jeromew ,
you sir are lifesaver !
from node-pg-copy-streams.
Related Issues (20)
- memory usage test breaking in node v16 HOT 1
- COPY errors result in hangs or delayed exceptions HOT 3
- copy-from performance with lots of chunks HOT 15
- Question: is it possible to get the line number of the file where copy failed? HOT 5
- copyFrom stream's end function does not behave as expected HOT 6
- Cannot execute multiple COPY commands within the same transaction HOT 2
- Error: Release called on client which has already been released to the pool. HOT 2
- copy-from fails when large csv file(200K+ user rows) is copied from the file to the DB HOT 17
- Very different times on table with keys vs table without keys HOT 5
- `COPY FROM` fails when upgrading from v6.0.1 to v6.0.2+ HOT 43
- Documentation doesn't work with latest `pg` HOT 6
- Idle connections with "COMMIT" HOT 2
- Something seems wrong with binary transfer for COPY ... FROM STDIN BINARY HOT 8
- Not really an issue... HOT 1
- client.query is not returning a stream HOT 10
- Row count always off by one on CopyToStreamQuery HOT 8
- Copying data failing but no error triggered HOT 23
- help needed: concurrent requests HOT 6
- copyFrom: TypeError: this.cb_ReadyForQuery is not a function HOT 5
- COPY TO with query_timeout
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-pg-copy-streams.