Search in sources :

Example 36 with Entity

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);
}
Also used : Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key)

Example 37 with 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]
}
Also used : Entity(com.google.cloud.datastore.Entity) DatastoreException(com.google.cloud.datastore.DatastoreException) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 38 with Entity

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;
}
Also used : Entity(com.google.cloud.datastore.Entity) Batch(com.google.cloud.datastore.Batch) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 39 with Entity

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;
}
Also used : Entity(com.google.cloud.datastore.Entity)

Example 40 with Entity

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]
}
Also used : Entity(com.google.cloud.datastore.Entity) DatastoreException(com.google.cloud.datastore.DatastoreException) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Aggregations

Entity (com.google.cloud.datastore.Entity)59 Key (com.google.cloud.datastore.Key)37 IncompleteKey (com.google.cloud.datastore.IncompleteKey)27 Test (org.junit.Test)25 FullEntity (com.google.cloud.datastore.FullEntity)24 Datastore (com.google.cloud.datastore.Datastore)16 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)15 Transaction (com.google.cloud.datastore.Transaction)15 KeyFactory (com.google.cloud.datastore.KeyFactory)12 DatastoreException (com.google.cloud.datastore.DatastoreException)11 Batch (com.google.cloud.datastore.Batch)2 GqlQuery (com.google.cloud.datastore.GqlQuery)2 NullValue (com.google.cloud.datastore.NullValue)2 HashSet (java.util.HashSet)2 BooleanValue (com.google.cloud.datastore.BooleanValue)1 LatLngValue (com.google.cloud.datastore.LatLngValue)1 ListValue (com.google.cloud.datastore.ListValue)1 StringValue (com.google.cloud.datastore.StringValue)1 TimestampValue (com.google.cloud.datastore.TimestampValue)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1