Search in sources :

Example 26 with Key

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

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

Example 28 with Key

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

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

the class DatastoreSnippets method addSingleEntity.

/**
   * Example of adding a single entity.
   */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
    // [START addSingleEntity]
    Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
    Entity.Builder entityBuilder = Entity.newBuilder(key);
    entityBuilder.set("propertyName", "value");
    Entity entity = entityBuilder.build();
    try {
        datastore.add(entity);
    } catch (DatastoreException ex) {
        if ("ALREADY_EXISTS".equals(ex.getReason())) {
        // entity.getKey() already exists
        }
    }
// [END addSingleEntity]
}
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 30 with Key

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

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