clickhouse export

This commit is contained in:
Jan Prochazka 2024-09-12 14:40:29 +02:00
parent e21c6d4872
commit 086bc0d9f3

View File

@ -77,9 +77,9 @@ const driver = {
let columnNames = null; let columnNames = null;
let dataTypes = null; let dataTypes = null;
const stream = resultSet.stream(); const strm = resultSet.stream();
stream.on('data', (rows) => { strm.on('data', (rows) => {
rows.forEach((row) => { rows.forEach((row) => {
const json = row.json(); const json = row.json();
if (!columnNames) { if (!columnNames) {
@ -102,11 +102,11 @@ const driver = {
}); });
}); });
stream.on('end', () => { strm.on('end', () => {
options.done(); options.done();
}); });
stream.on('error', (err) => { strm.on('error', (err) => {
options.info({ options.info({
message: err.toString(), message: err.toString(),
time: new Date(), time: new Date(),
@ -131,16 +131,55 @@ const driver = {
} }
}, },
// called when exporting table or view // called when exporting table or view
async readQuery(connection, sql, structure) { async readQuery(client, query, structure) {
const pass = new stream.PassThrough({ const pass = new stream.PassThrough({
objectMode: true, objectMode: true,
highWaterMark: 100, highWaterMark: 100,
}); });
// pass.write(structure) const resultSet = await client.query({
// pass.write(row1) query,
// pass.write(row2) format: 'JSONCompactEachRowWithNamesAndTypes',
// pass.end() });
let columnNames = null;
let dataTypes = null;
const strm = resultSet.stream();
strm.on('data', (rows) => {
rows.forEach((row) => {
const json = row.json();
if (!columnNames) {
columnNames = json;
return;
}
if (!dataTypes) {
dataTypes = json;
const columns = columnNames.map((columnName, i) => ({
columnName,
dataType: dataTypes[i],
}));
pass.write({
__isStreamHeader: true,
...(structure || { columns }),
});
return;
}
const data = _.zipObject(columnNames, json);
pass.write(data);
});
});
strm.on('end', () => {
pass.end();
});
strm.on('error', (err) => {
pass.end();
});
return pass; return pass;
}, },