@apla/clickhouse

中文文档
1.6.4发布于 2020/09/13MITCommonJS零依赖近 30 天下载 2.3万

Yandex ClickHouse database interface

原始使用文档
文档目录12 节
  1. Synopsis
  2. API
  3. new ClickHouse(options: Options)
  4. clickHouse.query(query, [options], [callback])
  5. clickHouse.ping(callback)
  6. Promise interface
  7. clickHouse.querying(query, [options])
  8. clickHouse.pinging()
  9. How it works
  10. Bulk data loading with INSERT statements
  11. Memory size
  12. Examples

Simple and powerful interface for ClickHouse travis codecov

npm install @apla/clickhouse

Synopsis

const ClickHouse = require('@apla/clickhouse')
const ch = new ClickHouse({ host, port, user, password })

const stream = ch.query("SELECT 1", (err, data) => {})
stream.pipe(process.stdout)

// promise interface, not recommended for selects
// (requires 'util.promisify' for node < 8, Promise shim for node < 4)
await ch.querying("CREATE DATABASE test")

Examples:

API

new ClickHouse(options: Options)

Options

requireddefaultdescription
host✓Host to connect.
userAuthentication user.
passwordAuthentication password.
path (pathname)/Pathname of ClickHouse server.
port8123Server port number.
protocol'http:''https:' or 'http:'.
dataObjectsfalseBy default (false), you'll receive array of values for each row.
If you set dataObjects: true, every row will become an object with format: { fieldName: fieldValue, … }.
Alias to format: 'JSON'.
formatJSONCompactAdds the FORMAT statement for query if it did not have one.
Specifies format of selected or inserted data.
See "Formats for input and output data" to find out possible values.
queryOptionsObject, can contain any ClickHouse option from Settings, Restrictions and Permissions.
See example.
readonlyfalseTells driver to send query with HTTP GET method. Same as readonly=1 setting. More details.
timeout,
headers,
agent,
localAddress,
servername,
etc…
Any http.request or https.request options are also available.
Options example:
const ch = new ClickHouse({
  host: "clickhouse.msk",
  dataObjects: true,
  readonly: true,
  queryOptions: {
    profile: "web",
    database: "test",
  },
})

clickHouse.query(query, [options], [callback])

Sends a query statement to a server.

query: string

SQL query statement.

options: Options

The same Options, excluding connection options.

callback: (error, result) => void

Will be always called upon completion.

Returns: DuplexStream

It supports .pipe to process records.
You should have at least one error handler listening. Via query callback or via stream error event.

Stream eventDescription
'error'Query execution finished with error.
If you have both query callback and stream error listener, you'll have error notification in both listeners.
'metadata'When a column information is parsed.
'data'When a row is available.
'end'When entire response is processed.
Regardless of whether there is an 'end' listener, the query callback are always called.
You should always listen to 'data' event together with 'end' event.
"The 'end' event will not be emitted unless the data is completely consumed."
If you don't need to handle 'data' event prefer to use only callback or Promise interface.
stream.supplemental

After response is processed, you can read a supplemental response data from it, such as row count.

Examples:

clickHouse.ping(callback)

Sends an empty query. Doesn't requires authorization.

callback: (error, result) => void

Will be called upon completion.


Promise interface

Promise interface is not recommended for INSERT and SELECT queries.

  • INSERT can't do bulk load data with promise interface.
  • SELECT will collect entire query result in the memory. See the Memory size section.

With promise interface query result are parsed synchronously. This means that large query result in promise interface:

  • Will synchronously block JS thread/event loop.
  • May lead to memory leaks in your app due peak GC loads.

Use it only for queries where resulting data size is is known and extremely small.
The good cases to use it is DESCRIBE TABLE or EXISTS TABLE

clickHouse.querying(query, [options])

Similar to ch.query(query) but collects entire response in memory and resolves with complete query result.
See the Memory size section.

options: Options

The same Options, excluding connection options.

Returns: Promise

Will be resolved with entire query result.

Example of promise interface.

clickHouse.pinging()

Promise interface for .ping.

Returns: Promise

How it works

Bulk data loading with INSERT statements

INSERT can be used for bulk data loading. There is a 2 formats easily implementable with javascript: CSV and TabSeparated/TSV.

CSV is useful for loading from file, thus you can read and .pipe into clickhouse file contents.
To activate CSV parsing you should set format driver option or query FORMAT statement to CSV:


var csvStream = fs.createReadStream('data.csv')
var clickhouseStream = ch.query(statement, { format: CSV })

csvStream.pipe(clickhouseStream)

TSV is useful for loading from file and bulk loading from external sources, such as other databases. Only \\, \t and \n need to be escaped in strings; numbers, nulls, bools and date objects need some minor processing. You can send prepared TSV data strings (line ending will be appended automatically), buffers (always passed as is) or Arrays with fields.

