Search in sources :

Example 6 with KeyFactory

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

the class TransactionSnippets method getMultiple.

/**
   * Example of getting entities for several keys.
   */
// [TARGET get(Key...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> getMultiple(String firstKeyName, String secondKeyName) {
    Datastore datastore = transaction.getDatastore();
    // TODO change so that it's not necessary to hold the entities in a list for integration testing
    // [START getMultiple]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key firstKey = keyFactory.newKey(firstKeyName);
    Key secondKey = keyFactory.newKey(secondKeyName);
    Iterator<Entity> entitiesIterator = transaction.get(firstKey, secondKey);
    List<Entity> entities = Lists.newArrayList();
    while (entitiesIterator.hasNext()) {
        Entity entity = entitiesIterator.next();
        // do something with the entity
        entities.add(entity);
    }
    transaction.commit();
    // [END getMultiple]
    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 7 with KeyFactory

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

the class DatastoreSnippets method fetchEntitiesWithKeys.

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

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

the class DatastoreSnippets method batchAllocateId.

/**
   * Example of allocating multiple ids in a single batch.
   */
// [TARGET allocateId(IncompleteKey...)]
public List<Key> batchAllocateId() {
    // [START batchAllocateId]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    IncompleteKey incompleteKey1 = keyFactory.newKey();
    IncompleteKey incompleteKey2 = keyFactory.newKey();
    // let cloud datastore automatically assign the ids
    List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2);
    // [END batchAllocateId]
    return keys;
}
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 9 with KeyFactory

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

the class ITDatastoreSnippets method testCreateKeyFactory.

@Test
public void testCreateKeyFactory() {
    KeyFactory keyFactory = datastoreSnippets.createKeyFactory();
    assertNotNull(keyFactory);
}
Also used : KeyFactory(com.google.cloud.datastore.KeyFactory) Test(org.junit.Test)

Example 10 with KeyFactory

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

the class AddEntitiesAndRunQuery method main.

public static void main(String... args) {
    // Create datastore service object.
    // By default, credentials are inferred from the runtime environment.
    Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
    // Add an entity to Datastore
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person");
    Key key = keyFactory.newKey("john.doe@gmail.com");
    Entity entity = Entity.newBuilder(key).set("name", "John Doe").set("age", 51).set("favorite_food", "pizza").build();
    datastore.put(entity);
    // Get an entity from Datastore
    Entity johnEntity = datastore.get(key);
    // Add a couple more entities to make the query results more interesting
    Key janeKey = keyFactory.newKey("jane.doe@gmail.com");
    Entity janeEntity = Entity.newBuilder(janeKey).set("name", "Jane Doe").set("age", 44).set("favorite_food", "pizza").build();
    Key joeKey = keyFactory.newKey("joe.shmoe@gmail.com");
    Entity joeEntity = Entity.newBuilder(joeKey).set("name", "Joe Shmoe").set("age", 27).set("favorite_food", "sushi").build();
    datastore.put(janeEntity, joeEntity);
    // Run a query
    Query<Entity> query = Query.newEntityQueryBuilder().setKind("Person").setFilter(PropertyFilter.eq("favorite_food", "pizza")).build();
    QueryResults<Entity> results = datastore.run(query);
    while (results.hasNext()) {
        Entity currentEntity = results.next();
        System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!");
    }
}
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)

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