use of com.google.cloud.datastore.Key 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.Key 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.Key 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.Key in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method addSingleEntity.
/**
* Example of adding a single entity.
*/
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
// [START addSingleEntity]
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
try {
datastore.add(entity);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// entity.getKey() already exists
}
}
// [END addSingleEntity]
}
use of com.google.cloud.datastore.Key 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;
}
Aggregations