Java 8 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Java 8 アプリケーションをデプロイできなくなります。既存の Java 8 アプリケーションは、
非推奨になる日付以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Java に移行することをおすすめします。
クエリ結果の取得
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
クエリを作成した後、さまざまな取得オプションを指定して、クエリから返される結果をさらに制御できます。アプリに対するクエリの構成について詳しくは、データストア クエリをご覧ください。
単一のエンティティを取得する
クエリと一致する単一のエンティティを取得するには、PreparedQuery.asSingleEntity()
メソッドを使用します。
このメソッドは、インデックスで最初に見つかった、クエリと一致する結果を返します(一致する結果が複数ある場合は、TooManyResultsException
がスローされます)。
クエリ結果を反復処理する
PreparedQuery.asIterable()
メソッドと PreparedQuery.asIterator()
メソッドを使用してクエリの結果を反復処理する場合、Cloud Datastore はバッチで結果を取得します。デフォルトでは、各バッチに 20 件の結果が含まれますが、この値は FetchOptions.chunkSize()
を使用して変更できます。すべての結果が返されるかリクエストがタイムアウトするまでクエリ結果の反復処理を繰り返すことができます。
エンティティから選択したプロパティを取得する
エンティティ全体ではなくエンティティのプロパティの中から選択したもののみを取得するには、射影クエリを使用します。このタイプのクエリは、エンティティ全体を返すよりも迅速かつ低コストで実行できます。
同様に、キーのみのクエリではエンティティ全体ではなく一致するエンティティのキーのみが返されるので、時間とリソースを節約できます。このタイプのクエリを作成するには、Query.setKeysOnly()
メソッドを使用します。
クエリに上限を設定する
クエリに対して上限を設定することで、1 つのバッチで返される結果の最大数を制御できます。次の例では、最も背の高い 5 名を Cloud Datastore から取得します。
次のステップ
- Cloud Datastore のクエリにおける一般的な制限について学習します。
- クエリの結果を一括して取得できるクエリカーソルについて学習します。
- データの整合性とそれが Cloud Datastore における異なるタイプのクエリでどのように動作するか理解します。
- Cloud Datastore のクエリの基本的な構文と構造について学習します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThis API supports first-generation runtimes and is applicable when upgrading to corresponding second-generation runtimes, while migration to Java 11/17 runtimes requires referring to a specific migration guide.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ePreparedQuery.asSingleEntity()\u003c/code\u003e retrieves a single entity matching a query, throwing a \u003ccode\u003eTooManyResultsException\u003c/code\u003e if multiple matches exist.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ePreparedQuery.asIterable()\u003c/code\u003e and \u003ccode\u003ePreparedQuery.asIterator()\u003c/code\u003e allow iterating through query results in batches, with the batch size adjustable via \u003ccode\u003eFetchOptions.chunkSize()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries retrieve only selected properties, and keys-only queries retrieve only keys, both being faster and less resource-intensive than full-entity queries.\u003c/p\u003e\n"],["\u003cp\u003eA limit can be set on queries to control the maximum number of results, as demonstrated in the example that retrieves the five tallest people.\u003c/p\u003e\n"]]],[],null,["# Retrieving query results\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| java-gen2\n|\n| /services/access). If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\nAfter constructing a query, you can specify a number of retrieval options to\nfurther control the results it returns.\nSee [datastore queries](/appengine/docs/legacy/standard/java/datastore/queries) for more information on structuring queries for your app.\n\nRetrieving a single entity\n--------------------------\n\n\u003cbr /\u003e\n\nTo retrieve just a single entity matching your query, use the method [`PreparedQuery.asSingleEntity()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/PreparedQuery#asSingleEntity--): \n\n Query q =\n new Query(\"Person\")\n .setFilter(new FilterPredicate(\"lastName\", FilterOperator.EQUAL, targetLastName));\n\n PreparedQuery pq = datastore.prepare(q);\n Entity result = pq.asSingleEntity();\n\nThis returns the first result found in the index that matches the query.\n\n(If there is more than one matching result, it throws a [`TooManyResultsException`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/PreparedQuery.TooManyResultsException).)\n\n\nIterating through query results\n-------------------------------\n\nWhen iterating through the results of a query using the [`PreparedQuery.asIterable()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/PreparedQuery#asIterable()) and [`PreparedQuery.asIterator()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/PreparedQuery#asiterator_1) methods, Cloud Datastore retrieves the results in batches. By default each batch contains 20 results, but you can change this value using [`FetchOptions.chunkSize()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/FetchOptions#chunksize). You can continue iterating through query results until all are returned or the request times out.\n\nRetrieving selected properties from an entity\n---------------------------------------------\n\nTo retrieve only selected properties of an entity rather than the entire entity, use a [*projection query*](/appengine/docs/legacy/standard/java/datastore/projectionqueries). This type of query runs faster and costs less than one that returns complete entities.\n\nSimilarly, a [*keys-only query*](/appengine/docs/legacy/standard/java/datastore/queries#keys-only_queries) saves time and resources by returning just the keys to the entities it matches, rather than the full entities themselves. To create this type of query, use the [`Query.setKeysOnly()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Query#setkeysonly) method: \n\n Query q = new Query(\"Person\").setKeysOnly();\n\nSetting a limit for your query\n------------------------------\n\nYou can specify a *limit* for your query to control the maximum number of results returned in one batch. The following example retrieves the five tallest people from Cloud Datastore: \n\n private List\u003cEntity\u003e getTallestPeople() {\n DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();\n\n Query q = new Query(\"Person\").addSort(\"height\", SortDirection.DESCENDING);\n\n PreparedQuery pq = datastore.prepare(q);\n return pq.asList(FetchOptions.Builder.withLimit(5));\n }\n\nWhat's next?\n------------\n\n- Learn the [common restrictions](/appengine/docs/legacy/standard/java/datastore/query-restrictions) for queries on Cloud Datastore.\n- Learn about [query cursors](/appengine/docs/legacy/standard/java/datastore/query-cursors), which allow an application to retrieve a query's results in convenient batches.\n- [Understand data consistency](/appengine/docs/legacy/standard/java/datastore/data-consistency) and how data consistency works with different types of queries on Cloud Datastore.\n- Learn the [basic syntax and structure of queries](/appengine/docs/legacy/standard/java/datastore/queries) for Cloud Datastore."]]