Entities, Properties, and Keys
Data objects in the 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 Google Cloud Datastore API provides methods for creating, modifying and retrieving entities from the Datastore. Entities themselves are represented in API calls in JSON or protocol buffer messages , depending on whether you are using the JSON API or protocol buffer API .
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
. An entity's kind is the kind specified in the last element of its key's
path
array (
JSON
) or repeated
pathElement
field (
protocol buffers
). 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:
Node.js (JSON)
var entity = {
key: { path: [{ kind: 'Employee' }] },
properties: {
firstName: { stringValue: 'Antonio' },
lastName: { stringValue: 'Salieri' },
hireDate: { dateTimeValue: '2013-05-14T13:01:00.234Z' },
attendedHrTraining: { booleanValue: true }
}
};
datastore.commit({
mutation: { insertAutoId: [entity] },
mode: 'NON_TRANSACTIONAL'
}).execute(callback);
Python (Protocol Buffers)
req = datastore.CommitRequest()
req.mode = datastore.CommitRequest.NON_TRANSACTIONAL
employee = req.mutation.insert_auto_id.add()
path_element = employee.key.path_element.add()
path_element.kind = 'Employee'
first_name_property = employee.property.add()
first_name_property.name = 'first_name'
first_name_property.value.string_value = 'Antonio'
last_name_property = employee.property.add()
last_name_property.name = 'last_name'
last_name_property.value.string_value = 'Salieri'
hire_date_property = employee.property.add()
hire_date_property.name = 'hire_date'
hire_date_property.value.timestamp_microseconds_value = long(
time.time() * 1e6)
attended_hr_training_property = employee.property.add()
attended_hr_training_property.name = 'attended_hr_training'
attended_hr_training_property.value.boolean_value = True
resp = self.datastore.commit(req)
Java (Protocol Buffers)
// import static com.google.apphosting.client.datastoreservice.client.DatastoreHelper.*;
Entity.Builder employee = Entity.newBuilder()
.setKey(makeKey("Employee"))
.addProperty(makeProperty("firstName", makeValue("Antonio")))
.addProperty(makeProperty("lastName", makeValue("Salieri")))
.addProperty(makeProperty("hireDate", makeValue(new Date())))
.addProperty(makeProperty("attendedHrTraining", makeValue(true)));
CommitRequest commitRequest = CommitRequest.newBuilder()
.setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
.setMutation(Mutation.newBuilder().addInsertAutoId(employee))
.build();
CommitResponse response = datastore.commit(commitRequest);
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, specify it in the
name
field of the last element of its key's
path
array (
JSON
) or repeated
pathElement
field (
protocol buffers
):
Node.js (JSON)
var entity = {
key: { path: [{ kind: 'Employee', name: 'asalieri' }] }
// ... some properties ...
};
Python (Protocol Buffers)
employee = datastore.Entity()
path_element = employee.key.path_element.add()
path_element.kind = 'Employee'
path_element.name = 'asalieri'
Java (Protocol Buffers)
// import static com.google.apphosting.client.datastoreservice.client.DatastoreHelper.*;
Entity employee = Entity.newBuilder().setKey(makeKey("Employee", "asalieri")).build();
To have the Datastore assign a numeric ID automatically, omit both the
name
and
id
fields and place the entity in the
insertAutoId
(JSON) or
insert_auto_id
(protocol buffers) field of the mutation:
Node.js (JSON)
var entity = {
key: { path: [{ kind: 'Employee' }] }
// ... some properties ...
};
Python (Protocol Buffers)
employee = datastore.Entity()
path_element = employee.key.path_element.add()
path_element.kind = 'Employee'
Java (Protocol Buffers)
// import static com.google.apphosting.client.datastoreservice.client.DatastoreHelper.*;
Key key = makeKey("Employee").build();