Search in sources :

Example 11 with KeyFactory

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

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

the class DatastoreSnippets method allocateIdSingle.

/**
   * Example of allocating an id.
   */
// [TARGET allocateId(IncompleteKey)]
public Key allocateIdSingle() {
    // [START allocateIdSingle]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    IncompleteKey incompleteKey = keyFactory.newKey();
    // let cloud datastore automatically assign an id
    Key key = datastore.allocateId(incompleteKey);
    // [END allocateIdSingle]
    return key;
}
Also used : KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 13 with KeyFactory

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

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

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

Aggregations

KeyFactory (com.google.cloud.datastore.KeyFactory)18 Key (com.google.cloud.datastore.Key)17 IncompleteKey (com.google.cloud.datastore.IncompleteKey)14 Entity (com.google.cloud.datastore.Entity)12 Datastore (com.google.cloud.datastore.Datastore)11 FullEntity (com.google.cloud.datastore.FullEntity)7 Test (org.junit.Test)3 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Transaction (com.google.cloud.datastore.Transaction)1