Search in sources :

Example 31 with Key

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

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

Example 33 with Key

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

the class DatastoreSnippets method batchPutEntities.

/**
   * Example of putting multiple entities.
   */
// [TARGET put(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchPutEntities(String keyName1, String keyName2) {
    // [START batchPutEntities]
    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();
    datastore.put(entity1, entity2);
// [END batchPutEntities]
}
Also used : Entity(com.google.cloud.datastore.Entity) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 34 with Key

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

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

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