📊

ClickHouse

Li-database

Sebaka sa polokelo ea data se shebaneng le kholomo bakeng sa tlhahlobo ea nako ea sebele le ts'ebetso ea data e kholo

Lintlha tsa ho kenya

Ho romelloa ha basebetsi: 2-5 min
sehlopha: Li-database
RAM e tlase: 1024MB
Tšehetso: Litekete le imeile

Tseba lengolo lena

Tlhaloso

ClickHouse ke sistimi ea taolo ea database ea column e bulehileng, e etselitsoeng online analytical processing (OLAP), e fanang ka ts'ebetso ea lipotso e potlakileng haholo lipalong tse kholo tsa data. E ntlafalitsoe ke Yandex ho matlafatsa sebaka sa eona sa webo sa analytics, ClickHouse e ka sebetsa lilione tsa mela le li-terabyte tsa data ka linako tsa karabo tse ka tlase ho sekondi e le 'ngoe, e etsa hore e be e 'ngoe ea li-database tsa analytics tse potlakileng ka ho fetisisa tse fumanehang kajeno.

Sephiri sa ts'ebetso e kholo ea ClickHouse se ka boemong ba eona ba polokelo ea column. Ho fapana le li-database tse bolokang mela ka mokhoa oa mela e feletseng, ClickHouse e boloka kholomo e 'ngoe le e 'ngoe ka bo eona. Sena se bolela hore ha u botsa likholomo tse ikhethileng, database e bala feela data eo e e hlokang, e fokotsa haholo liketso tsa I/O 'me e etsa hore lipotso tsa analytics li be potlakile haholo. Ka compression e matla ea data, ClickHouse e ka fihlella litekanyo tsa compression tsa 10-100x ha e ntse e boloka lebelo la lipotso.

Dialect ea SQL ea ClickHouse e matla 'me e tsebahala ho mang kapa mang ea sebetsaneng le li-database tsa SQL, empa e lokisitsoe bakeng sa mesebetsi ea analytics. E tšehetsa liaggregation tse rarahaneng, window function, mesebetsi ea array, le li-JOIN tse rarahaneng lipakeng tsa li-row tse limilione tse ngata. Optimizer ea lipotso e arola mesebetsi ka boits'oanelo lipakeng tsa li-core tsa CPU, 'me e ka aba lipotso lipakeng tsa li-node tsa cluster bakeng sa ts'ebetso e phahameng le ho feta.

Database e ntlafetse data ea nako ea mohato oa nako le analytics ka nako ea sebele. Ho kenya data ho potlakile haholo, ka bokhoni ba ho kenya li-row tse limilione ka sekondi ho li-hardware tse tloaelehileng. Sena se etsa hore ClickHouse e loketse tlhahlobo ea log, ho latela liketsahalo, litsamaiso tsa ho sheba, le maemo afe kapa afe moo u hlokang ho kenya lichaneli tsa data tse potlakileng le ho li botsa ka potlako. Data e ka hlophisoa le ho aroloa ka letsatsi ka li-clause tsa ORDER BY le PARTITION BY tsa tafole, e lumella lipotso tsa nako tse atlehileng le ho laola data ka bolokehi ha e ntse e hola.

Moaho oa ClickHouse o arotsoe o atolosa ka holimo ho li-node tse makholo ntle le ho senya ts'ebetso ea lipotso. Replication e kahare ho tafole, e fana ka ho fumaneha ka mokhoa o phahameng le ho tšepahala. Sistimi e ka tsoela pele ho sebetsa le ha li-node li hloleha, e fetisetsa lipotso habonolo ho li-replica tse phetseng hantle. Mekhoa ea sharding e fapaneha, e u lumella ho aba data ho latela li-key tse ikhethileng ho ntlafatsa mekhoa ea lipotso tsa hau.

Bokhoni ba tšebelisano bo pharalitse, ka tšehetso ea mantlha ea ho bala data ho MySQL, PostgreSQL, MongoDB, Kafka, S3, HDFS, le mehloli e meng e mengata ka table engine. ClickHouse e ka sebetsa e le engine ea lipotso e arohaneng, e u lumella ho kopanya data lipakeng tsa lisistimi tse ngata tsa kantle ntle le li-pipeline tsa ETL. Sistimi e boetse e fana ka JDBC/ODBC driver, HTTP API, le li-client tsa protocol ea mantlha bakeng sa lipuo tsohle tse kholo tsa lenaneo.

