이 튜토리얼에서는 CSV 및 Parquet 포맷의 Hacker News 데이터 2,800만 행을 ClickHouse 테이블에 삽입하고, 몇 가지 간단한 쿼리를 실행해 데이터를 살펴봅니다.
CSV
CSV 다운로드
데이터셋의 CSV 버전은 공개 S3 버킷에서 다운로드하거나, 다음 명령을 실행해 다운로드할 수 있습니다:
wget https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.csv.gz4.6GB, 2,800만 행 규모의 이 압축 파일은 다운로드에 5~10분 정도 소요됩니다.
데이터 샘플링
clickhouse-local을 사용하면
ClickHouse 서버를 배포하거나 구성하지 않고도 로컬 파일을 빠르게 처리할 수 있습니다.
ClickHouse에 데이터를 저장하기 전에 clickhouse-local로 파일을 샘플링해 보겠습니다. 콘솔에서 다음을 실행하십시오:
clickhouse-local다음으로, 데이터를 확인하려면 다음 명령을 실행하세요:
SELECT *
FROM file('hacknernews.csv.gz', CSVWithNames)
LIMIT 2
SETTINGS input_format_try_infer_datetimes = 0
FORMAT VerticalRow 1:
──────
id: 344065
deleted: 0
type: comment
by: callmeed
time: 2008-10-26 05:06:58
text: What kind of reports do you need?<p>ActiveMerchant just connects your app to a gateway for cc approval and processing.<p>Braintree has very nice reports on transactions and it's very easy to refund a payment.<p>Beyond that, you are dealing with Rails after all–it's pretty easy to scaffold out some reports from your subscriber base.
dead: 0
parent: 344038
poll: 0
kids: []
url:
score: 0
title:
parts: []
descendants: 0
Row 2:
──────
id: 344066
deleted: 0
type: story
by: acangiano
time: 2008-10-26 05:07:59
text:
dead: 0
parent: 0
poll: 0
kids: [344111,344202,344329,344606]
url: http://antoniocangiano.com/2008/10/26/what-arc-should-learn-from-ruby/
score: 33
title: What Arc should learn from Ruby
parts: []
descendants: 10이 명령에는 눈에 띄지 않지만 유용한 기능이 많이 있습니다.
file 연산자를 사용하면 포맷으로 CSVWithNames만 지정하여 로컬 디스크의 파일을 읽을 수 있습니다.
가장 중요한 점은 파일 내용에서 스키마가 자동으로 추론된다는 것입니다.
또한 clickhouse-local이 확장자를 바탕으로 gzip 포맷을 추론해 압축된 파일을 읽을 수 있다는 점에도 주목하십시오.
Vertical 포맷을 사용하면 각 컬럼의 데이터를 더 쉽게 확인할 수 있습니다.
스키마 추론으로 데이터 로드하기
데이터를 로드하는 가장 간단하면서도 강력한 도구는 clickhouse-client입니다. 기능이 풍부한 네이티브 command-line client입니다.
데이터를 로드할 때도 스키마 추론을 활용할 수 있으며, 컬럼의 타입은 ClickHouse가 결정합니다.
url 함수를 통해 원격 CSV 파일의 내용에 접근하여 테이블을 생성하고 데이터를 직접 삽입하려면, 다음 명령을 실행하세요.
스키마는 자동으로 추론됩니다:
CREATE TABLE hackernews ENGINE = MergeTree ORDER BY tuple
(
) EMPTY AS SELECT * FROM url('https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.csv.gz', 'CSVWithNames');이렇게 하면 데이터에서 자동으로 추론된 스키마를 사용해 빈 테이블을 생성합니다.
DESCRIBE TABLE 명령을 사용하면 할당된 타입을 확인할 수 있습니다.
DESCRIBE TABLE hackernews┌─name────────┬─type─────────────────────┬
│ id │ Nullable(Float64) │
│ deleted │ Nullable(Float64) │
│ type │ Nullable(String) │
│ by │ Nullable(String) │
│ time │ Nullable(String) │
│ text │ Nullable(String) │
│ dead │ Nullable(Float64) │
│ parent │ Nullable(Float64) │
│ poll │ Nullable(Float64) │
│ kids │ Array(Nullable(Float64)) │
│ url │ Nullable(String) │
│ score │ Nullable(Float64) │
│ title │ Nullable(String) │
│ parts │ Array(Nullable(Float64)) │
│ descendants │ Nullable(Float64) │
└─────────────┴──────────────────────────┴이 테이블에 데이터를 삽입하려면 INSERT INTO, SELECT 명령을 사용하십시오.
url 함수와 함께 사용하면 URL에서 데이터를 직접 스트리밍할 수 있습니다:
INSERT INTO hackernews SELECT *
FROM url('https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.csv.gz', 'CSVWithNames')명령 한 번으로 ClickHouse에 2,800만 개의 행을 성공적으로 삽입했습니다!
데이터 탐색하기
다음 쿼리를 실행하여 Hacker News 스토리와 특정 컬럼의 샘플을 확인합니다:
SELECT
id,
title,
type,
by,
time,
url,
score
FROM hackernews
WHERE type = 'story'
LIMIT 3
FORMAT VerticalRow 1:
──────
id: 2596866
title:
type: story
by:
time: 1306685152
url:
score: 0
Row 2:
──────
id: 2596870
title: WordPress capture users last login date and time
type: story
by: wpsnipp
time: 1306685252
url: http://wpsnipp.com/index.php/date/capture-users-last-login-date-and-time/
score: 1
Row 3:
──────
id: 2596872
title: Recent college graduates get some startup wisdom
type: story
by: whenimgone
time: 1306685352
url: http://articles.chicagotribune.com/2011-05-27/business/sc-cons-0526-started-20110527_1_business-plan-recession-college-graduates
score: 1스키마 추론은 초기 데이터 탐색에 매우 유용한 도구이지만, 「best effort」 방식이므로 데이터에 최적화된 스키마를 정의하는 것을 장기적으로 대체할 수는 없습니다.
스키마 정의
가장 명확하면서도 바로 적용할 수 있는 최적화 방법은 각 필드의 타입을 정의하는 것입니다.
시간 필드를 DateTime 타입으로 선언하는 것과 함께, 기존 데이터셋을 삭제한 뒤 아래 각 필드에도 적절한 타입을 지정합니다.
ClickHouse에서는 데이터의 프라이머리 키 ID를 ORDER BY 절로 정의합니다.
적절한 타입을 선택하고 ORDER BY
절에 포함할 컬럼을 결정하면 쿼리 속도와 압축 효율을 높이는 데 도움이 됩니다.
아래 쿼리를 실행하여 기존 스키마를 삭제하고 개선된 스키마를 생성하세요:
DROP TABLE IF EXISTS hackernews;
CREATE TABLE hackernews
(
`id` UInt32,
`deleted` UInt8,
`type` Enum('story' = 1, 'comment' = 2, 'poll' = 3, 'pollopt' = 4, 'job' = 5),
`by` LowCardinality(String),
`time` DateTime,
`text` String,
`dead` UInt8,
`parent` UInt32,
`poll` UInt32,
`kids` Array(UInt32),
`url` String,
`score` Int32,
`title` String,
`parts` Array(UInt32),
`descendants` Int32
)
ENGINE = MergeTree
ORDER BY id최적화된 스키마(schema)를 사용하면 이제 로컬 파일 시스템의 데이터를 삽입할 수 있습니다.
다시 clickhouse-client를 사용해, 명시적으로 INSERT INTO를 지정하고 INFILE 절로 파일의 데이터를 삽입합니다.
INSERT INTO hackernews FROM INFILE '/data/hacknernews.csv.gz' FORMAT CSVWithNames샘플 쿼리 실행
아래에 몇 가지 샘플 쿼리를 제공합니다. 직접 쿼리를 작성할 때 참고하시기 바랍니다.
Hacker News에서 "ClickHouse"는 얼마나 자주 언급되는 주제입니까?
score 필드는 스토리의 인기도를 나타내는 메트릭을 제공하며, id 필드와 ||
연결 연산자를 활용하면 원본 게시물의 링크를 생성할 수 있습니다.
SELECT
time,
score,
descendants,
title,
url,
'https://news.ycombinator.com/item?id=' || toString(id) AS hn_url
FROM hackernews
WHERE (type = 'story') AND (title ILIKE '%ClickHouse%')
ORDER BY score DESC
LIMIT 5 FORMAT VerticalRow 1:
──────
time: 1632154428
score: 519
descendants: 159
title: ClickHouse, Inc.
url: https://github.com/ClickHouse/ClickHouse/blob/master/website/blog/en/2021/clickhouse-inc.md
hn_url: https://news.ycombinator.com/item?id=28595419
Row 2:
──────
time: 1614699632
score: 383
descendants: 134
title: ClickHouse as an alternative to Elasticsearch for log storage and analysis
url: https://pixeljets.com/blog/clickhouse-vs-elasticsearch/
hn_url: https://news.ycombinator.com/item?id=26316401
Row 3:
──────
time: 1465985177
score: 243
descendants: 70
title: ClickHouse – high-performance open-source distributed column-oriented DBMS
url: https://clickhouse.yandex/reference_en.html
hn_url: https://news.ycombinator.com/item?id=11908254
Row 4:
──────
time: 1578331410
score: 216
descendants: 86
title: ClickHouse cost-efficiency in action: analyzing 500B rows on an Intel NUC
url: https://www.altinity.com/blog/2020/1/1/clickhouse-cost-efficiency-in-action-analyzing-500-billion-rows-on-an-intel-nuc
hn_url: https://news.ycombinator.com/item?id=21970952
Row 5:
──────
time: 1622160768
score: 198
descendants: 55
title: ClickHouse: An open-source column-oriented database management system
url: https://github.com/ClickHouse/ClickHouse
hn_url: https://news.ycombinator.com/item?id=27310247ClickHouse가 시간이 지남에 따라 노이즈가 증가하고 있습니까? 여기서 time 필드를 DateTime으로 정의하는 것의 유용성을 확인할 수 있습니다. 적절한 데이터 타입을 사용하면 toYYYYMM() 함수를 활용할 수 있습니다:
SELECT
toYYYYMM(time) AS monthYear,
bar(count(), 0, 120, 20)
FROM hackernews
WHERE (type IN ('story', 'comment')) AND ((title ILIKE '%ClickHouse%') OR (text ILIKE '%ClickHouse%'))
GROUP BY monthYear
ORDER BY monthYear ASC┌─monthYear─┬─bar(count(), 0, 120, 20)─┐
│ 201606 │ ██▎ │
│ 201607 │ ▏ │
│ 201610 │ ▎ │
│ 201612 │ ▏ │
│ 201701 │ ▎ │
│ 201702 │ █ │
│ 201703 │ ▋ │
│ 201704 │ █ │
│ 201705 │ ██ │
│ 201706 │ ▎ │
│ 201707 │ ▎ │
│ 201708 │ ▏ │
│ 201709 │ ▎ │
│ 201710 │ █▌ │
│ 201711 │ █▌ │
│ 201712 │ ▌ │
│ 201801 │ █▌ │
│ 201802 │ ▋ │
│ 201803 │ ███▏ │
│ 201804 │ ██▏ │
│ 201805 │ ▋ │
│ 201806 │ █▏ │
│ 201807 │ █▌ │
│ 201808 │ ▋ │
│ 201809 │ █▌ │
│ 201810 │ ███▌ │
│ 201811 │ ████ │
│ 201812 │ █▌ │
│ 201901 │ ████▋ │
│ 201902 │ ███ │
│ 201903 │ ▋ │
│ 201904 │ █ │
│ 201905 │ ███▋ │
│ 201906 │ █▏ │
│ 201907 │ ██▎ │
│ 201908 │ ██▋ │
│ 201909 │ █▋ │
│ 201910 │ █ │
│ 201911 │ ███ │
│ 201912 │ █▎ │
│ 202001 │ ███████████▋ │
│ 202002 │ ██████▌ │
│ 202003 │ ███████████▋ │
│ 202004 │ ███████▎ │
│ 202005 │ ██████▏ │
│ 202006 │ ██████▏ │
│ 202007 │ ███████▋ │
│ 202008 │ ███▋ │
│ 202009 │ ████ │
│ 202010 │ ████▌ │
│ 202011 │ █████▏ │
│ 202012 │ ███▋ │
│ 202101 │ ███▏ │
│ 202102 │ █████████ │
│ 202103 │ █████████████▋ │
│ 202104 │ ███▏ │
│ 202105 │ ████████████▋ │
│ 202106 │ ███ │
│ 202107 │ █████▏ │
│ 202108 │ ████▎ │
│ 202109 │ ██████████████████▎ │
│ 202110 │ ▏ │
└───────────┴──────────────────────────┘"ClickHouse"는 시간이 지날수록 인기가 높아지고 있는 것으로 보입니다.
ClickHouse 관련 기사에서 댓글을 가장 많이 작성한 사용자는 누구인가요?
SELECT
by,
count() AS comments
FROM hackernews
WHERE (type IN ('story', 'comment')) AND ((title ILIKE '%ClickHouse%') OR (text ILIKE '%ClickHouse%'))
GROUP BY by
ORDER BY comments DESC
LIMIT 5┌─by──────────┬─comments─┐
│ hodgesrm │ 78 │
│ zX41ZdbW │ 45 │
│ manigandham │ 39 │
│ pachico │ 35 │
│ valyala │ 27 │
└─────────────┴──────────┘어떤 댓글이 가장 많은 관심을 받습니까?
SELECT
by,
sum(score) AS total_score,
sum(length(kids)) AS total_sub_comments
FROM hackernews
WHERE (type IN ('story', 'comment')) AND ((title ILIKE '%ClickHouse%') OR (text ILIKE '%ClickHouse%'))
GROUP BY by
ORDER BY total_score DESC
LIMIT 5┌─by───────┬─total_score─┬─total_sub_comments─┐
│ zX41ZdbW │ 571 │ 50 │
│ jetter │ 386 │ 30 │
│ hodgesrm │ 312 │ 50 │
│ mechmind │ 243 │ 16 │
│ tosh │ 198 │ 12 │
└──────────┴─────────────┴────────────────────┘Parquet
ClickHouse의 강점 중 하나는 다양한 포맷을 처리할 수 있다는 점입니다. CSV는 상당히 이상적인 사용 사례를 보여주지만, 데이터 교환에는 가장 효율적인 방식이 아닙니다.
다음으로, 효율적인 컬럼 지향 포맷인 Parquet 파일에서 데이터를 로드합니다.
Parquet는 타입이 매우 제한적이며, ClickHouse는 이를 그대로 따라야 합니다. 그리고 이 타입 정보는 포맷 자체에 인코딩되어 있습니다. Parquet 파일에 대해 타입 추론을 수행하면 CSV 파일의 스키마와는 항상 약간 다른 스키마가 생성됩니다.
데이터 삽입
다음 쿼리를 실행하여 url 함수로 원격 데이터를 다시 읽되, 이번에는 Parquet 포맷으로 읽습니다:
DROP TABLE IF EXISTS hackernews;
CREATE TABLE hackernews
ENGINE = MergeTree
ORDER BY id
SETTINGS allow_nullable_key = 1 EMPTY AS
SELECT *
FROM url('https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.parquet', 'Parquet');
INSERT INTO hackernews SELECT *
FROM url('https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.parquet', 'Parquet');다음 명령을 실행하여 추론된 스키마를 확인하십시오:
DESCRIBE TABLE hackernews;┌─name────────┬─type───────────────────┬
│ id │ Nullable(Int64) │
│ deleted │ Nullable(UInt8) │
│ type │ Nullable(String) │
│ by │ Nullable(String) │
│ time │ Nullable(Int64) │
│ text │ Nullable(String) │
│ dead │ Nullable(UInt8) │
│ parent │ Nullable(Int64) │
│ poll │ Nullable(Int64) │
│ kids │ Array(Nullable(Int64)) │
│ url │ Nullable(String) │
│ score │ Nullable(Int32) │
│ title │ Nullable(String) │
│ parts │ Array(Nullable(Int64)) │
│ descendants │ Nullable(Int32) │
└─────────────┴────────────────────────┴이후 단계에서는 author, comment 등 더 명확한 컬럼명을 사용하므로, 수동으로 지정한 스키마를 사용합니다.
먼저 추론된 테이블을 삭제한 후 테이블을 생성하고, 공개 S3 버킷에서 직접 데이터를 삽입합니다:
DROP TABLE IF EXISTS hackernews;
CREATE TABLE hackernews
(
`id` UInt64,
`deleted` UInt8,
`type` String,
`author` String,
`timestamp` DateTime,
`comment` String,
`dead` UInt8,
`parent` UInt64,
`poll` UInt64,
`children` Array(UInt32),
`url` String,
`score` UInt32,
`title` String,
`parts` Array(UInt32),
`descendants` UInt32
)
ENGINE = MergeTree
ORDER BY (type, author);
INSERT INTO hackernews
SELECT * FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.parquet',
NOSIGN,
'Parquet',
'id UInt64,
deleted UInt8,
type String,
by String,
time DateTime,
text String,
dead UInt8,
parent UInt64,
poll UInt64,
kids Array(UInt32),
url String,
score UInt32,
title String,
parts Array(UInt32),
descendants UInt32');검색 속도를 높이기 위한 텍스트 인덱스 추가
"ClickHouse"를 언급한 댓글 수를 확인하려면 다음 쿼리를 실행하십시오.
SELECT count(*)
FROM hackernews
WHERE hasAnyTokens(lower(comment), 'clickhouse');┌─count()─┐
│ 1145 │
└─────────┘
1 row in set. Elapsed: 3.251 sec. Processed 28.74 million rows, 9.60 GB (8.84 million rows/s., 2.95 GB/s.)다음으로, 이 쿼리의 속도를 높이기 위해 comment 컬럼에 텍스트 인덱스를 생성합니다.
텍스트 인덱스는 토큰과 해당 토큰이 포함된 행을 매핑하는 역색인을 사용합니다.
splitByNonAlpha 토크나이저는 영숫자가 아닌 문자를 기준으로 텍스트를 분할합니다. 인덱스와 쿼리에서는 소문자 검색어와 함께 lower(comment)를 사용하므로 대소문자를 구분하지 않고 일치시킬 수 있습니다. 쿼리 표현식은 인덱싱된 표현식과 일치해야 합니다.
다음 명령어를 실행하여 인덱스를 생성합니다:
ALTER TABLE hackernews
ADD INDEX comment_idx lower(comment)
TYPE text(tokenizer = splitByNonAlpha);
ALTER TABLE hackernews
MATERIALIZE INDEX comment_idx
SETTINGS mutations_sync = 2;머티리얼라이즈하면 기존 데이터의 인덱스가 생성됩니다. mutations_sync 설정을 사용하면 머티리얼라이즈가 완료될 때까지 대기합니다.
system.data_skipping_indices 테이블에서 인덱스 정의를 확인할 수 있습니다.
인덱스가 구체화된 후 동일한 쿼리를 다시 실행합니다:
SELECT count(*)
FROM hackernews
WHERE hasAnyTokens(lower(comment), 'clickhouse');┌─count()─┐
│ 1145 │
└─────────┘
1 row in set. Elapsed: 0.019 sec. Processed 4.48 million rows, 4.48 MB (232.23 million rows/s., 232.23 MB/s.)결과는 동일합니다. 인덱스는 일치하는 행이 무엇인지가 아니라 ClickHouse가 일치하는 행을 찾는 방식을 변경하기 때문입니다. 인덱스가 적용된
쿼리는 훨씬 적은 데이터를 처리하므로 훨씬 빠르게 완료됩니다.
EXPLAIN을 사용하여 ClickHouse가 인덱스를 적용하도록 계획하는지 확인하십시오:
EXPLAIN indexes = 1
SELECT count(*)
FROM hackernews
WHERE hasAnyTokens(lower(comment), 'clickhouse');Output: count()
Aggregating
│ Keys:
│ Aggregates: count()
│ Skip merging: 0
└──Filter
│ Filter column: __text_index_comment_idx_hasAnyTokens_7b9491bd22343c822f64c95a9bda20f9
└──ReadFromMergeTree (default.hackernews)
Read type: Default
Parts: 4 | Granules: 547
Output: __text_index_comment_idx_hasAnyTokens_7b9491bd22343c822f64c95a9bda20f9
Indexes:
PrimaryKey
Condition: true
Parts: 4/4
Granules: 3527/3527
Skip
Name: comment_idx
Description: text GRANULARITY 100000000
Condition: (mode: Any; tokens: ["clickhouse"])
Parts: 4/4
Granules: 547/3527
Ranges: 437comment_idx 항목은 ClickHouse가 텍스트 인덱스를 적용할 계획임을 보여줍니다. 이 예시에서 계획은 3527개의
그래뉼 중 547개만 선택하므로 검사할 데이터 양이 크게 줄어듭니다.
여러 토큰 중 하나 이상 또는 모든 토큰을 검색할 수도 있습니다. 이러한 함수는 인덱스 토크나이저가 생성한 완전한 토큰과 일치합니다.
하나 이상의 토큰이 일치해야 하면 hasAnyTokens를 사용하세요:
SELECT count(*)
FROM hackernews
WHERE hasAnyTokens(lower(comment), 'oltp olap');┌─count()─┐
│ 2020 │
└─────────┘모든 토큰이 순서와 관계없이 일치해야 할 때는 hasAllTokens를 사용합니다:
SELECT count(*)
FROM hackernews
WHERE hasAllTokens(lower(comment), 'avx sve');┌─count()─┐
│ 22 │
└─────────┘