You can use the CREATE VIEW
statement to create a view. A view is a virtual table that is generated based on a SELECT statement. It is useful in scenarios such as type conversion, column conversion, virtual columns, and long code splitting.
Syntax
CREATE VIEW view name AS
SELECT clause
Example 1
Create a view with the name "MyView":
CREATE VIEW MyView AS
SELECT s1.time_, s1.client_ip, s1.uri, s1.protocol_version, s2.status_code, s2.date_
FROM KafkaSource1 AS s1, KafkaSource2 AS s2
WHERE s1.time_ = s2.time_ AND s1.client_ip = s2.client_ip;
Example 2
Due to reasons such as large data volumes or the use of specific function types, you may need to use data types such as TINYINT and SMALLINT. Such data types are not recognized by Kafka and some other data sources. In such cases, you can use the CREATE VIEW
statement together with the type conversion function CAST()
(see Type Conversion Functions) to define a virtual table and use it as the data source. Define a view named "KafkaSource2" to convert the status_code
column, whose data type is BIGINT, to the data type VARCHAR:
CREATE VIEW KafkaSource2 AS
SELECT
`time_`,
`client_ip`,
`method`,
CAST(`status_code` AS VARCHAR) AS status_code,
FROM KafkaSource1;
Note
Some CAST()
conversions, for example, from BIGINT to INTEGER or BIGINT to TINYINT, may lead to loss of precision.
To convert between string (VARCHAR) and timestamp (TIMESTAMP), see TO_TIMESTAMP
and DATE_FORMAT
functions in Date and Time Functions.
Was this page helpful?