← Back to Skills Marketplace
429668385

Clickhouse Database

by 429668385 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
104
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install clickhouse-database
Description
ClickHouse 数据库操作技能。通过 clickhouse-client CLI 连接数据库,执行 SELECT 查询、INSERT/UPDATE/DELETE 增删改、批量 SQL 执行、数据库/表管理、JSON 格式输出。适用场景:大数据查询、统计分析、数据导入导出、数据库巡检、表结构查看、远程连接、生...
README (SKILL.md)

ClickHouse Database Skill

Use the clickhouse-client CLI to connect to and interact with ClickHouse databases. Use the -q flag to execute SQL statements and combine with --format options to produce clean output suitable for processing. Pipe the result to jq for reliable JSON formatting.

快速使用场景

场景 1: 查询数据(最常用)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SELECT * FROM users LIMIT 10;" --format=JSONEachRow | jq -s '.'

场景 2: 查看表结构

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "DESCRIBE TABLE users;" --format=TSV 2>/dev/null

场景 3: 插入/更新/删除数据

# 插入
clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "INSERT INTO users (name, email) VALUES ('Test', '[email protected]');" 2>/dev/null

# 更新(需表引擎支持,如 MergeTree 家族)
clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "ALTER TABLE users UPDATE status=1 WHERE id=1;" 2>/dev/null

# 删除
clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "ALTER TABLE users DELETE WHERE id=1;" 2>/dev/null

场景 4: 统计数据报表

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SELECT COUNT(*) as total, SUM(amount) as revenue FROM orders WHERE toDate(create_time)=today();" --format=JSONEachRow | jq -s '.'

场景 5: 导出数据到文件

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SELECT * FROM users FORMAT CSV" > /tmp/users.csv 2>/dev/null

场景 6: 执行 SQL 脚本文件

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> --multiquery \x3C script.sql 2>/dev/null

数据库连接

基础连接

clickhouse-client -h \x3Chostname> --port \x3Cport> -u \x3Cusername> -d \x3Cdatabase-name>

示例 (连接本地数据库):

CLICKHOUSE_PASSWORD=yourpassword clickhouse-client -h 127.0.0.1 -u app_user -d app_db

从 JDBC URL 解析连接参数

用户可能提供 JDBC URL 格式:jdbc:clickhouse://host:port/database,需要解析为 clickhouse-client 参数:

jdbc:clickhouse://nexus.syrinxchina.com:8123/test3
  → -h nexus.syrinxchina.com --port 8123 -d test3
# 示例:从 JDBC URL 构建连接
JDBC_URL="jdbc:clickhouse://nexus.syrinxchina.com:8123/test3"
HOST=$(echo $JDBC_URL | sed -n 's/.*:\/\/\([^:]*\):\([0-9]*\)\/\(.*\)/\1/p')
PORT=$(echo $JDBC_URL | sed -n 's/.*:\/\/\([^:]*\):\([0-9]*\)\/\(.*\)/\2/p')
DB=$(echo $JDBC_URL | sed -n 's/.*:\/\/\([^:]*\):\([0-9]*\)\/\(.*\)/\3/p')
clickhouse-client -h "$HOST" --port "$PORT" -u root -d "$DB"

连接参数表

Option Description
-h / --host Hostname (default: localhost)
--port TCP port (default: 9000, HTTP port: 8123)
-u / --user Username (default: default)
--password Password (default: empty)
-d / --database Default database (default: default)
-q / --query Execute query and exit
--format Output format (TSV/CSV/JSON/JSONEachRow etc.)
--multiquery Allow multiple queries in one command
--secure Use SSL/TLS connection
--connect-timeout Connection timeout (seconds)
--send-timeout Send data timeout (seconds)
--receive-timeout Receive data timeout (seconds)

连接示例 (完整参数):

CLICKHOUSE_PASSWORD=password clickhouse-client -h 192.168.1.100 --port 9000 -u admin -d mydb --secure --connect-timeout=10 --format=JSONEachRow

使用配置文件

创建 ~/.clickhouse-client/config.xml 简化频繁连接:

