Entities, Properties, and Keys
Data objects in the App Engine Datastore are known as entities . An entity has one or more named properties , each of which can have one or more values. Entities of the same kind need not have the same properties, and an entity's values for a given property need not all be of the same data type. (If necessary, an application can establish and enforce such restrictions in its own data model.)
The Datastore supports a variety of data types for property values . These include, among others:
- Integers
- Floating-point numbers
- Strings
- Dates
- Binary data
Each entity in the Datastore has a key that uniquely identifies it. The key consists of the following components:
- The namespace of the entity, which allows for multitenancy
- The kind of the entity, which categorizes it for the purpose of Datastore queries
-
An
identifier
for the individual entity, which can be either
- a key name string
- an integer numeric ID
- An optional ancestor path locating the entity within the Datastore hierarchy
An application has access only to entities it has created itself; it can't access data belonging to other applications. It can fetch an individual entity from the Datastore using the entity's key, or it can retrieve one or more entities by issuing a query based on the entities' keys or property values.
The Java App Engine SDK includes a simple Java API, provided in the package
com.google.appengine.api.datastore
, that supports the features of the Datastore directly. All of the examples in this document are based on this low-level Java API; you can choose to use it either directly in your application or as a basis on which to build your own data management layer.
The Datastore itself does not enforce any restrictions on the structure of entities, such as whether a given property has a value of a particular type; this task is left to the application.
Contents
- Kinds and identifiers
- Ancestor paths
- Transactions and entity groups
- Properties and value types
- Working with entities
- Understanding write costs
Kinds and identifiers
Each Datastore entity is of a particular
kind,
which categorizes the entity for the purpose of queries: for instance, a human resources application might represent each employee at a company with an entity of kind
Employee
. In the Java Datastore API, you specify an entity's kind when you create it, as an argument to the
Entity()
constructor. All kind names that begin with two underscores (
__
) are reserved and may not be used.
The following example creates an entity of kind
Employee
, populates its property values, and saves it to the Datastore:
import java.util.Date;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity employee = new Entity("Employee");
employee.setProperty("firstName", "Antonio");
employee.setProperty("lastName", "Salieri");
Date hireDate = new Date();
employee.setProperty("hireDate", hireDate);
employee.setProperty("attendedHrTraining", true);
datastore.put(employee);
In addition to a kind, each entity has an identifier , assigned when the entity is created. Because it is part of the entity's key, the identifier is associated permanently with the entity and cannot be changed. It can be assigned in either of two ways:
- Your application can specify its own key name string for the entity.
- You can have the Datastore automatically assign the entity an integer numeric ID .
To assign an entity a key name, provide the name as the second argument to the constructor when you create the entity:
Entity employee = new Entity("Employee", "asalieri");
To have the Datastore assign a numeric ID automatically, omit this argument:
Entity employee = new Entity("Employee");
Assigning identifiers
The Datastore can be configured to generate auto IDs using two different auto id policies :-
The
default
policy generates a random sequence of IDs that are approximately uniformly distributed. Each ID can be up to 16 decimal digits long. -
The
legacy
policy creates a sequence of non-consecutive smaller integer IDs.