> ## Documentation Index
> Fetch the complete documentation index at: https://docs.exploratory.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Amazon Redshift データベースからデータをインポートする

Amazon Redshift DatabaseからExploratoryに素早くデータをインポートすることができます。

Redshiftについて詳しく紹介した[ブログ記事](https://blog.exploratory.io/exploratory-data-analysis-for-amazon-redshift-with-r-dplyr-9a14441020eb#.aqcbfa6h8)はこちらです。

## 1. 使用するデータコネクションを作成する

[この手順](https://docs.exploratory.io/en/import/connection.html.md)に従って、接続を作成します。

## 2. Redshift インポートダイアログを開く

データフレームの横にある「＋」ボタンをクリックし、「Database Data」を選択します。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/import-database.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=6f0f43084dc13ecad3cdece6414e8e99)をクリックします。

Amazon Redshiftをクリックして選択します。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/redshift.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=89cadde4014e2eb607addb86f1aa5d6b)

## 3. プレビューとインポート

実行ボタンをクリックすると、Redshift データベースから取得してきたデータを確認することができます。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/import-redshift-dialog.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=03113141953c7de21519b708f93a16ea)

問題ないようであれば、「インポート」をクリックしてExploratoryにデータをインポートします。

## 4. ランダムサンプルデータのクエリ

分析に適したサイズのデータからランダムにサンプルを取りたい場合があります。

[md5](http://docs.aws.amazon.com/redshift/latest/dg/r_MD5.html)関数を使って乱数を生成し、以下のように使用することでデータのランダムサンプルを得ることができます。

```
SELECT *
   FROM airline_2016_01
   ORDER BY md5('randomSeed' || flight_num)
   LIMIT 100000
```

## 5. SQLでパラメータを使用する

まず、SQL データインポートダイアログのパラメータリンクをクリックします。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/add_parameter.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=b89b043fecd54a0cd2a67fbbe27c0394)をクリックします。

次に、パラメータを定義し、\[保存]ボタンをクリックします。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/define_parameter.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=5b18fbed3e5994b9366967e8fb4f41bf)

最後に、クエリ内の変数名を@{}で囲むと、以下のようになります。

```
select *
from airline_2016_01
where carrier = @{carrier}
```

を入力すると、以下のようなパラメータが表示されます。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/insert_param_in_query.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=4150df940c59d8fb22d85897f5c8f6bb)のようになります。

詳しくはこちらの[ブログ記事](https://exploratory.io/note/kanaugust/An-Introduction-to-Parameter-in-Exploratory-WCO4Vgn7HJ)をご覧ください。

## 6. AWSセキュリティグループの設定

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/redshift4.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=be5bd97c83d9caffbf9a0089ad512980)

データベース接続エラーが発生した場合は、AWSコンソールにアクセスし、Redshiftクラスタに関連付けられたセキュリティグループ（Inbound側に）にクライアントPCのIPアドレスが追加されていることを確認してください。

## 7. 実際の行数

パフォーマンスの観点から、クエリ全体を再実行しないと取得できない、実際の行数を表示しないようにしました。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/sql_number_of_rows.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=da1ad82d42a81df0744d4dbf45f6d9a4)

それでもクエリの結果の実数を表示したい場合は、システム設定により表示させることができます。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/num_of_rows_config_menu.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=28cb3b60e8fb0cbf75446c99c84cfc53)

そして、"SQLデータインポートダイアログで実際の行数を表示"を「はい」に設定します。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/num_of_rows_config.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=66742c51a94f27d604e244fe31a7b4c3)

これにより、以下のように実際の行数が表示されます。

![](https://mintcdn.com/exploratory-inc/_cHetG8yIT5gmGiQ/ja/import/images/actual_num_of_rows.png?fit=max\&auto=format\&n=_cHetG8yIT5gmGiQ\&q=85\&s=51a79e66d3390f87916c4b08e3cbb4e5)

## 8. Rとdplyrを使ったAmazon Redshiftの探索的データ分析

ブログ記事R & dplyrによるAmazon Redshiftの探索的データ分析への[リンクはこちらです](https://blog.exploratory.io/exploratory-data-analysis-for-amazon-redshift-with-r-dplyr-9a14441020eb)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.