\x3Cconfig>
  \x3Chost>127.0.0.1\x3C/host>
  \x3Cport>9000\x3C/port>
  \x3Cuser>app_user\x3C/user>
  \x3Cpassword>yourpassword\x3C/password>
  \x3Cdatabase>app_db\x3C/database>
  \x3Csecure>0\x3C/secure>
\x3C/config>
clickhouse-client --config ~/.clickhouse-client/config.xml -q "SELECT 1;" --format=JSONEachRow

数据操作

查询 (SELECT)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SELECT * FROM your_table LIMIT 5;" --format=JSONEachRow | jq -s '.'

推荐格式化模式:

  • --format=JSONEachRow:每行一个 JSON 对象,适合多行结果
  • --format=JSON:单个 JSON 对象包裹所有结果
  • --format=TSV:制表符分隔,适合简单输出

插入 (INSERT)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "INSERT INTO users (name, email) VALUES ('New User', '[email protected]');" 2>/dev/null

更新 (ALTER UPDATE)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "ALTER TABLE users UPDATE status = 'active' WHERE signup_date \x3C '2026-01-01';" 2>/dev/null

删除 (ALTER DELETE)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "ALTER TABLE sessions DELETE WHERE last_activity \x3C subtractDays(now(), 30);" 2>/dev/null

高级查询与 JSON 输出

统计摘要查询

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "
SELECT JSON_OBJECT(
  'total_users', (SELECT COUNT(*) FROM users),
  'active_users', (SELECT COUNT(*) FROM users WHERE status = 'active'),
  'avg_posts', (SELECT AVG(post_count) FROM user_stats)
) AS report;
" --format=JSON | jq .

通用 JSON 输出模式

单行结果:

clickhouse-client ... -q "SELECT JSON_OBJECT('key1', column1, 'key2', column2) FROM ..." --format=JSON | jq .

多行结果:

clickhouse-client ... -q "SELECT id, name FROM users LIMIT 5" --format=JSONEachRow | jq -s '.'

批量执行 SQL 文件

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> --multiquery \x3C script.sql 2>/dev/null

批量导入 CSV:

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "INSERT INTO my_table FORMAT CSV" \x3C data.csv 2>/dev/null

错误处理

常见错误码

Error Code Meaning Solution
516 Authentication failed 检查用户名/密码是否正确
81 Unknown database 检查数据库名是否存在
210 Can't connect to ClickHouse 检查 ClickHouse 服务是否启动,端口是否开放
60 Table doesn't exist 检查表名拼写是否正确

超时配置

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> --connect-timeout=5 --send-timeout=30 --receive-timeout=30 -q "SELECT * FROM large_table;" --format=TSV

连接测试模式

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SELECT 1 AS connected;" --format=TSV 2>&1 | grep -q "connected" && echo "连接成功" || echo "连接失败"

数据库与表操作

创建数据库

clickhouse-client -h \x3Chost> -u \x3Cuser> -q "CREATE DATABASE IF NOT EXISTS new_db ENGINE = Atomic;" --format=TSV

列出所有数据库

clickhouse-client -h \x3Chost> -u \x3Cuser> -q "SHOW DATABASES;" --format=TSV

列出表

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SHOW TABLES;" --format=TSV

查看表结构

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "DESCRIBE TABLE users;" --format=TSV

查看索引

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SHOW INDEXES FROM users;" --format=TSV

查看建表语句

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "SHOW CREATE TABLE users;" --format=TSV

DESCRIBE TABLE 详解

查看单表结构

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "DESCRIBE TABLE users;" --format=TSV

输出字段说明:

字段 说明
name 列名
type 数据类型(String、Int64、DateTime 等)
default_type 默认值类型
default_expression 默认值表达式
comment 字段注释
codec_expression 压缩算法
ttl_expression TTL 表达式

格式化输出为 JSON

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "
SELECT JSON_ARRAYAGG(JSON_OBJECT(
  'column', name,
  'type', type,
  'default_type', default_type,
  'default_value', default_expression,
  'comment', comment,
  'ttl', ttl_expression
)) AS columns
FROM system.columns
WHERE database = '\x3Cdatabase>' AND table = '\x3Ctable>'
ORDER BY position;" --format=JSON | jq .

