# Embedded Apps CおよびC++インポート連携

C++をベースとするサーバーサイドアプリケーション、ゲーム、およびIoTデバイスからデータを収集できます。この記事では、[Fluent Bit](http://fluentbit.io/)を使用して、CおよびC++アプリケーションからデータを確実にインポートする方法について説明します。

- [前提条件](/ja/int/embedded-apps-c-and-c-import-integration#h1_1644201065)
- [Fluent Bitについて](/ja/int/embedded-apps-c-and-c-import-integration#h1__1874598616)
- [Fluent Bitの組み込みとTreasure Dataへの接続](/ja/int/embedded-apps-c-and-c-import-integration#h1_899088197)
- [Treasure APIキーの取得](/ja/int/embedded-apps-c-and-c-import-integration#h2_1456347407)
- [設定ファイルの準備](/ja/int/embedded-apps-c-and-c-import-integration#h2_1550180863)
- [fluent-bitをアプリケーションに組み込む](/ja/int/embedded-apps-c-and-c-import-integration#h2__622390659)
- [関連情報](/ja/int/embedded-apps-c-and-c-import-integration#h1__78781730)


# 前提条件

- C/C++アプリケーション開発の基礎知識
- Treasure Dataの基礎知識


# Fluent Bitについて

Fluent Bitは、組み込みオペレーティングシステム向けに特化したオープンソースのデータコレクターツールで、Treasure Dataによって開発されています。Fluent Bitはスタンドアロンツールとして使用できるほか、CまたはC++アプリケーションに組み込むこともできます。

C/C++アプリケーションにFluent Bitを組み込むことで、アプリケーション内に堅牢で軽量なデータ収集メカニズム(バッファリング、リトライなど)を実装できます。

# Fluent Bitの組み込みとTreasure Dataへの接続

## Treasure APIキーの取得

Write-Only APIキーを[こちら](/products/my-settings/getting-your-api-keys)から取得してください。

## 設定ファイルの準備

Treasure DataのWrite-Only APIキー、宛先のdatabase、およびtable名を指定して設定ファイルを準備してください。設定例は[こちら](https://github.com/fluent/fluent-bit/blob/master/conf/out_td.conf)で入手できます。


```
# Treasure Data Output
# ===================
# This configuration file specifies the information to be used
# when flushing information to TreasureData.com service. All
# key fields in the 'TD' section are mandatory.
[TD]
    # API
    # ===
    # The TreasureData API key. To obtain the TD API Key log into your
    # Treasure Data Console:
    #
    # 1. Go to https://console.treasuredata.com/users/current
    # 2. API Keys box: copy the API key hash
    API YOUR_WRITE_ONLY_API_KEY

    # Database
    # ========
    # Specify the name of your database. The database must exist.
    Database db_example

    # Table
    # =====
    # Specify the database table name where the records will be stored
    Table    table_example
```

## fluent-bitをアプリケーションに組み込む

GitHubからソースコード全体を取得してください。

- [https://github.com/fluent/fluent-bit](https://github.com/fluent/fluent-bit)


Fluent Bitをビルドすると、最終的なバイナリと、プロジェクトにリンクできるlibfluent-bit.soという共有ライブラリバージョンが作成されます。このライブラリを使用すると、エンジンにデータをエンキューして素早く戻ることができます。すべての処理は、非同期モードで動作する単一のposixスレッドで実行されます。呼び出し元のプロセスやスレッドはブロックされません。

以下は、Treasure Dataにデータを送信するサンプルプログラムです。


```
#include <unistd.h>
#include <fluent-bit.h>

int main(int argc, char **argv)
{
    int i;
    int n;
    int ret;
    char tmp[256];
    struct flb_config *config;

    if (argc < 2) {
        fprintf(stderr, "Usage: td /path/to/configuration.file\n");
        exit(EXIT_FAILURE);
    }

    /* Create configuration context */
    config = flb_config_init();
    if (!config) {
        exit(EXIT_FAILURE);
    }

    /* Enable verbose messages */
    flb_config_verbose(FLB_TRUE);

    /* Load a configuration file (required by TD output plugin) */
    ret = flb_lib_config_file(config, argv[1]);

    /* Initialize library */
    ret = flb_lib_init(config, (char *) "td");
    if (ret != 0) {
        exit(EXIT_FAILURE);
    }

    /* Start the background worker */
    flb_lib_start(config);

    /* Push some data */
    for (i = 0; i < 100; i++) {
        n = snprintf(tmp, sizeof(tmp) - 1, "{\"key\": \"val %i\"}", i);
        flb_lib_push(config, tmp, n);
    }

    flb_lib_stop(config);
    return 0;
}
```

`fluent-bit.h`ヘッダーをインクルードし、正しい順序で関数を使用するだけです。各関数の説明は以下で入手できます:

- [Fluent Bit Documentation for Developers](http://fluentbit.io/documentation/)


# 関連情報

詳細情報は以下のリンクで入手できます:

- [Fluent Bit](http://fluentbit.io/)
- [Fluent Bit Source Code](http://github.com/fluent/fluent-bit/)
- [Fluent Bit Documentation for Developers](http://fluentbit.io/documentation/)