本文介紹如何使用DMS(Data Management Service)在云數據庫ClickHouse中創建表。
前提條件
已完成快速入門的如下步驟。
操作步驟
在的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 ;
創建分布式表,方便數據的寫入和查詢。
說明云數據庫ClickHouse企業版不需要創建分布式表。企業版支持的表引擎詳情請參考云數據庫ClickHouse企業版兼容性指導。
分布式表(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());