use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class CreateEntity method main.
public static void main(String... args) {
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = Entity.newBuilder(key).set("name", "John Doe").set("age", 30).set("access_time", Timestamp.now()).build();
datastore.put(entity);
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method allocateIdSingle.
/**
* Example of allocating an id.
*/
// [TARGET allocateId(IncompleteKey)]
public Key allocateIdSingle() {
// [START allocateIdSingle]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
IncompleteKey incompleteKey = keyFactory.newKey();
// let cloud datastore automatically assign an id
Key key = datastore.allocateId(incompleteKey);
// [END allocateIdSingle]
return key;
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method fetchEntitiesWithKeys.
/**
* Example of fetching a list of entities for several keys.
*/
// [TARGET fetch(Key...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
Datastore datastore = transaction.getDatastore();
// [START fetchEntitiesWithKeys]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
List<Entity> entities = transaction.fetch(firstKey, secondKey);
for (Entity entity : entities) {
// do something with the entity
}
transaction.commit();
// [END fetchEntitiesWithKeys]
return entities;
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class UpdateEntity method main.
public static void main(String... args) {
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build();
datastore.update(entity);
}
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method getEntitiesWithKeys.
/**
* Example of getting multiple entity objects.
*/
// [TARGET get(Iterable, ReadOption...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> getEntitiesWithKeys(String firstKeyName, String secondKeyName) {
// TODO change so that it's not necessary to hold the entities in a list for integration testing
// [START getEntitiesWithKeys]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
Iterator<Entity> entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey));
List<Entity> entities = Lists.newArrayList();
while (entitiesIterator.hasNext()) {
Entity entity = entitiesIterator.next();
// do something with the entity
entities.add(entity);
}
// [END getEntitiesWithKeys]
return entities;
}
Aggregations