本文介紹如何使用DMS(Data Management Service)在雲資料庫ClickHouse中建立表。
前提條件
已完成快速入門的如下步驟。
操作步驟
在Data Management控制台的SQL Console頁面中,輸入
CREATE TABLE
語句建立本地表。說明本地表(Local tables)是實際儲存資料的表,每個ClickHouse節點上都有其自己的本地表,這些表用於管理和查詢儲存在該節點上的資料。本地表適合於作為叢集中每個節點的資料分區。
文法:
CREATE TABLE [IF NOT EXISTS] [db.]local_table_name ON CLUSTER cluster ( name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1], name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2], ... INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1, INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2 ) ENGINE = engine_name() [PARTITION BY expr] [ORDER BY expr] [PRIMARY KEY expr] [SAMPLE BY expr] [SETTINGS name=value, ...];
參數說明:
參數
說明
db
指定資料庫的名稱,預設為當前選擇的資料庫。
local_table_name
本地表名。
cluster
指定叢集的名稱,固定為default。
name1,name2
列名。
type1,type2
列類型。
engine_name
表引擎類型,具體請參見表引擎。
說明單副本版為MergeTree,雙副本版為ReplicatedMergeTree。
PARTITION BY
指定分區鍵。
ORDER BY
指定排序鍵。
PRIMARY KEY
指定主鍵。
SAMPLE BY
抽樣運算式。如果要使用抽樣運算式,主鍵中必須包含這個運算式。
SETTINGS
影響效能的其他參數。
說明更多參數說明,請參見Create Table。
樣本:
CREATE TABLE clickhouse_demo.ontime_local ON CLUSTER default ( `Year` UInt16, `Quarter` UInt8, `Month` UInt8, `DayofMonth` UInt8, `DayOfWeek` UInt8, `FlightDate` Date, `Reporting_Airline` String, `DOT_ID_Reporting_Airline` Int32, `IATA_CODE_Reporting_Airline` String, `Tail_Number` String, `Flight_Number_Reporting_Airline` String, `OriginAirportID` Int32, `OriginAirportSeqID` Int32, `OriginCityMarketID` Int32, `Origin` FixedString(5), `OriginCityName` String, `OriginState` FixedString(2), `OriginStateFips` String, `OriginStateName` String, `OriginWac` Int32, `DestAirportID` Int32, `DestAirportSeqID` Int32, `DestCityMarketID` Int32, `Dest` FixedString(5), `DestCityName` String, `DestState` FixedString(2), `DestStateFips` String, `DestStateName` String, `DestWac` Int32, `CRSDepTime` Int32, `DepTime` Int32, `DepDelay` Int32, `DepDelayMinutes` Int32, `DepDel15` Int32, `DepartureDelayGroups` String, `DepTimeBlk` String, `TaxiOut` Int32, `WheelsOff` Int32, `WheelsOn` Int32, `TaxiIn` Int32, `CRSArrTime` Int32, `ArrTime` Int32, `ArrDelay` Int32, `ArrDelayMinutes` Int32, `ArrDel15` Int32, `ArrivalDelayGroups` Int32, `ArrTimeBlk` String, `Cancelled` UInt8, `CancellationCode` FixedString(1), `Diverted` UInt8, `CRSElapsedTime` Int32, `ActualElapsedTime` Int32, `AirTime` Nullable(Int32), `Flights` Int32, `Distance` Int32, `DistanceGroup` UInt8, `CarrierDelay` Int32, `WeatherDelay` Int32, `NASDelay` Int32, `SecurityDelay` Int32, `LateAircraftDelay` Int32, `FirstDepTime` String, `TotalAddGTime` String, `LongestAddGTime` String, `DivAirportLandings` String, `DivReachedDest` String, `DivActualElapsedTime` String, `DivArrDelay` String, `DivDistance` String, `Div1Airport` String, `Div1AirportID` Int32, `Div1AirportSeqID` Int32, `Div1WheelsOn` String, `Div1TotalGTime` String, `Div1LongestGTime` String, `Div1WheelsOff` String, `Div1TailNum` String, `Div2Airport` String, `Div2AirportID` Int32, `Div2AirportSeqID` Int32, `Div2WheelsOn` String, `Div2TotalGTime` String, `Div2LongestGTime` String, `Div2WheelsOff` String, `Div2TailNum` String, `Div3Airport` String, `Div3AirportID` Int32, `Div3AirportSeqID` Int32, `Div3WheelsOn` String, `Div3TotalGTime` String, `Div3LongestGTime` String, `Div3WheelsOff` String, `Div3TailNum` String, `Div4Airport` String, `Div4AirportID` Int32, `Div4AirportSeqID` Int32, `Div4WheelsOn` String, `Div4TotalGTime` String, `Div4LongestGTime` String, `Div4WheelsOff` String, `Div4TailNum` String, `Div5Airport` String, `Div5AirportID` Int32, `Div5AirportSeqID` Int32, `Div5WheelsOn` String, `Div5TotalGTime` String, `Div5LongestGTime` String, `Div5WheelsOff` String, `Div5TailNum` String )ENGINE = ReplicatedMergeTree() PARTITION BY toYYYYMM(FlightDate) PRIMARY KEY (intHash32(FlightDate)) ORDER BY (intHash32(FlightDate)) SAMPLE BY intHash32(FlightDate) SETTINGS index_granularity= 8192 ;
建立分布式表,方便資料的寫入和查詢。
說明分布式表(Distributed tables)是虛擬表,不直接儲存任何資料,而是作為一個查詢層,將查詢分發到一個或多個節點上的本地表,並匯總返回結果。分布式表適用於在ClickHouse叢集環境中實現資料分布查詢和處理。
文法:
CREATE TABLE [db.]d_table_name ON CLUSTER cluster AS db.local_table_name ENGINE = Distributed(cluster, db, local_table_name [, sharding_key])
參數說明:
參數
說明
db
指定資料庫的名稱,預設為當前選擇的資料庫。
d_table_name
分布式表名。
cluster
指定叢集的名稱,固定為default。
local_table_name
已建立的本地表名。
sharding_key
分區運算式。
說明更多參數說明,請參見Create Table。
樣本:
CREATE TABLE ontime_local_distributed ON CLUSTER default AS clickhouse_demo.ontime_local ENGINE = Distributed(default, clickhouse_demo, ontime_local, rand());