Ha u aha li-dashboard tsa nako ea sebele, ha u sheba litsamaiso tsa marang-rang, kapa ha u sebetsana le data ea sensor ea IoT, ClickHouse e fana ka ts'ebetso le bokhoni bo hlokahalang bakeng sa mesebetsi ea analytics ea sejoale-joale ntle le bothata le litšenyehelo tsa litharollo tsa boiketsetso tse tšoanang.

Likarolo tsa bohlokoa

Injene ea Polokelo ea Column

Moaho o thehiloeng holim'a column o nang le compression e matla o fana ka katleho ea polokelo ea 10-100x le ts'ebetso e hlollang ea lipotso

Lipotso tse Potlakileng Haholo

Sebetsa lilione tsa mela ka li-millisecond ka ts'ebetso ea lipotso e sebelisang vector le ts'ebetso e iketsang ea ho sebetsa ka nako e le 'ngoe

Ho Kenya Data ka Nako ea Sebele

Kenya li-row tse limilione ka sekondi ka ho fumaneha ka potlako ha lipotso bakeng sa analytics ka nako ea sebele

Ho Atoloha ka Mokhoa o Otlolohileng

Moaho o arotsoeng o atoloha ho li-node tse makholo ka sharding le replication e iketsang

SQL e nang le Likarolo tse Ntlafetseng

Sintakse e tloaelehileng ea SQL e nang le window function, li-array, likaho tsa data tse kenelletsoeng, le mekhoa e lekanyetsang

Likopano tse ngata

Li-connector tsa mantlha bakeng sa Kafka, S3, MySQL, PostgreSQL, MongoDB, HDFS, le bokhoni ba lipotso tse arohaneng

Mehlala ea ho sebelisa

• Web Analytics: Hlahloba data ea clickstream, lipalo tsa leqephe, le boitšoaro ba basebedisi liketsahalong tse limilione tse ngata ka nako ea sebele
• Application Performance Monitoring: Boloka le botsa li-log, metric, le li-trace bakeng sa observability le ho rarolla mathata
• Business Intelligence: Fana ka matla ho li-dashboard tse sebelisanang le analytics ea potlako ho data e kholo, ka linako tsa karabo tse ka tlase ho sekondi e le 'ngoe
• Tlhahlobo ea Nako ea Mohato: Sheba litsamaiso tsa marang-rang, sensara tsa IoT, mebaraka ea lichelete, le likhohlano tsa data tse nang le nako e hlakileng
• Ho Kopanya Li-log: Kopanya li-log tsa application, liketsahalo tsa tšireletso, le li-audit trail, ka ho batla le tlhahlobo e potlakileng
• Tlhahlobo ea E-commerce: Hlahloba boitšoaro ba bareki, ts'ebetso ea lihlahisoa, le mekhoa ea thekiso lithekisong tse limilione tse ngata
• Ad Tech le Marketing: Tlhahlobo ea li-bid ka nako ea sebele, ho latela ts'ebetso ea likhampeine, le ho etsa mohlala oa attribution ka bongata

Litaelo tsa ho kenya

Kenyo ho Ubuntu 22.04 LTS:

1. Kenya Repository ea ClickHouse:
sudo apt update
sudo apt install -y apt-transport-https ca-certificates dirmngr

# Add GPG key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754

# Add repository
echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee /etc/apt/sources.list.d/clickhouse.list
2. Kenya Seva ea ClickHouse le Client:
sudo apt update
sudo apt install -y clickhouse-server clickhouse-client

# Set default password during installation (or leave blank for no password)
3. Qala Tšebeletso ea ClickHouse:
sudo systemctl start clickhouse-server
sudo systemctl enable clickhouse-server
sudo systemctl status clickhouse-server
4. Lokisa Phihlello ea Kantle:
sudo nano /etc/clickhouse-server/config.xml
Fumana 'me u tlose ho khetholla (kapa u kenye):
<listen_host>0.0.0.0</listen_host>
Qala tšebeletso hape:
sudo systemctl restart clickhouse-server
5. Theha Mosebelisi oa Admin:
# Connect to ClickHouse
clickhouse-client

