# Twitter Tweet Insights Import Integration TwitterからTweets、Retweets、Follower IDs、Follower List、またはUser TimelineをTreasure Dataにインポートできます。 ## 前提条件 - [Toolbelt](https://toolbelt.treasuredata.com/)を含むTreasure Dataの基本知識 - consumer keyとconsumer secretを持つTwitter開発者アプリケーション - Twitter開発環境でTreasure Dataが設定されていること。以下を参照してください: - [https://developer.twitter.com/en/docs/basics/authentication/overview/application-only](https://developer.twitter.com/en/docs/basics/authentication/overview/application-only) - [https://developer.twitter.com/en/docs/basics/authentication/overview/application-only#issuing-application-only-requests](https://developer.twitter.com/en/docs/basics/authentication/overview/application-only#issuing-application-only-requests) - 認証されたTreasure Dataアカウントへのアクセス - 当社のConnectorは、Twitter API access levelとしてElevated accessを必要とするTwitter API v1.1を使用します。 - [https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api](https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api) - 2021年11月15日以前に開発者アカウントの承認を受けた場合、自動的にElevated accessに変換されています。これは、既存のAppsを引き続き使用してstandard v1.1、premium v1.1、およびenterpriseエンドポイントへのリクエストを行うことができ、すべてのユーザーAccess Tokenが引き続き有効であることを意味します。 ## Treasure Data用のTwitter開発環境を確立する Twitterで、Treasure Dataにインポートするために使用するアプリを指定します。 TwitterからTreasure Dataへの認証フローは、一般的に以下の通りです: - Treasure Dataは、認証情報をbearer tokenと交換するために、POST oauth2 / tokenエンドポイントにリクエストを送信します。 - REST APIにアクセスする際、Treasure Dataはbearer tokenを使用して認証します。 ## Twitterでの設定 Twitterアカウント名でログインします。Appsに移動して、consumer keyとconsumer secretを取得します。 ![](/assets/image-20191104-181945.0122e07a3b81a67dac23a939f5c81054c51a96f4a044c5c1aa8c1d5e7fbcb039.cdc70e98.png) ダッシュボードに戻ります。**Account** > **Dev Environments**を選択します。 ![](/assets/image-20191104-181954.80e9a59ead7c72202b4c1ec591bd109f86552b0737ed22519fcc892acba04a33.cdc70e98.png) **Search Tweets 30-Days**ラベルと**Search Tweets Full-Archive**ラベルを選択します。これにより、データのインポートに使用されるTwitter検索エンドポイントAPIを指定しています。[詳細については、30日および全アーカイブ検索に関するTwitterのドキュメントを参照してください](https://developer.twitter.com/en/docs/tweets/search/overview)。 ![](/assets/image-20191104-182008.077ffee363efdf28a571cc0a2220fd71c8c995938fd76c5f3e383ecf6fd47af4.cdc70e98.png) ## TD Consoleを使用して接続を作成する ### 新しい接続を作成する [Treasure Data Connections](https://console.treasuredata.com/connections)に移動し、Tweet Insightsを検索して選択します。 ![](/assets/image-20191104-182017.06db822cff835bec621440d3659460fc16dc026abff229a80432b64e26125f89.cdc70e98.png) **Create**を選択して、認証済み接続を作成します。 以下のダイアログが開きます。 ![](/assets/image-20191104-182033.f75d857c532693c1cf9c50aecac6265e40208ed3b44e33d39c0ae077817c736e.cdc70e98.png) Twitter Appから取得したconsumer keyとconsumer secretを編集します。Twitter Paid Premium Accountかどうかを指定します。 **Continue**を選択します。 ![](/assets/image-20191104-182045.8c8021a235b2f5e0b29790a94d1b7c4282e2333e46f3fd047fa8e7e7ac86cc5f.cdc70e98.png) 新しいTwitter Tweet Insights Connectionに名前を付けます。**Done**を選択します。 ### Twitter Tweet InsightsデータをTreasure Dataに転送する 接続を作成すると、自動的に[Authentications](https://console.treasuredata.com/connections/list)タブに移動します。作成した接続を探して、**New Source**を選択します。 インポートするデータを指定します。 ### ハンドルからTweetsをインポートする ![](/assets/image-20191104-182102.f5b8fcb678a668bda0fa68c7a4656d3900252caa45902a0936b0f71afc2de8b2.cdc70e98.png) パラメータ: - **Data Type:** Tweets(デフォルト)またはAccount - **Object to Import:** データタイプがTweetsの場合: All TweetsまたはRetweets。データタイプがAccountの場合: User Timeline、Follower IDs、またはFollower List - **30-day dev environment**: 過去30日間のTweetsまたはRetweetsを検索するためのTwitter開発環境APIラベル。Accountデータタイプには適用されません。 - **Full archive dev environment**: アーカイブされたTweetsまたはRetweetsの完全検索用の開発環境APIラベル。Accountデータタイプには適用されません。 - **From this screen name or handle**: 必須。値は、ユーザーのTwitter数値アカウントIDまたはTwitterアカウントのユーザー名である必要があります。クエリ対象のデータを指定するために使用します。 - **Include videos**: ビデオリンクを含むTweetsのみをインポートします。 - **From Date**: この時刻以降に作成されたTweetsをインポートします。時刻はUTCで設定されます。 - **To Date**: この時刻までに作成されたTweetsをインポートします。時刻はUTCで設定されます。 - **Incremental**: スケジュールに基づいてインポートする場合、取得されるデータの時間枠は実行ごとに自動的に前進します。たとえば、初期設定が1月1日で期間が10日の場合、最初の実行では1月1日から1月10日までに変更されたデータを取得し、2回目の実行では1月11日から1月20日までを取得する、という具合になります。 ### ハンドルからRetweetsをインポートする ![](/assets/image-20191104-182117.dd81b2ceb2a51d9a3cc67180c1815f372991b1a1d97486b063fb25ca0dc621fb.cdc70e98.png) パラメータ: - **Retweets of this screen name/handle**: Twitterアカウントのユーザー名 - **Include videos**: ビデオを含むRetweetsのみをインポートします。 - **From Date**: この時刻以降に作成されたRetweetsをインポートします。時刻はUTCで設定されます。 - **To Date**: この時刻までに作成されたRetweetsをインポートします。時刻はUTCで設定されます。 - **Incremental**: スケジュールに基づいてインポートする場合、取得されるデータの時間枠は実行ごとに自動的に前進します。たとえば、初期設定が1月1日で期間が10日の場合、最初の実行では1月1日から1月10日までに変更されたデータを取得し、2回目の実行では1月11日から1月20日までを取得する、という具合になります。 ### ハンドルのUser Timelineをインポートする ![](/assets/image-20191104-182131.1633d00f67171e3a127e6c16da0c598815633ea36cf786849e71e39e5ed0cd39.cdc70e98.png) パラメータ: - **From this screen name/handle**: Twitterアカウントのユーザーネーム - **Incremental**: スケジュールに基づいてインポートする場合、取得したデータのTweetの最大IDは各実行時に自動的に前進します。例えば、初期設定の最大IDが1の場合、最初の実行でデータを取得し、返されたTweetの最大IDが100の場合、2回目の実行はID 100から取得し、2回目の実行で返された最大IDでIDを設定します。以降も同様です。 ### Import Follower IDs of handle ![](/assets/image-20191104-182144.b545858dafa7685033b992db6586074f869455d4c8fd03c75c021c60310149a0.cdc70e98.png) パラメータ: - **From this screen name/handle**: Twitterアカウントのユーザーネーム ### Import Follower List of handle ![](/assets/image-20191104-182155.0da0a6925d42b749ab02e7076895b9e56b4ffa95757ab88af9f97ac8a5ab8c2f.cdc70e98.png) パラメータ: - **From this screen name/handle**: Twitterアカウントのユーザーネーム 設定が完了したら、**Next**を選択します。 ### Preview データの[プレビュー](https://docs.treasuredata.com/smart/project-product-documentation/about-data-preview)が表示されます。変更を加える場合は**Advanced Settings**を選択し、それ以外の場合は**Next**を選択します。 ![](/assets/image-20191104-182226.ee4ee536dfd3325463fe40f1946139eaa7f013224ffc78450b9f233e4d27e610.cdc70e98.png) ### Advanced Settings ![](/assets/image-20191104-182235.b40fe4bd87d1418d7a6693ac8f838aa198bcb217951f1e371af7087d76f9ba81.cdc70e98.png) 以下のパラメータを指定できます: - Maximum retry times. 各API呼び出しの最大リトライ回数を指定します。 ``` Type: number Default: 7 ``` - Initial retry interval millisecond. 最初のリトライの待機時間を指定します。 ``` Type: number Default: 1000 ``` - Maximum retry interval milliseconds. リトライ間の最大待機時間を指定します。 ``` Type: number Default: 120000 ``` ### Choose the target database and table 既存のデータベースとテーブルを選択するか、新しいデータベースとテーブルを作成します。 ![](/assets/image-20191104-182247.61f690241468610391783757953bc91da6b756d027bda646cae01b5571ee28e5.cdc70e98.png) 新しいデータベースを作成し、データベースに名前を付けます。**Create new table**についても同様の手順を実行します。 既存のテーブルにレコードを**追加**するか、既存のテーブルを**置換**するかを選択します。 デフォルトキーではなく、異なる**partition key seed**を設定したい場合は、ポップアップメニューを使用して指定できます。 ### Scheduling **When**タブでは、1回限りの転送を指定するか、自動化された定期転送をスケジュールすることができます。 パラメータ - **Once now**: 1回限りのジョブを設定します。 - **Repeat…** - **Schedule**: *@hourly*、*@daily*、*@monthly*の3つのオプションとカスタム*cron*を指定できます。 - **Delay Transfer**: 実行時間の遅延を追加します。 - **TimeZone**: 'Asia/Tokyo'のような拡張タイムゾーン形式をサポートします。 ![](/assets/image-20191104-182257.4a061c0109a8dbfe17c35acc4ec94e9c15fd1010c16e0c6e946c6001a881876b.cdc70e98.png) ### Details 転送に名前を付けて**Done**を選択し、開始します。 ![](/assets/image-20191104-182305.55fbca55be070bc5e08baeb2ae7b6c7a96a4084c2dd0b6d1fcbfda38fb9ac048.cdc70e98.png) 転送が実行された後、**Databases**タブで転送結果を確認できます。 ## Use the Command Line to create your Twitter Tweet Insights connection Treasure Dataコンソールを使用して接続を設定できます。 ### Install the Treasure Data Toolbelt 最新の[Treasure Data Toolbelt](https://toolbelt.treasuredata.com/)をインストールします。 ### Create a Configuration File (load.yml) 設定ファイルには、Twitter Tweet Insightsからコネクタに入力される内容を指定するin:セクションと、コネクタがTreasure Dataのデータベースに出力する内容を指定するout:セクションが含まれます。 以下の例は、インクリメンタルスケジューリングなしでツイートをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: tweets tweet_type: all 30_day_env: xxxxxxxx full_archive_env: xxxxxxxx handle: xxxxxxxx include_video: false from_date: 2019-01-17T00:00:00.000Z to_date: 2019-01-27T00:00:00.000Z tweet_incremental: false out: mode: append ``` 以下の例は、インクリメンタルスケジューリングありでツイートをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: tweets tweet_type: all 30_day_env: xxxxxxxx full_archive_env: xxxxxxxx handle: xxxxxxxx include_video: false from_date: 2019-01-17T00:00:00.000Z to_date: 2019-01-27T00:00:00.000Z tweet_incremental: true out: mode: append ``` 以下の例は、インクリメンタルスケジューリングなしでリツイートをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: tweets tweet_type: retweets 30_day_env: xxxxxxxx full_archive_env: xxxxxxxx handle_retweet: xxxxxxxx include_video: false from_date: 2019-01-17T00:00:00.000Z to_date: 2019-01-27T00:00:00.000Z tweet_incremental: false out: mode: append ``` 以下の例は、インクリメンタルスケジューリングありでリツイートをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: tweets tweet_type: retweets 30_day_env: xxxxxxx full_archive_env: xxxxxxxx handle_retweet: xxxxxxxx include_video: false from_date: 2019-01-17T00:00:00.000Z to_date: 2019-01-27T00:00:00.000Z tweet_incremental: true out: mode: append ``` 以下の例は、インクリメンタルスケジューリングなしでユーザータイムラインをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: account account_type: user account_label: xxxxxxxx account_incremental: false out: mode: append ``` 次の例は、増分スケジューリングを使用してユーザータイムラインをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: account account_type: user account_label: xxxxxxxx account_incremental: true out: mode: append ``` 次の例は、フォロワーIDをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: account account_type: id account_label: xxxxxxxx out: mode: append ``` 次の例は、フォロワーリストをインポートする方法を示しています。 ```yaml in: type: twitter_tweet_insights comsumer_key: xxxxxxxx comsumer_secret: xxxxxxxx is_paid_account: false data_type: account account_type: list account_label: xxxxxxxx out: mode: append ``` ### インポートするデータのプレビュー(オプション) `td connector:preview`コマンドを使用して、インポートするデータをプレビューできます。 ``` $ td connector:preview load.yml ``` ### ロードジョブの実行 ジョブを実行するには、`td connector:issue`を使用します。 ロードジョブを実行する前に、データを保存するデータベースとテーブルを指定する必要があります。例:td_sample_db、td_sample_table ``` $ td connector:issue load.yml \ --database td_sample_db \ --table td_sample_table \ --time-column date_time_column ``` Treasure Dataのストレージは時間によってパーティション分割されているため、`--time-column`オプションを指定することを推奨します。このオプションが指定されていない場合、データコネクターは最初のlong型またはtimestamp型のカラムをパーティショニング時間として選択します。--time-columnで指定するカラムの型は、long型またはtimestamp型である必要があります(プレビュー結果を使用して、利用可能なカラム名と型を確認してください。一般的に、ほとんどのデータ型にはlast_modified_dateカラムがあります)。 データに時間カラムがない場合は、add_timeフィルターオプションを使用してカラムを追加できます。詳細については、[add_time filter](https://docs.treasuredata.com/smart/project-product-documentation/add_time-filter-function)プラグインを参照してください。 `td connector:issue`は、データベース(sample_db)とテーブル(sample_table)がすでに作成されていることを前提としています。データベースまたはテーブルがTDに存在しない場合、`td connector:issue`は失敗します。そのため、データベースとテーブルを手動で作成するか、`td connector:issue`の--auto-create-tableオプションを使用してデータベースとテーブルを自動的に作成する必要があります。 ``` $ td connector:issue load.yml \ --database td_sample_db \ --table td_sample_table \ --time-column date_time_column \ --auto-create-table ``` コマンドラインからロードジョブを送信します。データサイズによっては、処理に数時間かかる場合があります。 ## 付録 ### サンドボックスアプリケーションの制限事項 サンドボックスアカウントのMaxResultsパラメータは100ですが、プレミアムは500です。 リクエストレート制限は、分単位と秒単位の両方の粒度で適用されます。分単位のレート制限は、1分あたり30リクエストです。リクエストは1秒あたり10リクエストにも制限されます。リクエストは、dataエンドポイントとcountsエンドポイントの両方で集計されます。月単位のリクエスト制限も適用されます。サンドボックス環境は、月あたり250リクエストに制限されています。 ユーザーは、リクエストの使用状況と月次クォータについて、アプリケーションダッシュボードを確認する必要があります。