このトピックでは、ClickHouse クラスター内の各ノードのローカルテーブルにランダムモードでデータをすばやく書き込む方法について説明します。
前提条件
ClickHouse クラスターが作成されていること。詳細については、ClickHouse クラスターの作成をご参照ください。
手順
SSH モードで ClickHouse クラスターにログオンします。詳細については、クラスターへのログオンをご参照ください。
次のコマンドを実行して、公式のサンプル データセットをダウンロードします。
curl https://datasets.clickhouse.com/hits/tsv/hits_v1.tsv.xz | unxz --threads=`nproc` > hits_v1.tsv次のコマンドを実行して、ClickHouse クライアントを起動します。
clickhouse-client -h core-1-1 -m説明サンプルコマンドでは、core-1-1 はログオンするコアノードの名前を示します。複数のコアノードがある場合は、いずれかのノードにログオンできます。
次のコマンドを実行して、データベースを作成します。
on CLUSTER パラメーターを含む次のコマンドを実行して、クラスター内のすべてのノードのデータベースを作成できます。デフォルトでは、クラスターの名前は cluster_emr です。
CREATE DATABASE IF NOT EXISTS demo on CLUSTER cluster_emr;次の出力が返されます。

クラスター内のすべてのノードのレプリケートされたテーブルを作成します。
レプリケートされたテーブルは、クラスター内の各ノードのテーブルレプリカの数と同じ数のデータレプリカを生成し、データはすべてのテーブルレプリカで一貫性があります。
CREATE TABLE demo.hits_local ON CLUSTER cluster_emr ( `WatchID` UInt64, `JavaEnable` UInt8, `Title` String, `GoodEvent` Int16, `EventTime` DateTime, `EventDate` Date, `CounterID` UInt32, `ClientIP` UInt32, `ClientIP6` FixedString(16), `RegionID` UInt32, `UserID` UInt64, `CounterClass` Int8, `OS` UInt8, `UserAgent` UInt8, `URL` String, `Referer` String, `URLDomain` String, `RefererDomain` String, `Refresh` UInt8, `IsRobot` UInt8, `RefererCategories` Array(UInt16), `URLCategories` Array(UInt16), `URLRegions` Array(UInt32), `RefererRegions` Array(UInt32), `ResolutionWidth` UInt16, `ResolutionHeight` UInt16, `ResolutionDepth` UInt8, `FlashMajor` UInt8, `FlashMinor` UInt8, `FlashMinor2` String, `NetMajor` UInt8, `NetMinor` UInt8, `UserAgentMajor` UInt16, `UserAgentMinor` FixedString(2), `CookieEnable` UInt8, `JavascriptEnable` UInt8, `IsMobile` UInt8, `MobilePhone` UInt8, `MobilePhoneModel` String, `Params` String, `IPNetworkID` UInt32, `TraficSourceID` Int8, `SearchEngineID` UInt16, `SearchPhrase` String, `AdvEngineID` UInt8, `IsArtifical` UInt8, `WindowClientWidth` UInt16, `WindowClientHeight` UInt16, `ClientTimeZone` Int16, `ClientEventTime` DateTime, `SilverlightVersion1` UInt8, `SilverlightVersion2` UInt8, `SilverlightVersion3` UInt32, `SilverlightVersion4` UInt16, `PageCharset` String, `CodeVersion` UInt32, `IsLink` UInt8, `IsDownload` UInt8, `IsNotBounce` UInt8, `FUniqID` UInt64, `HID` UInt32, `IsOldCounter` UInt8, `IsEvent` UInt8, `IsParameter` UInt8, `DontCountHits` UInt8, `WithHash` UInt8, `HitColor` FixedString(1), `UTCEventTime` DateTime, `Age` UInt8, `Sex` UInt8, `Income` UInt8, `Interests` UInt16, `Robotness` UInt8, `GeneralInterests` Array(UInt16), `RemoteIP` UInt32, `RemoteIP6` FixedString(16), `WindowName` Int32, `OpenerName` Int32, `HistoryLength` Int16, `BrowserLanguage` FixedString(2), `BrowserCountry` FixedString(2), `SocialNetwork` String, `SocialAction` String, `HTTPError` UInt16, `SendTiming` Int32, `DNSTiming` Int32, `ConnectTiming` Int32, `ResponseStartTiming` Int32, `ResponseEndTiming` Int32, `FetchTiming` Int32, `RedirectTiming` Int32, `DOMInteractiveTiming` Int32, `DOMContentLoadedTiming` Int32, `DOMCompleteTiming` Int32, `LoadEventStartTiming` Int32, `LoadEventEndTiming` Int32, `NSToDOMContentLoadedTiming` Int32, `FirstPaintTiming` Int32, `RedirectCount` Int8, `SocialSourceNetworkID` UInt8, `SocialSourcePage` String, `ParamPrice` Int64, `ParamOrderID` String, `ParamCurrency` FixedString(3), `ParamCurrencyID` UInt16, `GoalsReached` Array(UInt32), `OpenstatServiceName` String, `OpenstatCampaignID` String, `OpenstatAdID` String, `OpenstatSourceID` String, `UTMSource` String, `UTMMedium` String, `UTMCampaign` String, `UTMContent` String, `UTMTerm` String, `FromTag` String, `HasGCLID` UInt8, `RefererHash` UInt64, `URLHash` UInt64, `CLID` UInt32, `YCLID` UInt64, `ShareService` String, `ShareURL` String, `ShareTitle` String, `ParsedParams` Nested(Key1 String,Key2 String,Key3 String,Key4 String,Key5 String,ValueDouble Float64), `IslandID` FixedString(16), `RequestNum` UInt32, `RequestTry` UInt8 ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/hits_local', '{replica}') PARTITION BY toYYYYMM(EventDate) ORDER BY (CounterID, EventDate, intHash32(UserID)) SAMPLE BY intHash32(UserID);説明{shard} パラメーターと {replica} パラメーターは、Alibaba Cloud E-MapReduce (EMR) によって ClickHouse クラスター用に自動的に生成されるマクロであり、直接使用できます。
次のコマンドを実行して、分散テーブルを作成します。
分散テーブルはデータを格納しません。基になるテーブルのビューにすぎません。分散テーブルを使用して、複数のサーバーで分散クエリを実行できます。この例では、rand() ランダム関数を使用しています。この関数は、データが各ノードのローカルテーブルにランダムに書き込まれることを保証します。
CREATE TABLE demo.hits_all on CLUSTER cluster_emr AS demo.hits_local ENGINE = Distributed(cluster_emr, demo, hits_local, rand());ClickHouse クライアントを終了し、サンプルデータが格納されているディレクトリで次のコマンドを実行してデータをインポートします。
clickhouse-client -h core-1-1 --query "INSERT INTO demo.hits_all FORMAT TSV" --max_insert_block_size=100000 < hits_v1.tsv;ClickHouse クライアントを再起動して、データを表示します。
データはランダムに書き込まれます。そのため、データ量はノードごとに異なる場合があります。
次のコマンドを実行して、core-1-1 ノードの demo.hits_all テーブルのデータ量を表示します。
select count(*) from demo.hits_all;次のコマンドを実行して、core-1-1 ノードの demo.hits_local テーブルのデータ量を表示します。
select count(*) from demo.hits_local;core-1-2 ノードの demo.hits_local テーブルのデータ量を表示します。
説明次の操作を実行して、他のノードの demo.hits_local テーブルのデータ量を表示できます。E-MapReduce コンソールのクラスターの [ノード] タブでノード名を表示できます。
次のコマンドを実行して、ClickHouse クライアントにログオンします。
clickhouse-client -h core-1-2 -mClickHouse クライアントで次のコマンドを実行して、demo.hits_local テーブルのデータ量を表示します。
select count(*) from demo.hits_local;