use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method multipleUpdateEntities.
/**
* Example of updating multiple entities.
*/
// [TARGET update(Entity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleUpdateEntities(String keyName1, String keyName2) {
Datastore datastore = transaction.getDatastore();
// [START multipleUpdateEntities]
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
entityBuilder1.set("propertyName", "value3");
Entity entity1 = entityBuilder1.build();
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
entityBuilder2.set("propertyName", "value4");
Entity entity2 = entityBuilder2.build();
transaction.update(entity1, entity2);
transaction.commit();
// [END multipleUpdateEntities]
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method getMultiple.
/**
* Example of getting entities for several keys.
*/
// [TARGET get(Key...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> getMultiple(String firstKeyName, String secondKeyName) {
Datastore datastore = transaction.getDatastore();
// TODO change so that it's not necessary to hold the entities in a list for integration testing
// [START getMultiple]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
Iterator<Entity> entitiesIterator = transaction.get(firstKey, secondKey);
List<Entity> entities = Lists.newArrayList();
while (entitiesIterator.hasNext()) {
Entity entity = entitiesIterator.next();
// do something with the entity
entities.add(entity);
}
transaction.commit();
// [END getMultiple]
return entities;
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class TransactionSnippets method multipleAddEntitiesDeferredId.
/**
* Example of adding multiple entities with deferred id allocation.
*/
// [TARGET addWithDeferredIdAllocation(FullEntity...)]
public List<Key> multipleAddEntitiesDeferredId() {
Datastore datastore = transaction.getDatastore();
// [START multipleAddEntitiesDeferredId]
IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey();
FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1);
entityBuilder1.set("propertyName", "value1");
FullEntity entity1 = entityBuilder1.build();
IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey();
FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2);
entityBuilder2.set("propertyName", "value2");
FullEntity entity2 = entityBuilder2.build();
transaction.addWithDeferredIdAllocation(entity1, entity2);
Response response = transaction.commit();
// [END multipleAddEntitiesDeferredId]
return response.getGeneratedKeys();
}
use of com.google.cloud.datastore.Datastore in project google-cloud-java by GoogleCloudPlatform.
the class AddEntitiesAndRunQuery method main.
public static void main(String... args) {
// Create datastore service object.
// By default, credentials are inferred from the runtime environment.
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
// Add an entity to Datastore
KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person");
Key key = keyFactory.newKey("john.doe@gmail.com");
Entity entity = Entity.newBuilder(key).set("name", "John Doe").set("age", 51).set("favorite_food", "pizza").build();
datastore.put(entity);
// Get an entity from Datastore
Entity johnEntity = datastore.get(key);
// Add a couple more entities to make the query results more interesting
Key janeKey = keyFactory.newKey("jane.doe@gmail.com");
Entity janeEntity = Entity.newBuilder(janeKey).set("name", "Jane Doe").set("age", 44).set("favorite_food", "pizza").build();
Key joeKey = keyFactory.newKey("joe.shmoe@gmail.com");
Entity joeEntity = Entity.newBuilder(joeKey).set("name", "Joe Shmoe").set("age", 27).set("favorite_food", "sushi").build();
datastore.put(janeEntity, joeEntity);
// Run a query
Query<Entity> query = Query.newEntityQueryBuilder().setKind("Person").setFilter(PropertyFilter.eq("favorite_food", "pizza")).build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
Entity currentEntity = results.next();
System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!");
}
}
use of com.google.cloud.datastore.Datastore 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);
}
Aggregations