# CSV、TSV、LTSVフォーマットを使用したログのインポート

Treasure Agent (td-agent) から CSV、TSV、LTSV 形式のログをインポートして、アクセスログをクラウドに継続的にインポートできます。

**td-agentはログローテーションを処理します**。td-agentはログの最終位置を記録し、td-agentプロセスがダウンした場合でも各行が正確に1回だけ読み取られることを保証します。ただし、情報はファイルに保持されるため、ファイルが破損すると「exactly once」の保証は無効になります。

## 前提条件

td-agentは[Fluentdプロジェクト](http://fluentd.org/)の下にあります。td-agentはTreasure Data用のカスタムプラグインでFluentdを拡張しています。

## td-agentのインストール

アプリケーションサーバーにtd-agentをインストールします。td-agentは、あらゆる種類の時系列データのストリーミングアップロード専用のデーモンプログラムです。td-agentはFluentdプロジェクトの下にあります。td-agentはTreasure Data用のカスタムプラグインでFluentdを拡張しています。

td-agentをセットアップするには、以下の記事を参照してください。Linuxシステム用のdeb/rpmパッケージを提供しています。

| **環境** | **参照先** |
|  --- | --- |
| MacOS X | [Installing td-agent on MacOS X](https://docs.fluentd.org/installation/install-by-dmg) |
| Ubuntu System | [Installing td-agent for Debian and Ubuntu](https://docs.fluentd.org/installation/install-by-deb) |
| RHEL / CentOS System | [Installing td-agent for Redhat and CentOS](https://docs.fluentd.org/installation/install-by-rpm) |
| AWS Elastic Beanstalk | [Installing td-agent on AWS Elastic Beanstalk](https://github.com/treasure-data/elastic-beanstalk-td-agent) |


## td-agent.confの変更

`apikey`オプションを設定して認証キーを指定します。APIキーはTreasure コンソールから確認できます。

`/etc/td-agent/td-agent.conf`にアクセスして、`apikey`オプションを設定します。

*YOUR_API_KEY*は、あなたのAPIキー文字列に置き換えてください。


```conf
# Tailing the CSV formatted Logs
<source>
  type tail
  format csv
  path /path/to/log/foo.csv
  pos_file /var/log/td-agent/foo.pos
  tag td.production.foo

  keys key1, key2, key3
  time_key key3
</source>

# Tailing the TSV formatted Logs
<source>
  type tail
  format tsv
  path /path/to/log/bar.tsv
  pos_file /var/log/td-agent/bar.pos
  tag td.production.bar

  keys key1, key2, key3
  time_key key3
</source>

# Tailing the LTSV formatted Logs
<source>
  type tail
  format ltsv
  path /path/to/log/buz.ltsv
  pos_file /var/log/td-agent/buz.pos
  tag td.production.buz

  time_key time_field_name
</source>

# Treasure Data Input and Output
<match td.*.*>
  type tdlog
  endpoint api.treasuredata.com
  apikey YOUR_API_KEY
  auto_create_table
  buffer_type file
  buffer_path /var/log/td-agent/buffer/td
  use_ssl true
</match>
```

以下の行が配置されたら、agentを再起動します。


```
$ sudo /etc/init.d/td-agent restart
```

td-agentはファイルをtailし、ログをバッファリングし(*var/log/td-agent/buffer/td*)、自動的にログをクラウドにアップロードします。

# データインポートの確認

SIGUSR1シグナルを送信すると、td-agentのバッファがフラッシュされ、アップロードがすぐに開始されます。


```
# append new records to the logs
$ ...

# flush the buffer
$ kill -USR1 `cat /var/run/td-agent/td-agent.pid`
```

データが正常にアップロードされたことを確認するには、次のように`td tables`コマンドを実行します。


```
$ td tables
+------------+------------+------+-----------+
| Database   | Table      | Type | Count     |
+------------+------------+------+-----------+
| production | foo        | log  | 1         |
| production | bar        | log  | 3         |
| production | buz        | log  | 5         |
+------------+------------+------+-----------+
```

正しく動作しない場合は、`/var/log/td-agent.log`を確認してください。`td-agent:td-agent`がログを読み取る権限を持っている必要があります。

## 次のステップ

従来のRDBMSよりも柔軟なスキーマメカニズムを提供しています。クエリには、Hive Query Languageを活用しています。

- [Schema Management](/products/customer-data-platform/data-workbench/databases/schema-management)
- [Hive Query Language](/products/customer-data-platform/data-workbench/queries)
- [Programmatic Access with REST API and its Bindings](/tools/cli-and-sdks/api-references-and-tool-references)