File size: 4,398 Bytes
67d20c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# ClickHouse Database Connection Guide

## Connection Details
- **Host**: localhost
- **Port**: 9000 (HTTP), 9004 (HTTPS), 9009 (Native TCP)
- **Protocol**: HTTP/TCP (ClickHouse native)
- **Default Database**: default
- **Health Check**: `clickhouse-client --query "SELECT version()"`

## Authentication
- **Default User**: default (no password)
- **Access**: Localhost only, no authentication required
- **Security**: Development mode - add authentication for production

## ClickHouse-CLI Examples
```bash
# Connect to ClickHouse
clickhouse-client

# Show databases
SHOW DATABASES;

# Create nova database
CREATE DATABASE nova_analytics;

# Basic query
SELECT version(), now(), currentDatabase();

# Table operations
CREATE TABLE nova_analytics.events (
    timestamp DateTime,
    event_type String,
    data String
) ENGINE = MergeTree()
ORDER BY timestamp;

INSERT INTO nova_analytics.events VALUES 
(now(), 'session_start', '{"user": "test"}');

SELECT * FROM nova_analytics.events;
```

## HTTP API Examples
```bash
# Health check via HTTP
curl "http://localhost:8123/?query=SELECT%20version()"

# Create table via HTTP
curl -X POST "http://localhost:8123/?query=CREATE%20TABLE%20test%20(id%20UInt32)%20ENGINE%20%3D%20Memory"

# Insert data
curl -X POST "http://localhost:8123/?query=INSERT%20INTO%20test%20VALUES%20(1)%2C%20(2)%2C%20(3)"

# Query data
curl "http://localhost:8123/?query=SELECT%20*%20FROM%20test"
```

## Python Client Example
```python
from clickhouse_driver import Client

# Connect to ClickHouse
client = Client(
    host='localhost',
    port=9000,
    user='default',
    database='default'
)

# Execute queries
version = client.execute('SELECT version()')
print(f"ClickHouse version: {version[0][0]}")

# Create analytics table
client.execute('''
CREATE TABLE IF NOT EXISTS nova_analytics.metrics (
    timestamp DateTime,
    metric_name String,
    value Float64,
    tags String
) ENGINE = MergeTree()
ORDER BY (metric_name, timestamp)
''')

# Insert metrics data
client.execute('''
INSERT INTO nova_analytics.metrics VALUES
(now(), 'memory_usage', 75.5, '{"host": "vast1"}'),
(now(), 'cpu_usage', 45.2, '{"host": "vast1"}')
''')

# Query analytics
results = client.execute('''
SELECT 
    metric_name,
    avg(value) as avg_value,
    max(timestamp) as last_seen
FROM nova_analytics.metrics 
GROUP BY metric_name
''')

for row in results:
    print(f"Metric: {row[0]}, Avg: {row[1]:.2f}, Last: {row[2]}")
```

## Configuration Notes
- **Data Directory**: `/data/data/clickhouse/data/`
- **Log Directory**: `/data/data/clickhouse/logs/`
- **Max Memory**: 50GB (configurable)
- **Backup Location**: `/data/adaptai/backups/clickhouse/`
- **Port Configuration**: 
  - 8123: HTTP interface
  - 9000: Native TCP interface
  - 9004: HTTPS interface
  - 9009: Native TCP with SSL

## Performance Tuning
```sql
-- Monitor system metrics
SELECT * FROM system.metrics LIMIT 10;

-- Check query performance
SELECT 
    query,
    elapsed,
    read_rows,
    memory_usage
FROM system.processes;

-- Table sizes and parts
SELECT 
    database,
    table,
    sum(bytes) as size_bytes
FROM system.parts 
GROUP BY database, table
ORDER BY size_bytes DESC;
```

## Health Checks
```bash
# Basic connectivity
clickhouse-client --query "SELECT 1"

# System health
clickhouse-client --query "SELECT * FROM system.metrics WHERE metric LIKE '%memory%'"

# Disk usage
clickhouse-client --query "
SELECT 
    name,
    free_space,
    total_space,
    formatReadableSize(free_space) as free,
    formatReadableSize(total_space) as total
FROM system.disks
"

# Active queries
clickhouse-client --query "SELECT query, elapsed FROM system.processes"
```

## Security
- ❗ Localhost binding only
- ❗ No authentication configured (development mode)
- ❗ Add password authentication for production
- ❗ Monitor disk usage on /data partition
- ❗ Regular backups recommended
- ❗ Consider enabling SSL for encrypted connections

## Backup Procedures
```bash
# Create backup
clickhouse-client --query "BACKUP DATABASE nova_analytics TO '/data/adaptai/backups/clickhouse/nova_analytics_backup'"

# Restore backup
clickhouse-client --query "RESTORE DATABASE nova_analytics FROM '/data/adaptai/backups/clickhouse/nova_analytics_backup'"

# Manual file backup
sudo rsync -av /data/data/clickhouse/data/ /data/adaptai/backups/clickhouse/full_backup/
```

---
**Last Updated:** September 4, 2025