Initially, you can create a database through the root or admin account with the following command:
CREATE DATABASE example_db;
All commands can use HELP command
to see detailed syntax help. For example: HELP CREATE DATABASE;
.
If you don't know the full name of the command, you can use "help + a field" for fuzzy query. For example, if you type HELP CREATE
, you can get commands like CREATE DATABASE
, CREATE TABLE
, and CREATE USER
.
After the database is created, you can view the database information through SHOW DATABASES;
.
MySQL> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| example_db |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)
information_schema
exists to be compatible with the MySQL protocol. In practice, information may not be accurate. Therefore, you are advised to obtain the information about specific databases by directly querying the corresponding databases.
After the example_db
database is created, you can authorize the read/write permissions for example_db
to an ordinary account, such as test, through the root or admin account. After authorization, you can log in to and operate example_db
using the test account.
GRANT ALL ON example_db TO test;
Create a table with the CREATE TABLE
command. More parameter information can be seen by running the HELP CREATE TABLE;
command.
Switch the database using the following command:
USE example_db;
Doris supports two ways to create a table: single partitioning and composite partitioning.
In composite partitioning:
Composite partitioning is recommended for the following scenarios:
DELETE
statement within a specified partition.The following takes the aggregation model as an example to separately illustrate the table creation statements of the two kinds of partitioning.
Create a logical table named table1
. The bucket column is siteid
and the number of buckets is 10. The schema of this table is as follows:
siteid
: the type is INT
(4 bytes); the default value is 10
.citycode
: the type is SMALLINT
(2 bytes).username
: the type is VARCHAR
; the maximum length is 32
; the default value is an empty string.pv
: the type is BIGINT
(8 bytes); the default value is 0
. This is a metric column. Doris will aggregate the metric column internally. The aggregation method of this column is SUM
.The table creation statement is as follows:
CREATE TABLE table1
(
siteid INT DEFAULT '10',
citycode SMALLINT,
username VARCHAR(32) DEFAULT '',
pv BIGINT SUM DEFAULT '0'
)
AGGREGATE KEY(siteid, citycode, username)
DISTRIBUTED BY HASH(siteid) BUCKETS 10
PROPERTIES("replication_num" = "1");
Create a logical table named table2
. The schema of this table is as follows:
event_day
: the type is DATE
; no default value.siteid
: the type is INT
(4 bytes); the default value is 10
.citycode
: the type is SMALLINT
(2 bytes).username
: the type is VARCHAR
; the maximum length is 32
; the default value is an empty string.pv
: the type is BIGINT
(8 bytes); the default value is 0
. This is a metric column. Doris will aggregate the metric column internally. The aggregation method of this column is SUM
.The event_day
column is used as the partition column to create three partitions: p201706
, p201707
, and p201708
.
p201706
: the range is [Minimum, 2017-07-01).p201707
: the range is [2017-07-01, 2017-08-01).p201708
: the range is [2017-08-01, 2017-09-01).Note:Note that the interval is left-closed and right-open.
Each partition uses siteid
to hash buckets, with a bucket count of 10. The table creation statement is as follows:
CREATE TABLE table2
(
event_day DATE,
siteid INT DEFAULT '10',
citycode SMALLINT,
username VARCHAR(32) DEFAULT '',
pv BIGINT SUM DEFAULT '0'
)
AGGREGATE KEY(event_day, siteid, citycode, username)
PARTITION BY RANGE(event_day)
(
PARTITION p201706 VALUES LESS THAN ('2017-07-01'),
PARTITION p201707 VALUES LESS THAN ('2017-08-01'),
PARTITION p201708 VALUES LESS THAN ('2017-09-01')
)
DISTRIBUTED BY HASH(siteid) BUCKETS 10
PROPERTIES("replication_num" = "1");
After the table is created, you can view the information of the table in example_db
:
MySQL> SHOW TABLES;
+----------------------+
| Tables_in_example_db |
+----------------------+
| table1 |
| table2 |
+----------------------+
2 rows in set (0.01 sec)
MySQL> DESC table1;
+----------+-------------+------+-------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-------+---------+-------+
| siteid | int(11) | Yes | true | 10 | |
| citycode | smallint(6) | Yes | true | N/A | |
| username | varchar(32) | Yes | true | | |
| pv | bigint(20) | Yes | false | 0 | SUM |
+----------+-------------+------+-------+---------+-------+
4 rows in set (0.00 sec)
MySQL> DESC table2;
+-----------+-------------+------+-------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-------+---------+-------+
| event_day | date | Yes | true | N/A | |
| siteid | int(11) | Yes | true | 10 | |
| citycode | smallint(6) | Yes | true | N/A | |
| username | varchar(32) | Yes | true | | |
| pv | bigint(20) | Yes | false | 0 | SUM |
+-----------+-------------+------+-------+---------+-------+
5 rows in set (0.00 sec)
Note:For more syntax description regarding the use of Doris, see Data Table Creation and Data Import.
replication_num
are all single-replica tables. Doris recommends that users adopt the default three-replica settings to ensure high availability.Null
attribute for column is true
, which may affect the query performance.Doris supports a variety of data import methods. The following uses streaming import and broker import as examples.
Streaming import transfers data to Doris over the HTTP protocol. It can import local data directly without relying on other systems or components.
With table1_20170707
as the label, import the table1
table using the table1_data
local file.
curl --location-trusted -u test:test -H "label:table1_20170707" -H "column_separator:," -T table1_data http://FE_HOST:8030/api/example_db/table1/_stream_load
FE_HOST
is the IP of any FE node and 8030
is the http_port
in fe.conf
.webserver_port
in be.conf
for import. For example: BE_HOST:8040
.The table1_data
local file uses a comma (,) as the separator between data. The specific content is as follows:
1,1,jim,2
2,1,grace,2
3,2,tom,2
4,3,bush,3
5,3,helen,3
With table2_20170707
as the label, import the table2
table using the table2_data
local file.
curl --location-trusted -u test:test -H "label:table2_20170707" -H "column_separator:|" -T table2_data http://127.0.0.1:8030/api/example_db/table2/_stream_load
The table2_data
local file uses a vertical bar (|) as the separator between data. The specific content is as follows:
2017-07-03|1|1|jim|2
2017-07-05|2|1|grace|2
2017-07-12|3|2|tom|2
2017-07-15|4|3|bush|3
2017-07-12|5|3|helen|3
Broker imports import data from external storage through deployed broker processes.
With table1_20170708
as the label, import files on HDFS into the table1
table.
LOAD LABEL table1_20170708
(
DATA INFILE("hdfs://your.namenode.host:port/dir/table1_data")
INTO TABLE table1
)
WITH BROKER hdfs
(
"username"="hdfs_user",
"password"="hdfs_password"
)
PROPERTIES
(
"timeout"="3600",
"max_filter_ratio"="0.1"
);
Broker imports are asynchronous commands. Successful execution of the above commands only indicates successful submission of tasks. Successful imports need to be checked through `SHOW LOAD;'. The command is as follows:
SHOW LOAD WHERE LABEL = "table1_20170708";
In the return result, FINISHED
in the State
field indicates that the import was successful.
Asynchronous import tasks can be canceled before the end by using the following command:
CANCEL LOAD WHERE LABEL = "table1_20170708";
Was this page helpful?