use of com.google.cloud.datastore.Datastore 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.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method get.
/**
* Example of getting an entity for a given key.
*/
// [TARGET get(Key)]
// [VARIABLE "my_key_name"]
public Entity get(String keyName) {
Datastore datastore = transaction.getDatastore();
// [START get]
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity entity = transaction.get(key);
transaction.commit();
// [END get]
return entity;
}
use of com.google.cloud.datastore.Datastore 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.Datastore 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.Datastore 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]
}
Aggregations