快速查看主键和分区键

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "
SELECT name, type, is_in_primary_key, is_in_partition_key
FROM system.columns
WHERE database = '\x3Cdatabase>'
  AND table = '\x3Ctable>'
  AND (is_in_primary_key = 1 OR is_in_partition_key = 1)
ORDER BY is_in_primary_key DESC, position;" --format=JSONEachRow | jq -s '.'

EXPLAIN 查询分析(重要!)

分析 SELECT 查询执行计划:

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "EXPLAIN SELECT * FROM users WHERE phone = '13800138000';" --format=JSONEachRow | jq -s '.'

输出关键字段说明:

字段 说明
Expression 表达式计算
Filter 过滤条件
ReadFromStorage 存储读取方式
PrimaryKey 主键使用情况
Partition 分区过滤情况
Files 涉及文件数
Rows 预计扫描行数(越小越好)

优化要点:

  • 确认分区键被有效使用
  • 避免全表扫描(Full scan)
  • 检查主键是否命中

EXPLAIN ANALYZE(ClickHouse 21.1+)

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "EXPLAIN ANALYZE SELECT * FROM users WHERE phone = '13800138000';" --format=JSONEachRow | jq -s '.'

比 EXPLAIN 更详细,包含实际运行时间实际扫描行数执行步骤耗时

查看表大小和行数

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "
SELECT
  table AS table_name,
  total_rows AS rows,
  formatReadableSize(total_bytes) AS total_size,
  formatReadableSize(data_bytes) AS data_size,
  formatReadableSize(index_bytes) AS index_size
FROM system.tables
WHERE database = '\x3Cdatabase>'
ORDER BY total_bytes DESC;" --format=JSONEachRow | jq -s '.'

查看数据库中所有表的基本信息

clickhouse-client -h \x3Chost> -u \x3Cuser> -d \x3Cdb> -q "
SELECT
  name AS table,
  engine AS table_engine,
  total_rows AS rows,
  formatReadableSize(total_bytes) AS size,
  comment AS table_comment
FROM system.tables
WHERE database = '\x3Cdatabase>'
  AND engine NOT LIKE '%View%'
ORDER BY total_bytes DESC;" --format=JSONEachRow | jq -s '.'

环境变量配置

export CLICKHOUSE_PASSWORD="yourpassword"
export CLICKHOUSE_HOST="127.0.0.1"
export CLICKHOUSE_USER="app_user"
export CLICKHOUSE_DB="app_db"

clickhouse-client -h "$CLICKHOUSE_HOST" -u "$CLICKHOUSE_USER" -d "$CLICKHOUSE_DB" --password="$CLICKHOUSE_PASSWORD" -q "SELECT 1;" --format=JSONEachRow

完整示例脚本

#!/bin/bash
# 查询用户统计数据(带错误处理)

DB_HOST="${CLICKHOUSE_HOST:-127.0.0.1}"
DB_USER="${CLICKHOUSE_USER:-app_user}"
DB_PASS="${CLICKHOUSE_PASSWORD:-}"
DB_NAME="${CLICKHOUSE_DB:-app_db}"

QUERY="
SELECT JSON_OBJECT(
  'timestamp', now(),
  'summary', JSON_OBJECT(
    'total_users', (SELECT COUNT(*) FROM users),
    'active_users', (SELECT COUNT(*) FROM users WHERE status = 'active'),
    'new_today', (SELECT COUNT(*) FROM users WHERE toDate(created_at) = today())
  )
) AS report;
"

clickhouse-client -h "$DB_HOST" -u "$DB_USER" --password="$DB_PASS" -d "$DB_NAME" -q "$QUERY" --format=JSON 2>&1 | jq .

安全建议

  1. 禁止在命令行中直接写密码(进程列表可见)
  2. 使用 CLICKHOUSE_PASSWORD 环境变量或配置文件
  3. 生产环境强制使用 SSL (--secure 选项)
  4. 配置文件权限设置为 chmod 600 ~/.clickhouse-client/config.xml
  5. 查询操作使用只读账号
  6. 避免使用默认端口和默认用户名/密码

