Search in sources :

Example 16 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method run.

/**
   * Example of running a query to find all entities with an ancestor.
   */
// [TARGET run(Query)]
// [VARIABLE "my_parent_key_name"]
public List<Entity> run(String parentKeyName) {
    Datastore datastore = transaction.getDatastore();
    // [START run]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind");
    Key parentKey = keyFactory.newKey(parentKeyName);
    // Build a query
    Query<Entity> query = Query.newEntityQueryBuilder().setKind("MyKind").setFilter(PropertyFilter.hasAncestor(parentKey)).build();
    QueryResults<Entity> results = transaction.run(query);
    List<Entity> entities = Lists.newArrayList();
    while (results.hasNext()) {
        Entity result = results.next();
        // do something with result
        entities.add(result);
    }
    transaction.commit();
    // [END run]
    return entities;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 17 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method addSingleEntity.

/**
   * Example of adding a single entity.
   */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
    Datastore datastore = transaction.getDatastore();
    // [START addSingleEntity]
    Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
    Entity.Builder entityBuilder = Entity.newBuilder(key);
    entityBuilder.set("propertyName", "value");
    Entity entity = entityBuilder.build();
    transaction.add(entity);
    transaction.commit();
// [END addSingleEntity]
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 18 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class TransactionSnippets method active.

/**
   * Example of verifying if a transaction is active.
   */
// [TARGET active()]
public Key active() {
    Datastore datastore = transaction.getDatastore();
    // [START active]
    // create an entity
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key key = datastore.allocateId(keyFactory.newKey());
    Entity entity = Entity.newBuilder(key).set("description", "active()").build();
    // calling transaction.active() now would return true
    try {
        // add the entity and commit
        transaction.put(entity);
        transaction.commit();
    } finally {
        // then transaction.isActive() will be false
        if (transaction.isActive()) {
            // otherwise it's true and we need to rollback
            transaction.rollback();
        }
    }
    // [END active]
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 19 with Key

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

Example 20 with Key

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

Aggregations

Key (com.google.cloud.datastore.Key)49 Entity (com.google.cloud.datastore.Entity)37 IncompleteKey (com.google.cloud.datastore.IncompleteKey)32 Datastore (com.google.cloud.datastore.Datastore)19 KeyFactory (com.google.cloud.datastore.KeyFactory)17 FullEntity (com.google.cloud.datastore.FullEntity)16 Test (org.junit.Test)15 Transaction (com.google.cloud.datastore.Transaction)9 DatastoreException (com.google.cloud.datastore.DatastoreException)4 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)3 Batch (com.google.cloud.datastore.Batch)2 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 BeforeClass (org.junit.BeforeClass)1