# Perl Apps Import Integration Treasure Dataは、サーバーサイドのログとイベントを収集し、Perlアプリケーションからシームレスにデータをインポートするために[td-agent](https://docs.treasuredata.com/smart/project-product-documentation/about-treasure-data-s-server-side-agent)を提供しています。 ## 前提条件 - PerlとCPANの基本知識 - 最新バージョンの[TD Toolbelt](https://toolbelt.treasuredata.com/)がインストールされているTreasure Dataの基本知識 - Perl 5.10以上(ローカルテスト用) ## td-agentのインストール アプリケーションサーバーに`td-agent`をインストールします。td-agentはアプリケーションサーバー内に配置され、アプリケーションログのクラウドへのアップロードに専念します。 ![](/assets/image2020-12-2_14-12-51.1754f4e3154420d54cc976b16d454ef773c83ad5fc4b8580d99a87302c614932.23757061.png) [fluent-logger-perl](http://github.com/fluent/fluent-logger-perl)ライブラリにより、Perlアプリケーションはローカルの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 and 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+ ``` $ 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-perlの使用 まず、CPAN経由で*Fluent::Logger*ライブラリをインストールします。 ``` $ cpan cpan[1]> install Fluent::Logger ``` 次に、次のように初期化してレコードを投稿します。 ### test.pl ```perl use Fluent::Logger; my $logger = Fluent::Logger->new( host => '127.0.0.1', port => 24224, tag_prefix => 'td.test_db', ); $logger->post("test_table", { "entry1" => "value1", "entry2" => 2 }); ``` ## データインポートの確認 まず、前述のプログラムを実行します。 ```bash $ perl test.pl ``` 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 | test_table | log | 1 | +------------+------------+------+-----------+ ``` post()の最初の引数は、データベース名とテーブル名を決定します。`td.test\_db.test\_table`を指定すると、データはデータベース*test_db*内のテーブル*test_table*にインポートされます。これらはアップロード時に自動的に作成されます。 # 本番デプロイ ## td-agentの高可用性構成 高トラフィックのウェブサイト(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 Query Languageを活用しています。 - [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/)