privatevoidprintGuestbookEntries(DatastoreServicedatastore,Queryquery,PrintWriterout){List<Entity>guests=datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));for(Entityguest:guests){Stringcontent=(String)guest.getProperty("content");Datestamp=(Date)guest.getProperty("date");out.printf("Message %s posted on %s.\n",content,stamp.toString());}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eThis API supports first-generation runtimes and is applicable when upgrading to second-generation runtimes, and if you are moving to App Engine Java 11/17, refer to the migration guide for details.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries allow you to retrieve only specific properties of an entity from Datastore, reducing latency and costs compared to retrieving entire entities.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries are built by adding properties to a \u003ccode\u003eQuery\u003c/code\u003e object using the \u003ccode\u003eaddProjection()\u003c/code\u003e method, and the type specified for each property must match its original definition type.\u003c/p\u003e\n"],["\u003cp\u003eUsing \u003ccode\u003esetDistinct()\u003c/code\u003e in projection queries ensures that only unique results are returned, eliminating duplicate entities with the same projected property values.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries are limited to indexed properties, cannot project the same property more than once, and properties in equality or membership filters cannot be projected, and results should not be written back to Datastore.\u003c/p\u003e\n"]]],[],null,["# Projection Queries\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\nMost Datastore [queries](/appengine/docs/legacy/standard/java/datastore/queries) return whole [entities](/appengine/docs/legacy/standard/java/datastore/entities) as their results, but often an application is actually interested in only a few of the entity's properties. *Projection queries* allow you to query Datastore for just those specific properties of an entity that you actually need, at lower latency and cost than retrieving the entire entity.\n\nProjection queries are similar to SQL queries of the form: \n\n SELECT name, email, phone FROM CUSTOMER\n\nYou can use all of the filtering and sorting features available for standard entity queries, subject to the [limitations](#Limitations_on_projections) described below. The query returns abridged results with only the specified properties (`name`, `email`, and `phone` in the example) populated with values; all other properties have no data.\n\nUsing projection queries in Java 8\n----------------------------------\n\nTo build a projection query, you create a [`Query`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Query) object and add properties to it using the [`addProjection()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Query#addProjection-com.google.appengine.api.datastore.Projection-) method: \n\n private void addGuestbookProjections(Query query) {\n query.addProjection(new PropertyProjection(\"content\", String.class));\n query.addProjection(new PropertyProjection(\"date\", Date.class));\n }\n\nThe type you specify for each property must match the type you used when you first defined the property with [`Entity.setProperty()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/PropertyContainer#setProperty-java.lang.String-java.lang.Object-). The following example shows how to process the query's results by iterating through the list of entities returned and casting each property value to the expected type: \n\n private void printGuestbookEntries(DatastoreService datastore, Query query, PrintWriter out) {\n List\u003cEntity\u003e guests = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));\n for (Entity guest : guests) {\n String content = (String) guest.getProperty(\"content\");\n Date stamp = (Date) guest.getProperty(\"date\");\n out.printf(\"Message %s posted on %s.\\n\", content, stamp.toString());\n }\n }\n\n| **Note:** Instead of specifying a property's type explicitly to the `Query.addProjection()` method, you can simply pass `null` for the type. The property's value will then be returned as a [`RawValue`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/RawValue) object, which you can test to determine its type. (In the processing example above, you would cast the property to `RawValue` instead of a specific type.)\n\nGrouping^(experimental)^\n------------------------\n\nProjection queries can use the [`setDistinct()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Query#setDistinct-boolean-) method to ensure that only completely unique results will be returned in a result set. This will only return the first result for entities which have the same values for the properties that are being projected. \n\n Query q = new Query(\"TestKind\");\n q.addProjection(new PropertyProjection(\"A\", String.class));\n q.addProjection(new PropertyProjection(\"B\", Long.class));\n q.setDistinct(true);\n q.setFilter(Query.FilterOperator.LESS_THAN.of(\"B\", 1L));\n q.addSort(\"B\", Query.SortDirection.DESCENDING);\n q.addSort(\"A\");\n\nLimitations on projections\n--------------------------\n\nProjection queries are subject to the following limitations:\n\n- **Only indexed properties can be projected.**\n\n Projection is not supported for properties that are not indexed, whether explicitly or implicitly. Long text strings ([`Text`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Text)) and long byte strings ([`Blob`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Blob)) are not indexed.\n- **The same property cannot be projected more than once.**\n\n- **Properties referenced in an equality (`EQUAL`) or membership (`IN`) filter cannot be projected.**\n\n For example, \n\n SELECT A FROM kind WHERE B = 1\n\n is valid (projected property not used in the equality filter), as is \n\n SELECT A FROM kind WHERE A \u003e 1\n\n (not an equality filter), but \n\n SELECT A FROM kind WHERE A = 1\n\n (projected property used in equality filter) is not.\n- **Results returned by a projection query should not be saved back to Datastore.**\n\n Because the query returns results that are only partially populated, you should not write them back to Datastore.\n\nProjections and multiple-valued properties\n------------------------------------------\n\nProjecting a property with multiple values will not populate all values for that property. Instead, a separate entity will be returned for each unique combination of projected values matching the query. For example, suppose you have an entity of kind `Foo` with two multiple-valued properties, `A` and `B`: \n\n entity = Foo(A=[1, 1, 2, 3], B=['x', 'y', 'x'])\n\nThen the projection query \n\n SELECT A, B FROM Foo WHERE A \u003c 3\n\nwill return four entities with the following combinations of values:\n\n`A` = `1`, `B` = `'x'` \n\n`A` = `1`, `B` = `'y'` \n\n`A` = `2`, `B` = `'x'` \n\n`A` = `2`, `B` = `'y'`\n\nNote that if an entity has a multiple-valued property with no values, no entries\nwill be included in the index, and no results for that entity will be returned\nfrom a projection query including that property.\n| **Warning:** Including more than one multiple-valued property in a projection will result in an [exploding index](/appengine/docs/legacy/standard/java/datastore/indexes#index-limits).\n\nIndexes for projections\n-----------------------\n\nProjection queries require all properties specified in the projection to be included in a Datastore [index](/appengine/docs/legacy/standard/java/datastore/indexes). The App Engine development server automatically generates the needed indexes for you in the index configuration file, `datastore-indexes-auto.xml`, which is uploaded with your application.\n\nOne way to minimize the number of indexes required is to project the same properties consistently, even when not all of them are always needed. For example, these queries require two separate indexes: \n\n SELECT A, B FROM Kind\n SELECT A, B, C FROM Kind\n\nHowever, if you always project properties `A`, `B`, and `C`, even when `C` is not required, only one index will be needed.\n\nConverting an existing query into a projection query may require building a new index if the properties in the projection are not already included in another part of the query. For example, suppose you had an existing query like \n\n SELECT * FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nwhich requires the index \n\n Index(Kind, A, B)\n\nConverting this to either of the projection queries \n\n SELECT C FROM Kind WHERE A \u003e 1 ORDER BY A, B\n SELECT A, B, C FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nintroduces a new property (`C`) and thus will require building a new index `Index(Kind,` `A,` `B,` `C)`. Note that the projection query \n\n SELECT A, B FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nwould *not* change the required index, since the projected properties `A` and `B` were already included in the existing query."]]