All Products
Search
Document Center

ApsaraDB for ClickHouse:Create a table

Last Updated:Jan 04, 2024

This topic describes how to use Data Management (DMS) to create a table in ApsaraDB for ClickHouse.

Prerequisites

The following steps listed in Quick Start are complete:

Procedure

  1. On the SQL Console page in the DMS console, execute the CREATE TABLE statement to create a local table.

    Note

    Local tables are tables that actually store data. Each ApsaraDB for ClickHouse node has its own local tables. These tables are used to manage and query the data stored on that node. Local tables are suitable for serving as the data shards of each node in the cluster.

    Syntax:

    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, ...];

    Parameter description:

    Parameter

    Description

    db

    The name of the database. The default value is the name of the database that is selected.

    local_table_name

    The name of the local table.

    cluster

    The name of the cluster. The value is default.

    name1,name2

    The name of the column.

    type1,type2

    The type of the column.

    engine_name

    The type of the table engine. For more information, see Table engines.

    Note

    The MergeTree table engine is used for Single-replica Edition. The ReplicatedMergeTree table engine is used for Double-replica Edition.

    PARTITION BY

    The partition key.

    ORDER BY

    The sort key.

    PRIMARY KEY

    The primary key.

    SAMPLE BY

    The sample expression. If you want to use the sample expression, include the sample expression in the primary key.

    SETTINGS

    Other parameters that can affect the performance of creating the table.

    Note

    For more information, see Create Table.

    Example:

    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 ;
  2. Create a distributed table to write data and perform queries.

    Note
    • Distributed tables are virtual tables that do not directly store any data. Instead, they serve as a query layer to distribute queries to local tables on one or more nodes and aggregate the returned results. Distributed tables are suitable for distributed queries and processing in an ApsaraDB for ClickHouse cluster.

    Syntax:

    CREATE TABLE  [db.]d_table_name ON CLUSTER cluster
     AS db.local_table_name ENGINE = Distributed(cluster, db, local_table_name [, sharding_key])

    Parameter description:

    Parameter

    Description

    db

    The name of the database. The default value is the name of the database that is selected.

    d_table_name

    The name of the distributed table.

    cluster

    The name of the cluster. The value is default.

    local_table_name

    The name of the local table that you created.

    sharding_key

    The sharding expression.

    Note

    For more information, see Create Table.

    Example:

    CREATE TABLE ontime_local_distributed ON CLUSTER default
     AS clickhouse_demo.ontime_local  
    ENGINE = Distributed(default, clickhouse_demo, ontime_local, rand());

What to do next

Import data