Search in sources :

Example 36 with Key

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

the class UpdateEntity 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 = datastore.get(key);
    if (entity != null) {
        System.out.println("Updating access_time for " + entity.getString("name"));
        entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build();
        datastore.update(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 Key

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

the class DatastoreSnippets method getEntitiesWithKeys.

/**
   * Example of getting multiple entity objects.
   */
// [TARGET get(Iterable, ReadOption...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> getEntitiesWithKeys(String firstKeyName, String secondKeyName) {
    // TODO change so that it's not necessary to hold the entities in a list for integration testing
    // [START getEntitiesWithKeys]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key firstKey = keyFactory.newKey(firstKeyName);
    Key secondKey = keyFactory.newKey(secondKeyName);
    Iterator<Entity> entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey));
    List<Entity> entities = Lists.newArrayList();
    while (entitiesIterator.hasNext()) {
        Entity entity = entitiesIterator.next();
        // do something with the entity
        entities.add(entity);
    }
    // [END getEntitiesWithKeys]
    return entities;
}
Also used : Entity(com.google.cloud.datastore.Entity) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 38 with Key

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

the class TransactionSnippets method putSingleEntity.

/**
   * Example of putting a single entity.
   */
// [TARGET put(FullEntity)]
// [VARIABLE "my_key_name"]
public void putSingleEntity(String keyName) {
    Datastore datastore = transaction.getDatastore();
    // [START putSingleEntity]
    Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
    Entity.Builder entityBuilder = Entity.newBuilder(key);
    entityBuilder.set("propertyName", "value");
    Entity entity = entityBuilder.build();
    transaction.put(entity);
    transaction.commit();
// [END putSingleEntity]
}
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 39 with Key

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

the class TransactionSnippets method multipleDeleteEntities.

/**
   * Example of deleting multiple entities.
   */
// [TARGET delete(Key...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleDeleteEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multipleDeleteEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    transaction.delete(key1, key2);
    transaction.commit();
// [END multipleDeleteEntities]
}
Also used : Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 40 with Key

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

the class TransactionSnippets method multipleAddEntities.

/**
   * Example of adding multiple entities.
   */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleAddEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multipleAddEntities]
    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();
    transaction.add(entity1, entity2);
    transaction.commit();
// [END multipleAddEntities]
}
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)

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