# Bulk Importでの環境変数の使用

場合によっては、Embulk設定ファイルに接続詳細を含めることが理想的でないことがあります。特定の詳細を隠したりマスクしたりする必要がある状況では、設定ファイルに環境変数を埋め込むことができます。

Embulkでの環境変数の使用は実験的な機能です。この機能は将来のリリースで変更または削除される可能性があります。

* [前提条件](/ja/products/customer-data-platform/integration-hub/batch/import/using-environment-variables-with-bulk-import#prerequisites)
* [環境変数の命名規則の理解](/ja/products/customer-data-platform/integration-hub/batch/import/using-environment-variables-with-bulk-import#understanding-environment-variable-naming-conventions)
* [環境変数の設定](/ja/products/customer-data-platform/integration-hub/batch/import/using-environment-variables-with-bulk-import#setting-environment-variables)
* [config.yml.liquidファイルの例](/ja/products/customer-data-platform/integration-hub/batch/import/using-environment-variables-with-bulk-import#example-configymlliquid-file)


# 前提条件

* Treasure Dataの基本的な知識。
* [Embulk](http://www.embulk.org/docs/)の基本的な知識。
* 環境に設定された環境変数。
* Embulk変数のベースとなっている[Liquid Template Engineのドキュメント](https://shopify.github.io/liquid/)を確認してください。


# 環境変数の命名規則の理解

一部の環境変数を置き換え、変数命名規則に従う必要があります：`{{ env.replaced_detail }}`。`replaced_detail`は環境変数の名前です。例えば、データベースパスワードの環境変数を設定し、それを`DB_PASSWORD`と名付けた場合、設定ファイル内の値は以下のようになります：

`{{ env.DB_PASSWORD }}`

規則は、二重波括弧{{ }}内で`env.`の後に環境変数の名前を続けます。

# 環境変数の設定

環境変数は、実行中のプロセスがタスクを完了するために使用できる動的に名前付けされた値です。例えば、実行中のプロセスはDB_HOST環境変数の値をクエリしてMySQLデータベースのIPアドレスを発見したり、API_KEY変数を使用してTreasure Dataで認証するためのAPIキーの値を見つけることができます。環境変数を設定または変更する手順はプラットフォームによって異なります。例えば、[Mac OS Xでの環境変数](http://osxdaily.com/2015/07/28/set-enviornment-variables-mac-os-x/)を参照してください。

設定ファイルで変数を使用するには：

1. .yml設定ファイルの拡張子が`.yml.liquid`で終わるように名前を変更します。例えば、設定ファイルが元々`config.yml`という名前だった場合、`config.yml.liquid`に名前を変更します。
2. 変数命名規則を使用して接続詳細を置き換え、環境変数を設定ファイルに挿入します。
3. プレビューモードでEmbulkを実行して変更を検証します。例：



```bash
embulk preview config.yml.liquid
```

1. Embulkを実行して新しい設定ファイルの詳細を設定します。例：



```bash
embulk run config.yml.liquid
```

# config.yml.liquidファイルの例

例えば、元のconfig.ymlファイルが以下の場合：


```yaml
    in:
        type: mysql
        host: localhost
        port: 3306
        user: username
        password: password
        database: mysql_db
        select: "col1, col2, datecolumn"
        where: "col4 != 'a'"
    out:
        type: td
        apikey: xxxxxxxxxxxx
        endpoint: api.treasuredata.com
        database: dbname
        table: tblname
        time_column: datecolumn
        mode: replace
        # by default mode: append is used, if not defined.
        # Imported records are appended to the target table with this mode.
        # mode: replace, replaces existing target table
        default_timestamp_format: '%d/%m/%Y'
```

MySQLのポート、ユーザー名、パスワード、データベースを隠したい場合。出力セクションでは、APIキーを隠したい場合があります。正しい命名規則`{{ env.replaced_details }}`を使用すると、ファイルは以下のようになります：


```yaml
    in:
        type: mysql
        host: {{ env.db_host }}
        port: {{ env.db_port }}
        user: {{ env.db_username }}
        password: {{ env.db_password }}
        database: {{ env.db_name }}
        select: "col1, col2, datecolumn"
        where: "col4 != 'a'"
    out:
        type: td
        apikey: {{ env.td_apikey }}
        endpoint: {{ env.api_endpoint }}
        database: {{ env.td_db_name }}
        table: {{ env.td_table_name }}
        time_column: datecolumn
        mode: replace
        #by default mode: append is used, if not defined. Imported records
        #are appended to the target table with this mode.
        #mode: replace, replaces existing target table
        default_timestamp_format: '%d/%m/%Y'.
```