重要提示: 使用 --format 参数指定输出格式(如 TSV/JSON/JSONEachRow)确保 clickhouse-client 输出纯净数据,是生成有效 JSON 的前提。

Usage Guidance
This skill is coherent for ClickHouse administration, but take these precautions before installing/using it: (1) ensure clickhouse-client and jq are installed from trusted sources; (2) provide the minimal-privilege DB credentials possible (ideally a read-only user for query tasks); (3) avoid exposing high-privilege credentials in environment variables or plaintext config files—if you must, restrict file permissions and consider using a secrets manager; (4) be cautious when allowing the agent to run multiquery / ALTER / DELETE statements—review queries before execution; (5) because the skill is instruction-only it won't install code itself, but the agent invoking these shell commands will have the same access as your agent runtime—limit autonomous invocation or restrict env vars if you don't want the agent to run destructive operations.
Capability Analysis
Type: OpenClaw Skill Name: clickhouse-database Version: 1.0.0 The skill bundle provides a comprehensive set of instructions and examples for interacting with ClickHouse databases using the `clickhouse-client` CLI. It covers standard operations such as querying, data manipulation (INSERT/UPDATE/DELETE), schema management, and performance analysis (EXPLAIN). The documentation in SKILL.md includes security best practices, such as using environment variables for credentials and enabling SSL, and does not contain any evidence of malicious intent, data exfiltration, or unauthorized execution patterns.
Capability Assessment
Purpose & Capability
Name/description and the SKILL.md are consistent: all examples and guidance use clickhouse-client to run SELECT/INSERT/ALTER/DELETE, parse JDBC URLs, export/import CSV, and format JSON. Nothing requested or referenced is unrelated to ClickHouse database operations.
Instruction Scope
The SKILL.md contains explicit shell commands (clickhouse-client, sed, jq) and examples that read/write files (script.sql, data.csv, /tmp/users.csv) and a recommended config file (~/.clickhouse-client/config.xml). It also shows setting CLICKHOUSE_PASSWORD as an env var. These are expected for a DB helper, but the agent (or user following the instructions) can execute arbitrary SQL—including destructive ALTER/DELETE or multiquery—so credentials and invocation scope should be limited accordingly.
Install Mechanism
There is no install spec and no code files; this is instruction-only. That minimizes installation risk (nothing is downloaded or written by the skill itself). It does assume the host has clickhouse-client and jq installed.
Credentials
Registry metadata declares no required env vars or config paths, but SKILL.md examples reference CLICKHOUSE_PASSWORD and recommend a ~/.clickhouse-client/config.xml containing passwords. Requesting/using credentials is proportional for a DB skill, but users should be aware credentials may be provided as env vars or stored in a local config file (potentially plaintext).
Persistence & Privilege
Skill is not marked always:true and is user-invocable. It does not request persistent presence or modify other skills or system-wide settings.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install clickhouse-database
  3. After installation, invoke the skill by name or use /clickhouse-database
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
Initial release of clickhouse-database skill. - Enables ClickHouse database operations via the clickhouse-client CLI. - Supports querying, inserting, updating, deleting data, batch SQL execution, and database/table management. - Detailed examples included for data import/export, JSON output formatting, and troubleshooting. - Usage scenarios tailored for big data analytics, schema inspection, and production environment debugging. - Provides guides for both CLI parameters and config file usage, as well as error handling and connection testing.
Metadata
Slug clickhouse-database
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Clickhouse Database?

ClickHouse 数据库操作技能。通过 clickhouse-client CLI 连接数据库,执行 SELECT 查询、INSERT/UPDATE/DELETE 增删改、批量 SQL 执行、数据库/表管理、JSON 格式输出。适用场景:大数据查询、统计分析、数据导入导出、数据库巡检、表结构查看、远程连接、生... It is an AI Agent Skill for Claude Code / OpenClaw, with 104 downloads so far.

How do I install Clickhouse Database?

Run "/install clickhouse-database" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Clickhouse Database free?

Yes, Clickhouse Database is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Clickhouse Database support?

Clickhouse Database is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Clickhouse Database?

It is built and maintained by 429668385 (@429668385); the current version is v1.0.0.

💬 Comments