# Create user
CREATE USER admin IDENTIFIED BY 'secure_password';
GRANT ALL ON *.* TO admin WITH GRANT OPTION;
6. Hlahloba Ho Kenya:
# Connect with client
clickhouse-client --user admin --password secure_password

# Run test query
SELECT 'Hello, ClickHouse!' AS message;

# Create test table
CREATE TABLE test_table (
    id UInt32,
    name String,
    timestamp DateTime
) ENGINE = MergeTree()
ORDER BY (id, timestamp);

# Insert test data
INSERT INTO test_table VALUES (1, 'Alice', now()), (2, 'Bob', now());

# Query data
SELECT * FROM test_table;
7. Lokisa Basebedisi (Tlhahisong):
sudo nano /etc/clickhouse-server/users.xml
Eketsa mosebedisi ea ikhethetseng:
<users>
    <myuser>
        <password_sha256_hex><!-- SHA256 hash of password --></password_sha256_hex>
        <networks>
            <ip>10.0.0.0/8</ip>
            <ip>192.168.0.0/16</ip>
        </networks>
        <profile>default</profile>
        <quota>default</quota>
    </myuser>
</users>
8. Beha ZooKeeper bakeng sa Replication (Ea Khetho):
sudo apt install -y zookeeper zookeeperd

# Configure in config.xml
sudo nano /etc/clickhouse-server/config.xml
Eketsa litlhophiso tsa ZooKeeper:
<zookeeper>
    <node>
        <host>zk1.example.com</host>
        <port>2181</port>
    </node>
    <node>
        <host>zk2.example.com</host>
        <port>2181</port>
    </node>
    <node>
        <host>zk3.example.com</host>
        <port>2181</port>
    </node>
</zookeeper>
9. Hlophisa Firewall:
sudo ufw allow 9000/tcp  # Native protocol
sudo ufw allow 8123/tcp  # HTTP interface
sudo ufw allow 9009/tcp  # Interserver communication
10. Netefatsa Ho Kenya:
# Check version
clickhouse-server --version

# Check listening ports
sudo netstat -tlnp | grep clickhouse

# View logs
sudo tail -f /var/log/clickhouse-server/clickhouse-server.log

Litlhahiso tsa ho kenya

Litlhophiso tse Hlokahalang:

1. Litlhophiso tsa Seva (/etc/clickhouse-server/config.xml):
- <listen_host>: Liaterese tsa IP tseo ho mamelwang ho tsona (0.0.0.0 bakeng sa tsohle)
- <http_port>: Port ea sebopeho sa HTTP (ea kamehla 8123)
- <tcp_port>: Port ea protocol ea mantlha (ea kamehla 9000)
- <max_connections>: Palo e kholo ea likhokahano tse sebetsang ka nako e le 'ngoe
- <max_concurrent_queries>: Ho laola palo ea lipotso tse sebetsang ka nako e le 'ngoe

2. Ho Lokisa Memori le Ts'ebetso:
<max_server_memory_usage>80000000000</max_server_memory_usage>  <!-- 80GB -->
<max_memory_usage>40000000000</max_memory_usage>  <!-- 40GB per query -->
<max_bytes_before_external_group_by>20000000000</max_bytes_before_external_group_by>
<max_threads>8</max_threads>  <!-- Parallel query threads -->
3. Litlhophiso tsa Polokelo:
<storage_configuration>
    <disks>
        <default>
            <path>/var/lib/clickhouse/</path>
        </default>
        <fast_ssd>
            <path>/mnt/fast_ssd/clickhouse/</path>
        </fast_ssd>
        <slow_hdd>
            <path>/mnt/slow_hdd/clickhouse/</path>
        </slow_hdd>
    </disks>
    <policies>
        <hot_and_cold>
            <volumes>
                <hot>
                    <disk>fast_ssd</disk>
                </hot>
                <cold>
                    <disk>slow_hdd</disk>
                </cold>
            </volumes>
            <move_factor>0.2</move_factor>
        </hot_and_cold>
    </policies>
</storage_configuration>
4. Mehlala ea ho Theha Litafole:
-- MergeTree (basic)
CREATE TABLE events (
    event_date Date,
    event_time DateTime,
    user_id UInt32,
    event_type String,
    url String
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, user_id)
SETTINGS index_granularity = 8192;

