Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe readable stream across writable stream #131

Open
SudhakarSA opened this issue Aug 16, 2022 · 3 comments
Open

Pipe readable stream across writable stream #131

SudhakarSA opened this issue Aug 16, 2022 · 3 comments

Comments

@SudhakarSA
Copy link

SudhakarSA commented Aug 16, 2022

Here is my table schema


eventDate          Date    
eventDateTime  DateTime
numericValue     UInt32  

And i am trying to write data from one table to another. Below is my code

let sql  = "select toDate(now()) as eventDate, now() as eventDateTime, 1 as numericValue";

const rs = connection.query(sql).stream();

const tf = new stream.Transform({
    objectMode : true,
    transform  : function (chunk, enc, cb) {

        cb(null, JSON.stringify(chunk));
    }
});

const ws = connection.insert('INSERT INTO demo.testing_table').stream();
const result = await rs.pipe(tf).pipe(ws).exec();

I am getting this error
Error:

Error: Cannot parse input: expected '\t' before: 'ate":"2022-08-16","eventDateTime":"2022-08-16 13:42:35","numericValue":1}': (at row 1)
    at getErrorObj (/home/ubuntu/development/git/atatus/atatus-ch-migration/node_modules/clickhouse/index.js:230:14)
    at IncomingMessage.<anonymous> (/home/ubuntu/development/git/atatus/atatus-ch-migration/node_modules/clickhouse/index.js:341:9)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 27
}
@TimonKK
Copy link
Owner

TimonKK commented Aug 20, 2022

In code

cb(null, JSON.stringify(chunk));

you pass data as JSON. But internal format is TSV. So If you pass like

cb(null, [chunk.eventDate, chunk.eventDateTime, data.numericValue].join('\t'));

it must work

@SudhakarSA
Copy link
Author

SudhakarSA commented Aug 22, 2022

Yes. this works for me. In docs, i see JSON.stringify instead of tab separated values as internal format is TSV

@Mauzzz0
Copy link

Mauzzz0 commented Dec 26, 2022

cb(null, [chunk.eventDate, chunk.eventDateTime, data.numericValue].join('\t'));

You forgot about '\n', full working code looks like this:

cb(null, [chunk.eventDate, chunk.eventDateTime, data.numericValue].join('\t') + '\n');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants