07-import.md 2.9 KB
Newer Older
D
dingbo 已提交
1
---
G
gccgdb1234 已提交
2
title: Data Import
D
dingbo 已提交
3 4
---

5
There are multiple ways of importing data provided byTDengine: import with script, import from data file, import using `taosdump`.
D
dingbo 已提交
6

7
## Import Using Script
D
dingbo 已提交
8

9
TDengine CLI `taos` supports `source <filename>` command for executing the SQL statements in the file in batch. The SQL statements for creating databases, creating tables, and inserting rows can be written in single file with one statement on each line, then the file can be executed using `source` command in TDengine CLI `taos` to execute the SQL statements in order and in batch. In the script file, any line beginning with "#" is treated as comments and ignored silently.
D
dingbo 已提交
10

11
## Import from Data File
D
dingbo 已提交
12

13
In TDengine CLI, data can be imported from a CSV file into an existing table. The data in single CSV must belong to same table and must be consistent with the schema of that table. The SQL statement is as below:
D
dingbo 已提交
14 15 16 17 18 19

```sql
insert into tb1 file 'path/data.csv';
```

:::note
20
If there is description in the first line of a CSV file, please remove it before importing. If there is no value for a column, please use `NULL` without quotes.
D
dingbo 已提交
21 22 23

:::

24
For example, there is a sub table d1001 whose schema is as below:
D
dingbo 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37

```sql
taos> DESCRIBE d1001
             Field              |        Type        |   Length    |    Note    |
=================================================================================
 ts                             | TIMESTAMP          |           8 |            |
 current                        | FLOAT              |           4 |            |
 voltage                        | INT                |           4 |            |
 phase                          | FLOAT              |           4 |            |
 location                       | BINARY             |          64 | TAG        |
 groupid                        | INT                |           4 | TAG        |
```

38
The format of the CSV file to be imported, data.csv, is as below:
D
dingbo 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51

```csv
'2018-10-04 06:38:05.000',10.30000,219,0.31000
'2018-10-05 06:38:15.000',12.60000,218,0.33000
'2018-10-06 06:38:16.800',13.30000,221,0.32000
'2018-10-07 06:38:05.000',13.30000,219,0.33000
'2018-10-08 06:38:05.000',14.30000,219,0.34000
'2018-10-09 06:38:05.000',15.30000,219,0.35000
'2018-10-10 06:38:05.000',16.30000,219,0.31000
'2018-10-11 06:38:05.000',17.30000,219,0.32000
'2018-10-12 06:38:05.000',18.30000,219,0.31000
```

52
Then, below SQL statement can be used to import data from file "data.csv", assuming the file is located under the home directory of current Linux user.
D
dingbo 已提交
53 54 55 56 57 58

```sql
taos> insert into d1001 file '~/data.csv';
Query OK, 9 row(s) affected (0.004763s)
```

59
## Import using taosdump
D
dingbo 已提交
60

61
A convenient tool for importing and exporting data is provided by TDengine, `taosdump`, which can used to export data from one TDengine cluster and import into another one. For the details of using `taosdump` please refer to [Tool for exporting and importing data: taosdump](/reference/taosdump).