-- ReplicatedMergeTree (with replication)
CREATE TABLE events_replicated (
    event_date Date,
    event_time DateTime,
    user_id UInt32,
    event_type String
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/events', '{replica}')
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, user_id);

-- Distributed table (cluster)
CREATE TABLE events_distributed AS events_replicated
ENGINE = Distributed(cluster_name, database_name, events_replicated, rand());
5. Mehlala ea Mefuta ea Data le Compression:
CREATE TABLE metrics (
    timestamp DateTime CODEC(Delta, ZSTD),
    metric_name LowCardinality(String),
    value Float64 CODEC(Gorilla, ZSTD),
    tags Array(String),
    metadata Map(String, String)
) ENGINE = MergeTree()
ORDER BY (metric_name, timestamp);
6. Ntlafatso ea Lipotso:
-- Use PREWHERE for filtering (faster than WHERE)
SELECT * FROM events
PREWHERE event_date >= today() - 7
WHERE user_id IN (SELECT id FROM users WHERE premium = 1);

-- Materialized views for pre-aggregation
CREATE MATERIALIZED VIEW events_hourly
ENGINE = SummingMergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, event_hour, event_type)
AS SELECT
    toDate(event_time) AS event_date,
    toHour(event_time) AS event_hour,
    event_type,
    count() AS event_count
FROM events
GROUP BY event_date, event_hour, event_type;
7. Polokelo ea Data le TTL:
-- Automatic data expiration
ALTER TABLE events
MODIFY TTL event_date + INTERVAL 90 DAY;

-- Move old data to cold storage
ALTER TABLE events
MODIFY TTL 
    event_date + INTERVAL 7 DAY TO VOLUME 'hot',
    event_date + INTERVAL 30 DAY TO VOLUME 'cold',
    event_date + INTERVAL 90 DAY DELETE;
8. Lipotso tsa ho Sheba:
-- System metrics
SELECT * FROM system.metrics;

-- Current queries
SELECT query_id, user, query, elapsed FROM system.processes;

-- Query log analysis
SELECT 
    query_duration_ms,
    read_rows,
    read_bytes,
    memory_usage,
    query
FROM system.query_log
WHERE type = 'QueryFinish'
ORDER BY query_duration_ms DESC
LIMIT 10;

-- Table sizes
SELECT 
    database,
    table,
    formatReadableSize(sum(bytes)) AS size,
    sum(rows) AS rows
FROM system.parts
WHERE active
GROUP BY database, table
ORDER BY sum(bytes) DESC;
Mekhoa e Molemo ka ho Fetisisa:
- Sebelisa injene ea MergeTree bakeng sa malapa a litafole tsa tlhahiso
- Khetha ORDER BY ka hloko, e ama ts'ebetso ea lipotso
- Arola ka letsatsi bakeng sa data ea nako ea mohato oa nako (e lumella TTL le ho hlakola ho sebetsang)
- Sebelisa LowCardinality bakeng sa likholomo tsa litekanyo tsa string tse nang le litekanyo tse ka tlase ho 10K tse ikhethileng
- Nolofalletsa li-codec tsa compression (Delta, Gorilla, ZSTD) bakeng sa mefuta e ikhethileng ea data
- Theha materialized view bakeng sa liaggregation tse sebelisoang khafetsa
- Sebelisa litafole tse arohaneng bakeng sa deployment ea li-node tse ngata
- Sheba ts'ebetso ea lipotso ka system.query_log
- Beha max_memory_usage e loketseng ho thibela OOM
- Etsa OPTIMIZE TABLE khafetsa bakeng sa mesebetsi ea merge (empa e sebelise ka tlhokomelo)

Likopo tsa theknoloji

Litaelo tsa sistimi

  • Khopolo: 1024MB RAM

Lekanya Sengoloa sena

★★★★★
-
Ho kenya...

Na o se o loketse ho kenya kopo ya hao? ClickHouse?

Qala ka metsotsoana ka ts'ebetso ea rona e bonolo ea ho kenya VPS

Ho kenya ka ho ka bang 2 metsotso • Full root ho kena • Thuto ka terene le lengolo tsoibila