# Node.js アプリケーション インポート インテグレーション Treasure Data は [td-agent](https://docs.treasuredata.com/smart/project-product-documentation/about-treasure-data-s-server-side-agent) を提供しており、サーバーサイドのログとイベントを収集し、Node.js アプリケーションからデータをシームレスにインポートできます。 fluent-logger-node ライブラリにより、Node.js アプリケーションはローカルの td-agent にレコードを送信できます。 td-agent は、5 分ごとにデータをクラウドにアップロードします。デーモンがローカルノードで実行されているため、ログの遅延は無視できます。 以下のトピックに進んでください: ## 前提条件 - Node.js と NPM の基本的な知識。 - [TD Toolbelt](https://toolbelt.treasuredata.com/) を含む Treasure Data の基本的な知識。 - Node.js 0.6 以上(ローカルテスト用)。 ## td-agent のインストール アプリケーションサーバーに `td-agent` をインストールします。td-agent はアプリケーションサーバー内に配置され、アプリケーションログをクラウドにアップロードすることに重点を置いています。 ![](/assets/image2020-12-2_14-6-46.1754f4e3154420d54cc976b16d454ef773c83ad5fc4b8580d99a87302c614932.7c05c672.png) [fluent-logger-node](https://github.com/fluent/fluent-logger-node) ライブラリにより、Node.js アプリケーションはローカルの td-agent にレコードを送信できます。td-agent は、5 分ごとにデータをクラウドにアップロードします。デーモンがローカルノードで実行されているため、ログの遅延は無視できます。 ## td-agent インストールオプション `td-agent` をインストールするには、環境に基づいて以下のコマンドのいずれかを実行します。エージェントプログラムは、rpm/deb/dmg などの各プラットフォームのパッケージ管理ソフトウェアを使用して自動的にインストールされます。 ### RHEL/CentOS 5,6,7 ```bash $ curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh ``` ### Ubuntu と Debian ```bash # 18.04 Bionic $ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent3.sh | sh # 16.04 Xenial (64bit only) $ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-xenial-td-agent3.sh | sh ``` ### Amazon Linux Amazon Linux 1 または Amazon Linux 2 を選択できます。[Installing td-agent on Amazon Linux](https://docs.treasuredata.com/smart/project-product-documentation/installing-td-agent-on-aws-linux) を参照してください。 ### MacOS X 10.11+ ```bash $ open 'https://td-agent-package-browser.herokuapp.com/3/macosx/td-agent-3.1.1-0.dmg' ``` MacOS X 10.11.1 (El Capitan) ではいくつかのセキュリティ変更が導入されました。td-agent のインストール後、**/Library/LaunchDaemons/td-agent.plist** ファイルを編集して **/usr/sbin/td-agent** を **/opt/td-agent/usr/sbin/td-agent** に変更してください。 ### Windows Server 2012+ Windows のインストールには、以下に詳述されている手順が必要です: - [Installing Treasure Agent using .msi Installer (Windows)](https://docs.fluentd.org/installation/install-by-msi) ### Opscode Chef Repository ``` $ echo 'cookbook "td-agent"' >> Berksfile $ berks install ``` [AWS Elastic Beanstalk](https://github.com/treasure-data/elastic-beanstalk-td-agent) もサポートされています。Windows は現在サポートされていません。 ## /etc/td-agent/td-agent.conf の変更 `apikey` オプションを設定して API キーを指定します。API キーは TD Console のプロフィールから確認できます。td-agent.conf ファイルで `apikey` オプションを設定します。 ``` # Treasure Data Input and Output source type forward port 24224 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 ``` `YOUR_API_KEY` should be your actual apikey string. You can retrieve your API key from your profiles in TD Console. Using a [write-only API key](/products/my-settings/getting-your-api-keys) is recommended. 以下の行が配置されたら、エージェントを再起動してください。 ``` # Linux $ sudo /etc/init.d/td-agent restart # MacOS X $ sudo launchctl unload /Library/LaunchDaemons/td-agent.plist $ sudo launchctl load /Library/LaunchDaemons/td-agent.plist ``` td-agent はポート 24224 経由でデータを受け入れ、データをバッファリングし (*var/log/td-agent/buffer/td*)、自動的にデータをクラウドにアップロードします。 ## fluent-logger-node の使用 #### 最新バージョンの取得 [fluent-logger-node](https://github.com/fluent/fluent-logger-node) の最新バージョンを取得します。 #### サンプルアプリケーション fluent-logger-node を使用したサンプル [Express](http://expressjs.com/) アプリは以下の通りです: #### package.json ``` { "name": "node-example", "version": "0.0.1", "dependencies": { "express": "2.5.9", "fluent-logger": "0.1.0" } } ``` 次に *npm* を使用して依存関係をローカルにインストールします: ``` $ npm install fluent-logger@0.1.0 ./node_modules/fluent-logger express@2.5.9 ./node_modules/express |-- qs@0.4.2 |-- mime@1.2.4 |-- mkdirp@0.3.0 |-- connect@1.8.6 (formidable@1.0.9) ``` #### web.js これは最もシンプルな Web アプリです。 ``` var express = require('express'); var app = express.createServer(express.logger()); var logger = require('fluent-logger'); logger.configure('td.test_db', {host: 'localhost', port: 24224}); app.get('/', function(request, response) { logger.emit('follow', {from: 'userA', to: 'userB'}); response.send('Hello World!'); }); var port = process.env.PORT || 3000; app.listen(port, function() { console.log("Listening on " + port); }); ``` ## データインポートの確認 アプリを実行し、ブラウザで `http://localhost:3000/` にアクセスします。 ``` $ node web.js ``` SIGUSR1 シグナルが送信され、td-agent のバッファがフラッシュされます。アップロードは直ちに開始されます。 ``` # Linux $ kill -USR1 `cat /var/run/td-agent/td-agent.pid` # MacOS X $ sudo kill -USR1 `sudo launchctl list | grep td-agent | cut -f 1` ``` データが正常にアップロードされたことを確認するには、次のように *td tables* コマンドを実行します: ``` $ td tables +------------+------------+------+-----------+ | Database | Table | Type | Count | +------------+------------+------+-----------+ | test_db | follow | log | 1 | +------------+------------+------+-----------+ ``` | | | --- | | post() の最初の引数はデータベース名とテーブル名を決定します。`td.test\_db.test\_table` を指定すると、データはデータベース *test_db* 内のテーブル *test_table* にインポートされます。これらはアップロード時に自動的に作成されます。 | # 本番環境への展開 ## td-agent の高可用性構成 高トラフィックの Web サイト(5 つ以上のアプリケーションノード)の場合は、td-agent の高可用性構成を使用して、データ転送の信頼性とクエリパフォーマンスを向上させます。 - [High-Availability Configurations of td-agent](https://docs.treasuredata.com/smart/project-product-documentation/configuring-td-agent-for-high-availability) ## td-agent の監視 td-agent 自体の監視も重要です。td-agent の一般的な監視方法については、以下のドキュメントを参照してください。 - [Monitoring td-agent](https://docs.treasuredata.com/smart/project-product-documentation/monitoring-td-agent) td-agent は [Fluentd プロジェクト](http://fluentd.org/)の下で完全にオープンソース化されています。 # 次のステップ 従来の RDBMS よりも柔軟なスキーマメカニズムを提供しています。クエリには、Hive と Presto のクエリ言語を活用します。 - [Schema Management](https://docs.treasuredata.com/smart/project-product-documentation/schema-management) - [Presto Query Language](https://api-docs.treasuredata.com/en/tools/presto/quickstart/#choosing-an-analytics-engine) - [Hive Query Language](https://api-docs.treasuredata.com/en/tools/hive/quickstart/) - [Programmatic Access with REST API and its Bindings](https://api-docs.treasuredata.com/en/api/td-api/td-client/)