use of com.google.cloud.datastore.Key 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.Key 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;
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method putSingleEntity.
/**
* Example of putting a single entity.
*/
// [TARGET put(FullEntity)]
// [VARIABLE "my_key_name"]
public void putSingleEntity(String keyName) {
Datastore datastore = transaction.getDatastore();
// [START putSingleEntity]
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
transaction.put(entity);
transaction.commit();
// [END putSingleEntity]
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method multipleDeleteEntities.
/**
* Example of deleting multiple entities.
*/
// [TARGET delete(Key...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleDeleteEntities(String keyName1, String keyName2) {
Datastore datastore = transaction.getDatastore();
// [START multipleDeleteEntities]
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
transaction.delete(key1, key2);
transaction.commit();
// [END multipleDeleteEntities]
}
use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method multipleAddEntities.
/**
* Example of adding multiple entities.
*/
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleAddEntities(String keyName1, String keyName2) {
Datastore datastore = transaction.getDatastore();
// [START multipleAddEntities]
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
entityBuilder1.set("propertyName", "value1");
Entity entity1 = entityBuilder1.build();
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
entityBuilder2.set("propertyName", "value2");
Entity entity2 = entityBuilder2.build();
transaction.add(entity1, entity2);
transaction.commit();
// [END multipleAddEntities]
}
Aggregations