forked from mneedham/clickhouse-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
--- | ||
date: 2023-11-22 | ||
title: A simple nodeJS example using @clickhouse/client | ||
--- | ||
|
||
# A basic NodeJS example using @clickhouse/client | ||
|
||
Here is a basic code snippet file`main.ts`. | ||
|
||
`Package.json` (place it under `./`): | ||
|
||
```json | ||
{ | ||
"name": "a simple clickhouse client example", | ||
"version": "1.0.0", | ||
"main": "main.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"typescript": "^5.3.2" | ||
}, | ||
"dependencies": { | ||
"@clickhouse/client": "^0.2.6" | ||
} | ||
} | ||
``` | ||
|
||
`Main.ts` (place it under `./src`): | ||
|
||
```ts | ||
import { ClickHouseClient, createClient } from '@clickhouse/client'; // or '@clickhouse/client-web' | ||
|
||
interface ClickHouseResultSet<T> { | ||
meta: Meta[]; | ||
data: T[]; | ||
rows: number; | ||
statistics: Statistics; | ||
} | ||
|
||
interface Statistics { | ||
elapsed: number; | ||
rows_read: number; | ||
bytes_read: number; | ||
} | ||
|
||
interface Meta { | ||
name: string; | ||
type: string; | ||
} | ||
|
||
interface Count { | ||
c: number; | ||
} | ||
|
||
//Please replace client connection parameters like`host` | ||
//`username`, `passowrd`, `database` as needed. | ||
|
||
const initClickHouseClient = async (): Promise<ClickHouseClient> => { | ||
const client = createClient({ | ||
host: 'https://FQDN.aws.clickhouse.cloud', | ||
username: 'default', | ||
password: 'password', | ||
database: 'default', | ||
application: `pingpong`, | ||
}); | ||
|
||
console.log('ClickHouse ping'); | ||
if (!(await client.ping())) { | ||
throw new Error('failed to ping clickhouse!'); | ||
} | ||
console.log('ClickHouse pong!'); | ||
return client; | ||
}; | ||
|
||
const main = async () => { | ||
console.log('Initialising clickhouse client'); | ||
const client = await initClickHouseClient(); | ||
|
||
const row = await client.query({ | ||
query: `SELECT count() AS c FROM system.tables WHERE database='system'`, | ||
}); | ||
|
||
const jsonRow: ClickHouseResultSet<Count> = await row.json(); | ||
|
||
console.log(`I have found ${jsonRow.data[0].c} system tables!`); | ||
|
||
await client.close(); | ||
console.log(`π`); | ||
}; | ||
|
||
main(); | ||
``` | ||
|
||
To install the packages, run `yarn` from `./`: | ||
|
||
``` | ||
$ yarn | ||
yarn install v1.22.19 | ||
[1/4] π Resolving packages... | ||
[2/4] π Fetching packages... | ||
[3/4] π Linking dependencies... | ||
[4/4] π¨ Building fresh packages... | ||
β¨ Done in 0.14s. | ||
``` | ||
|
||
|
||
execute the code in `main.ts` from `./` with: | ||
``` | ||
$ npx ts-node src/main.ts | ||
``` | ||
|
||
will output: | ||
``` | ||
Initialising clickhouse client | ||
ClickHouse ping | ||
ClickHouse pong! | ||
I have found 120 system tables! | ||
π | ||
``` | ||
|