-
Notifications
You must be signed in to change notification settings - Fork 30
/
insert_file_stream_csv.ts
50 lines (44 loc) · 1.29 KB
/
insert_file_stream_csv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { createClient } from '@clickhouse/client'
import type { Row } from '@clickhouse/client-common'
import Fs from 'fs'
import { cwd } from 'node:process'
import Path from 'path'
void (async () => {
const client = createClient()
const tableName = 'insert_file_stream_csv'
await client.command({
query: `DROP TABLE IF EXISTS ${tableName}`,
})
await client.command({
query: `
CREATE TABLE ${tableName}
(id UInt64, name String, flags Array(UInt8))
ENGINE MergeTree()
ORDER BY (id)
`,
})
// contains data as 1,"foo","[1,2]"\n2,"bar","[3,4]"\n...
const filename = Path.resolve(cwd(), './node/resources/data.csv')
const fileStream = Fs.createReadStream(filename)
await client.insert({
table: tableName,
values: fileStream,
format: 'CSV',
clickhouse_settings: {
/** See also: https://clickhouse.com/docs/en/interfaces/formats#csv-format-settings.
* You could specify these (and other settings) here. */
},
})
const rs = await client.query({
query: `SELECT * from ${tableName}`,
format: 'CSV',
})
for await (const rows of rs.stream()) {
// or just `rows.text()`
// to consume the entire response at once
rows.forEach((row: Row) => {
console.log(row.text)
})
}
await client.close()
})()