# FTP Import Integration CLI

You can also use the FTP data connector from the command line interface. The following instructions show you how to import data using the CLI.

## Install ‘td’ Command v0.11.9 or Later

Install the most current [TD Toolbelt](https://toolbelt.treasuredata.com/).


```bash
$ td --version
0.11.10
```

## Create Seed Config File (seed.yml)

First, prepare *seed.yml* as shown in the following example, with your FTP details. You must also specify bucket name, and target file name (or prefix for multiple files).


```yaml
in:
  type: ftp
  host: ftp.example.net
  port: 21
  user: anonymous
  password: XXXX
  path_prefix: /ftp/file/path/prefix # path of the *.csv or *.tsv file on your FTP server
out:
  mode: append
```

The Data Connector for FTP imports all files that match the specified prefix (e.g. path_prefix: path/to/sample_ > path/to/sample_201501.csv.gz, path/to/sample_201502.csv.gz, …, path/to/sample_201505.csv.gz).

Active Mode is NOT supported.

If you’re using FTPS, specify additional details as follows:


```yaml
in:
  type: ftp
  host: ftp.example.net
  port: 21
  user: anonymous
  password: "mypassword"
  path_prefix: /ftp/file/path/prefix
  ssl: true
  ssl_verify: false
out:
  mode: append
```

For more details, see [modes for out plugin](/int/ftp-import-integration-cli).

## Guess Fields (Generate load.yml)

Use *connector:guess*. This command automatically reads the source file, and assesses (uses logic to guess) the file format.

The guess command needs more than 3 rows and 2 columns in source data file, because the command assesses the column definition using sample rows from source data.


```bash
$ td connector:guess seed.yml -o load.yml
```

If you open load.yml, you’ll see the assessed file format definitions including file formats, encodings, column names, and types.


```yaml
in:
  type: ftp
  host: ftp.example.net
  port: 21
  user: anonymous
  password: XXXX
  path_prefix: /ftp/file/path/prefix
  parser:
    charset: UTF-8
    newline: CRLF
    type: csv
    delimiter: ','
    quote: '"'
    escape: ''
    skip_header_lines: 1
    columns:
    - name: id
      type: long
    - name: company
      type: string
    - name: customer
      type: string
    - name: created_at
      type: timestamp
      format: '%Y-%m-%d %H:%M:%S'
out:
  mode: append
```

Then, you can preview how the system will parse the file by using the *preview* command.


```bash
$ td connector:preview load.yml
```

If the system detects your column name or column type unexpectedly, modify *load.yml* directly and preview again.

Currently, the data connector supports parsing of “boolean”, “long”, “double”, “string”, and “timestamp” types.

You also must create a database and table prior to executing the data load job. Follow these steps:


```bash
td database:create td_sample_db
td table:create td_sample_db td_sample_table
```

## Execute Load Job

Finally, submit the load job. It may take a couple of hours depending on the size of the data. Specify the Treasure Data database and table where the data should be stored.

It’s also recommended to specify *--time-column* option, because Treasure Data’s storage is partitioned by time (see [data partitioning](https://docs.treasuredata.com/smart/project-product-documentation/data-partitioning-in-treasure-data)) If the option is not provided, the data connector chooses the first *long* or *timestamp* column as the partitioning time. The type of the column specified by *--time-column* must be either of *long* and *timestamp* type.

If your data doesn’t have a time column you can add a time column by using *add_time* filter option. For more details see [add_time filter plugin](https://docs.treasuredata.com/smart/project-product-documentation/add_time-filter-function).


```bash
$ td connector:issue load.yml --database td_sample_db --table td_sample_table \
--time-column created_at
```

The connector:issue command assumes that you have already created a *database(td_sample_db)* and a *table(td_sample_table)*. If the database or the table do not exist in TD, the connector:issue command fails, so create the database and table manually or use *--auto-create-table* option with *td connector:issue* command to auto create the database and table:


```bash
$ td connector:issue load.yml --database td_sample_db --table td_sample_table --time-column created_at --auto-create-table
```

At present, the data connector does not sort records on server-side. To use time-based partitioning effectively, sort records in files beforehand.

If you have a field called *time*, you don’t have to specify the *--time-column* option.


```bash
td connector:issue load.yml --database td_sample_db --table td_sample_table
```

## Scheduled Execution

You can schedule periodic data connector execution for incremental FTP file import. We take great care in distributing and operating our scheduler to achieve high availability. By using this feature, you no longer need a *cron* daemon on your local data center.

For the scheduled import, the Data Connector for FTP imports all files that match with the specified prefix (e.g. path_prefix: `path/to/sample_` –> `path/to/sample_201501.csv.gz`, `path/to/sample_201502.csv.gz`, …, `path/to/sample_201505.csv.gz`) at first and remembers the last path (`path/to/sample_201505.csv.gz`) for the next execution.

On the second and on subsequent runs, the connector imports only files that comes after the last path in alphabetical (lexicographic) order. (`path/to/sample_201506.csv.gz`, …)

### Create the Schedule

A new schedule can be created using the *td connector:create* command. The following are required: the name of the schedule, the cron-style schedule, the database and table where the data will be stored, and the Data Connector configuration file.


```
$ td connector:create \
    daily_import \
    "10 0 * * *" \
    td_sample_db \
    td_sample_table \
    load.yml
```

It’s also recommended to specify the *--time-column* option, because Treasure Data’s storage is partitioned by time (see [data partitioning](https://docs.treasuredata.com/smart/project-product-documentation/data-partitioning-in-treasure-data)).


```
$ td connector:create \
    daily_import \
    "10 0 * * *" \
    td_sample_db \
    td_sample_table \
    load.yml \
    --time-column created_at
```

The `cron` parameter also accepts three special options: `@hourly`, `@daily` and `@monthly`.

By default, schedule is setup in UTC timezone. You can set the schedule in a timezone using -t or --timezone option. Note that `--timezone` option supports only extended timezone formats like 'Asia/Tokyo', 'America/Los_Angeles' etc. Timezone abbreviations like PST, CST are *not* supported and may lead to unexpected schedules.

### List the Schedules

You can see the list of currently scheduled entries by running the command *td connector:list*.


```
$ td connector:list
```

## Show the Setting and Schedule History

*td connector:show* shows the execution setting of a schedule entry.


```
td connector:show daily_import
```

*td connector:history* shows the execution history of a schedule entry. To investigate the results of each individual run, use *td job jobid*.


```
td connector:history daily_import
```