Search in sources :

Example 41 with Entity

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

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

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

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

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

Entity (com.google.cloud.datastore.Entity)59 Key (com.google.cloud.datastore.Key)37 IncompleteKey (com.google.cloud.datastore.IncompleteKey)27 Test (org.junit.Test)25 FullEntity (com.google.cloud.datastore.FullEntity)24 Datastore (com.google.cloud.datastore.Datastore)16 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)15 Transaction (com.google.cloud.datastore.Transaction)15 KeyFactory (com.google.cloud.datastore.KeyFactory)12 DatastoreException (com.google.cloud.datastore.DatastoreException)11 Batch (com.google.cloud.datastore.Batch)2 GqlQuery (com.google.cloud.datastore.GqlQuery)2 NullValue (com.google.cloud.datastore.NullValue)2 HashSet (java.util.HashSet)2 BooleanValue (com.google.cloud.datastore.BooleanValue)1 LatLngValue (com.google.cloud.datastore.LatLngValue)1 ListValue (com.google.cloud.datastore.ListValue)1 StringValue (com.google.cloud.datastore.StringValue)1 TimestampValue (com.google.cloud.datastore.TimestampValue)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1