use of com.google.cloud.datastore.Entity 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.Entity 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.Entity in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method newBatch.
/**
* Example of starting a new batch.
*/
// [TARGET newBatch()]
// [VARIABLE "my_key_name_1"]
// [VARIABLE "my_key_name_2"]
public Batch newBatch(String keyName1, String keyName2) {
// [START newBatch]
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Batch batch = datastore.newBatch();
Entity entity1 = Entity.newBuilder(key1).set("name", "John").build();
Entity entity2 = Entity.newBuilder(key2).set("title", "title").build();
batch.add(entity1);
batch.add(entity2);
batch.submit();
// [END newBatch]
return batch;
}
use of com.google.cloud.datastore.Entity in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method runQueryOnProperty.
/**
* Example of running a query to find all entities with a matching property value.
*/
// [TARGET run(Query, ReadOption...)]
// [VARIABLE "my_kind"]
// [VARIABLE "my_property"]
// [VARIABLE "my_value"]
public List<Entity> runQueryOnProperty(String kind, String property, String value) {
// TODO change so that it's not necessary to hold the entities in a list for integration testing
// [START runQueryOnProperty]
StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setKind(kind).setFilter(PropertyFilter.eq(property, value)).build();
QueryResults<Entity> results = datastore.run(query);
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
// [END runQueryOnProperty]
return entities;
}
use of com.google.cloud.datastore.Entity in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method batchAddEntities.
/**
* Example of adding multiple entities.
*/
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchAddEntities(String keyName1, String keyName2) {
// [START batchAddEntities]
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();
try {
datastore.add(entity1, entity2);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// at least one of entity1.getKey() and entity2.getKey() already exists
}
}
// [END batchAddEntities]
}
Aggregations