Internally, every field will be converted to the format which ClickHouse can accept. Then escaped and joined with delimiter for the particular format. If you ever need to store rows (in arrays) and send preformatted data, you can do it.

ClickHouse also supports JSONEachRow format which can be useful to insert javascript objects if you have such recordset.

const stream = ch.query(statement, { format: 'JSONEachRow' })

stream.write(object) // Do write as many times as possible
stream.end() // And don't forget to finish insert query

Memory size

You can read all the records into memory in single call like this:


var ch = new ClickHouse({ host: host, port: port })
ch.querying("SELECT number FROM system.numbers LIMIT 10", (err, result) => {
  // result will contain all the data you need
})

In this case whole JSON response from the server will be read into memory, then parsed into memory hogging your CPU. Default parser will parse server response line by line and emits events. This is slower, but much more memory and CPU efficient for larger datasets.


Examples

Selecting with stream:

const readableStream = ch.query(
  'SELECT * FROM system.contributors FORMAT JSONEachRow',
  (err, result) => {},
)
const writableStream = fs.createWriteStream('./contributors.json')
readableStream.pipe(writableStream)

Inserting with stream:

const readableStream = fs.createReadStream('./x.csv')
const writableStream = ch.query('INSERT INTO table FORMAT CSV', (err, result) => {})
readableStream.pipe(writableStream)

Insert single row of data:

const ch = new ClickHouse(options)
const writableStream = ch.query(`INSERT INTO table FORMAT TSV`, (err) => {
  if (err) {
    console.error(err)
  }
  console.log('Insert complete!')
})

// data will be formatted for you
writableStream.write([1, 2.22, "erbgwerg", new Date()])

// prepare data yourself
writableStream.write("1\t2.22\terbgwerg\t2017-07-17 17:17:17")

writableStream.end()

Selecting large dataset:

const ch = new ClickHouse(options)
// it is better to use stream interface to fetch select results
const stream = ch.query("SELECT * FROM system.numbers LIMIT 10000000")

stream.on('metadata', (columns) => { /* do something with column list */ })

let rows = []
stream.on('data', (row) => rows.push(row))

stream.on('error', (err) => { /* handler error */ })

stream.on('end', () => {
  console.log(
    rows.length,
    stream.supplemental.rows,
    stream.supplemental.rows_before_limit_at_least, // how many rows in result are set without windowing
  )
})

Inserting large dataset:

const ch = new ClickHouse(options)
// insert from file
const tsvStream = fs.createReadStream('data.tsv')
const clickhouseStream = ch.query('INSERT INTO table FORMAT TSV')

tsvStream.pipe(clickhouseStream)

Settings for connection:

const ch = new ClickHouse({
  host: 'clickhouse.msk',
  queryOptions: {
    database: "test",
    profile: "web",
    readonly: 2,
    force_index_by_date: 1,
    max_rows_to_read: 10 * 1e6,
  },
})

Settings for query:

const ch = new ClickHouse({ host: 'clickhouse.msk' })
const stream = ch.query('INSERT INTO table FORMAT TSV', {
  queryOptions: {
    database: "test",
    insert_quorum: 2,
  },
})

Promise interface:

const ch = new ClickHouse(options)
// Check connection to server. Doesn't requires authorization.
await ch.pinging()
const { data } = await ch.querying("SELECT 1")
// [ [ 1 ] ]
const { data } = await ch.querying("DESCRIBE TABLE system.numbers", { dataObjects: true })
// [ { name: 'number', type: 'UInt64', default_type: '', default_expression: '' } ]

版本与发布记录

@apla/clickhouse 共发布 16 个版本,首次发布于 2016/07/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.6.4latest2020/09/136 年前
1.6.32020/05/186 年前
1.6.22020/03/296 年前
1.6.12019/10/057 年前
1.6.02019/05/097 年前
1.5.52019/02/177 年前

关于 @apla/clickhouse 的常见问题

@apla/clickhouse 是什么?

Yandex ClickHouse database interface

如何安装 @apla/clickhouse?

在项目目录运行 npm install @apla/clickhouse,或使用 pnpm add / yarn add / bun add。

@apla/clickhouse 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apla__clickhouse 是否存在。

@apla/clickhouse 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apla/clickhouse 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apla/clickhouse 可以商用吗?

@apla/clickhouse 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apla/clickhouse 的下载量有多少?

根据 npm 官方下载统计,@apla/clickhouse 在 2026/10/07 – 2026/11/05 这 30 天内被下载 22,664 次,日均约 755 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apla/clickhouse 还在维护吗?

最新版本 1.6.4 发布于 2020/09/13(6 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/07/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。