{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-@l10n/ja/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["admonition"]},"type":"markdown"},"seo":{"title":"TD Toolbelt を使用してクエリ結果を出力する","description":"Treasure Data Product Documentation · Collect and Unify · Segment and Activate · Experiment and Analyze · Decisioning Automate with AI Scale and Trust.","siteUrl":"https://docs.treasuredata.com","lang":"en-US","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"td-toolbelt-を使用してクエリ結果を出力する","__idx":0},"children":["TD Toolbelt を使用してクエリ結果を出力する"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"オンデマンドジョブの場合","__idx":1},"children":["オンデマンドジョブの場合"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["オンデマンドジョブの場合は、td query コマンドに --result / -r オプションを追加するだけです。ジョブが完了すると、結果がコレクションに書き込まれます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"$ td query --result 'mongodb://user:password@host:1234/database/collection' \\\n-w -d testdb \"SELECT code, COUNT(1) FROM www_access GROUP BY code\"\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"スケジュールされたジョブの場合","__idx":2},"children":["スケジュールされたジョブの場合"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["スケジュールされたジョブの場合は、ジョブをスケジュールする際に --result / -r オプションを追加します。ジョブが実行されるたびに、結果が mytbl に書き込まれます。"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"bash","header":{"controls":{"copy":{}}},"source":"$ td result:create mydb 'mongodb://user:password@host:1234/database'\n$ td sched:create hourly_count_example \"0 * * * *\" \\\n-d testdb \"select count(*) from www_access\" --result mydb:mycollection\n","lang":"bash"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"フォーマット","__idx":3},"children":["フォーマット"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["結果出力先は、次の形式の URL で表されます:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mongodb://username:password@host/database/collection\nmongodb://username:password@host:port/database/collection\n"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["各項目の説明:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["mongodb"]}," は MongoDB への結果出力を識別します。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["username"]}," と ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["password"]}," は MongoDB インスタンスへの認証情報です。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["hostname"]}," は MongoDB インスタンスのホスト名です。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["port"]}," は MongoDB インスタンスにアクセスするためのポート番号です。これはオプションです。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["database"]}," は宛先データベースの名前です。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["collection"]}," は宛先コレクションの名前です。"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"モード","__idx":4},"children":["モード"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["次のモードを使用して、データを追加または削除できます:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mongodb://user:password@host:1234/database/collection # append\nmongodb://user:password@host:1234/database/collection?mode=append # append\nmongodb://user:password@host:1234/database/collection?mode=replace # replace\nmongodb://user:password@host:1234/database/collection?mode=truncate # truncate\nmongodb://user:password@host:1234/database/collection?mode=update&unique=key1 # update\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"modeappend-デフォルト","__idx":5},"children":["mode=append (デフォルト)"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["これはデフォルトのモードです。クエリ結果はコレクションに追加されます。コレクションがまだ存在しない場合は、新しいコレクションが作成されます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["この方法は",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/customer-data-platform/data-workbench/databases/data-management"},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["アトミック"]}]},"です。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"modereplace","__idx":6},"children":["mode=replace"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["コレクションが既に存在する場合、既存のコレクションの行はクエリ結果で置き換えられます。コレクションがまだ存在しない場合は、新しいコレクションが作成されます。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["単一のトランザクション"]},"で次の3つのステップを実行することにより、",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["アトミック性"]},"(コレクションのコンシューマーが常に一貫性のあるデータを持つようにする)を実現します。"]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["一時コレクションを作成します。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["一時コレクションに書き込みます。"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["RENAME コマンドを使用して、既存のコレクションを一時コレクションで置き換えます。"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["この方法は",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/customer-data-platform/data-workbench/databases/data-management"},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["アトミック"]}]},"です。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"modetruncate","__idx":7},"children":["mode=truncate"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["システムはまず既存のコレクションを切り捨て、次にクエリ結果を挿入します。コレクションがまだ存在しない場合は、新しいコレクションが作成されます。"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["REPLACE とは異なり、TRUNCATE はコレクションのインデックスを保持します。"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["この方法は",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/products/customer-data-platform/data-workbench/databases/data-management"},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["アトミック"]}]},"です。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"modeupdate","__idx":8},"children":["mode=update"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["このモードは、MongoDB の find と「upsert」メソッドを使用します(",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"http://docs.mongodb.org/manual/reference/method/Bulk.find.upsert/"},"children":["MongoDB のドキュメント"]},"を参照)。簡単に言うと、ユニークインデックスまたは主キーで重複値が発生する場合を除き、行が挿入され、重複する場合は更新が実行されます。引数で指定したフィールドに既にユニークインデックスを作成していることを確認してください。このモードを使用する場合、unique オプションが必須です。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["MongoDB はトランザクションをサポートしていないため、このモードではトランザクションのアトミック性を保証できません。"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"unique","__idx":9},"children":["Unique"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["このオプションは update モードでのみ関連し、必須です。MongoDB コレクションを更新するために使用するユニークキーまたはキー(カンマ区切り)のカラム名を指定します。"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["例"]}]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mongodb://user:password@host:1234/database/collection?mode=update&unique=key1\nmongodb://user:password@host:1234/database/collection?mode=update&unique=key1,key2,key3\n"},"children":[]}]},"headings":[{"value":"TD Toolbelt を使用してクエリ結果を出力する","id":"td-toolbelt-を使用してクエリ結果を出力する","depth":1},{"value":"オンデマンドジョブの場合","id":"オンデマンドジョブの場合","depth":2},{"value":"スケジュールされたジョブの場合","id":"スケジュールされたジョブの場合","depth":2},{"value":"フォーマット","id":"フォーマット","depth":2},{"value":"モード","id":"モード","depth":2},{"value":"mode=append (デフォルト)","id":"modeappend-デフォルト","depth":3},{"value":"mode=replace","id":"modereplace","depth":3},{"value":"mode=truncate","id":"modetruncate","depth":3},{"value":"mode=update","id":"modeupdate","depth":3},{"value":"Unique","id":"unique","depth":2}],"frontmatter":{"seo":{"title":"TD Toolbelt を使用してクエリ結果を出力する"}},"lastModified":"2026-01-09T05:58:30.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/ja/int/using-the-cli-to-output-query-results","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}