# Raspberry Pi Import Integration

[Raspberry Pi](http://www.raspberrypi.org/)は、クレジットカードサイズのシングルボードコンピュータです。低コストで、さまざまなタイプのセンサーを簡単に装備できるため、Raspberry Piをクラウドデータロガーとして使用することは、理想的な使用例の1つです。

Raspberry PiからTreasure Dataにセンサーデータをインポートできます。

# Raspbianのインストール

[Raspbian](http://www.raspbian.org/)は、Debianをベースにした無料のオペレーティングシステムで、Raspberry Pi用に最適化されています。以下のブログ投稿の手順に従って、Raspberry PiにRaspbianをインストールしてください:

- [Getting Started with Raspberry Pi: Installing Raspbian](http://www.andrewmunsell.com/blog/getting-started-raspberry-pi-install-raspbian)


# Treasure Agent（Fluentd）のインストール

次に、RaspbianにTreasure Agent（Fluentd）をインストールします。RaspbianはデフォルトでRuby 1.9.3をバンドルしていますが、追加の開発パッケージが必要です。


```
$ sudo aptitude install ruby-dev
```

これで、Treasure Agent（Fluentd）と必要なプラグインをインストールします。


```
$ sudo gem install fluentd
$ sudo fluent-gem install fluent-plugin-td

または、fluentdの代わりにfluent-bitを使用できます。詳細については、以下のドキュメントを参照してください。
http://fluentbit.io/documentation/0.12/installation/raspberry_pi.html
```

# Treasure Agentの設定と起動

[Treasure アカウントにサインイン](https://console.treasuredata.com/users/sign_in)してください。アカウントのAPIキーは、[usersページ](https://console.treasuredata.com/users/current)から取得できます。

APIキーを含む以下の情報でfluent.confファイルを準備します。


```
<match td.*.*>
  type tdlog
  apikey YOUR_API_KEY_HERE

  auto_create_table
  buffer_type file
  buffer_path /home/pi/td
</match>
source
  type http
  port 8888
</source>
source
  type forward
</source>
```

最後に、ターミナルからTreasure Agent（Fluentd）を起動します。


```
$ fluentd -c fluent.conf
```

# アップロードテスト

設定をテストするには、HTTP経由でTreasure AgentにJSONメッセージを投稿するだけです。


```
$ curl -X POST -d 'json={"sensor1":3123.13,"sensor2":321.3}' \
  http://localhost:8888/td.testdb.raspberrypi
```

Pythonを使用している場合は、pythonロガーライブラリを使用できます。
次に、databasesページにアクセスして、データがクラウドに正しくアップロードされたことを確認してください。

- [Treasure Data: List of Databases](https://console.treasuredata.com/databases)


これで、インポートされたデータに対してクエリを発行できます。

- [Treasure Data: New Query](https://console.treasuredata.com/query_forms/new)


たとえば、これらのクエリはsensor1値の平均とsensor2値の合計を計算します。


```
SELECT AVG(sensor1) FROM raspberrypi;
SELECT SUM(sensor2) FROM raspberrypi;
```

# 結論

Raspberry Piは、データロガーハードウェアのプロトタイピングに理想的なプラットフォームです。Raspberry PiとTreasure Dataを組み合わせることで、クラウドで物理世界を簡単に分析できます。