-
Notifications
You must be signed in to change notification settings - Fork 30
/
insert_file_stream_parquet.ts
64 lines (53 loc) · 1.62 KB
/
insert_file_stream_parquet.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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'
/** See also: https://clickhouse.com/docs/en/interfaces/formats#parquet-format-settings */
void (async () => {
const client = createClient()
const tableName = 'insert_file_stream_parquet'
await client.command({
query: `DROP TABLE IF EXISTS ${tableName}`,
})
await client.command({
query: `
CREATE TABLE ${tableName}
(id UInt64, name String, sku Array(UInt8))
ENGINE MergeTree()
ORDER BY (id)
`,
})
const filename = Path.resolve(cwd(), './node/resources/data.parquet')
const fileStream = Fs.createReadStream(filename)
/*
(examples) $ pqrs cat node/resources/data.parquet
############################
File: node/resources/data.parquet
############################
{id: 0, name: [97], sku: [1, 2]}
{id: 1, name: [98], sku: [3, 4]}
{id: 2, name: [99], sku: [5, 6]}
*/
await client.insert({
table: tableName,
values: fileStream,
format: 'Parquet',
clickhouse_settings: {
/** See also https://clickhouse.com/docs/en/interfaces/formats#parquet-format-settings.
* You could specify these (and other settings) here. */
},
})
const rs = await client.query({
query: `SELECT * from ${tableName}`,
format: 'JSONEachRow',
})
for await (const rows of rs.stream()) {
// or just `rows.json()`
// to consume the entire response at once
rows.forEach((row: Row) => {
console.log(row.json())
})
}
await client.close()
})()