use of com.google.cloud.datastore.KeyFactory 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.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method fetchEntitiesWithKeys.
/**
* Example of fetching a list of Entity objects.
*/
// [TARGET fetch(Iterable, ReadOption...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
// [START fetchEntitiesWithKeys]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
for (Entity entity : entities) {
// do something with the entity
}
// [END fetchEntitiesWithKeys]
return entities;
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method batchAllocateId.
/**
* Example of allocating multiple ids in a single batch.
*/
// [TARGET allocateId(IncompleteKey...)]
public List<Key> batchAllocateId() {
// [START batchAllocateId]
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
IncompleteKey incompleteKey1 = keyFactory.newKey();
IncompleteKey incompleteKey2 = keyFactory.newKey();
// let cloud datastore automatically assign the ids
List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2);
// [END batchAllocateId]
return keys;
}
use of com.google.cloud.datastore.KeyFactory in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreSnippets method testCreateKeyFactory.
@Test
public void testCreateKeyFactory() {
KeyFactory keyFactory = datastoreSnippets.createKeyFactory();
assertNotNull(keyFactory);
}
use of com.google.cloud.datastore.KeyFactory 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!");
}
}
Aggregations