Search in sources :

Example 11 with Datastore

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

the class TransactionSnippets method fetchEntitiesWithKeys.

/**
   * Example of fetching a list of entities for several keys.
   */
// [TARGET fetch(Key...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
    Datastore datastore = transaction.getDatastore();
    // [START fetchEntitiesWithKeys]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key firstKey = keyFactory.newKey(firstKeyName);
    Key secondKey = keyFactory.newKey(secondKeyName);
    List<Entity> entities = transaction.fetch(firstKey, secondKey);
    for (Entity entity : entities) {
    // do something with the entity
    }
    transaction.commit();
    // [END fetchEntitiesWithKeys]
    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 12 with Datastore

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

the class TransactionSnippets method get.

/**
   * Example of getting an entity for a given key.
   */
// [TARGET get(Key)]
// [VARIABLE "my_key_name"]
public Entity get(String keyName) {
    Datastore datastore = transaction.getDatastore();
    // [START get]
    Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
    Entity entity = transaction.get(key);
    transaction.commit();
    // [END get]
    return entity;
}
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 13 with Datastore

use of com.google.cloud.datastore.Datastore 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 14 with Datastore

use of com.google.cloud.datastore.Datastore 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 15 with Datastore

use of com.google.cloud.datastore.Datastore 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)

Aggregations

Datastore (com.google.cloud.datastore.Datastore)21 Key (com.google.cloud.datastore.Key)19 IncompleteKey (com.google.cloud.datastore.IncompleteKey)17 Entity (com.google.cloud.datastore.Entity)16 FullEntity (com.google.cloud.datastore.FullEntity)15 KeyFactory (com.google.cloud.datastore.KeyFactory)11 Response (com.google.cloud.datastore.Transaction.Response)2 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Transaction (com.google.cloud.datastore.Transaction)1 Test (org.junit.Test)1