@Before public void setUp() { DATASTORE.put(ENTITY1, ENTITY2); }
/** Example of putting a single entity. */ // [TARGET put(FullEntity)] // [VARIABLE "my_key_name"] public void putSingleEntity(String keyName) { // [START putSingleEntity] Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName); Entity.Builder entityBuilder = Entity.newBuilder(key); entityBuilder.set("propertyName", "value"); Entity entity = entityBuilder.build(); datastore.put(entity); // [END putSingleEntity] }
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); } }
/** 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] }
@Test public void testPut() { Entity updatedEntity = Entity.newBuilder(ENTITY1).set("new_property", 42L).build(); assertEquals(updatedEntity, datastore.put(updatedEntity)); assertEquals(updatedEntity, datastore.get(updatedEntity.getKey())); Entity entity2 = Entity.newBuilder(ENTITY2).clear().set("bla", new NullValue()).build(); assertNotEquals(ENTITY2, entity2); List<Entity> entities = datastore.put(ENTITY1, entity2, ENTITY3, PARTIAL_ENTITY1); assertEquals(ENTITY1, entities.get(0)); assertEquals(entity2, entities.get(1)); assertEquals(ENTITY3, entities.get(2)); assertEquals(PARTIAL_ENTITY1.getProperties(), entities.get(3).getProperties()); assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(3).getKey().getAncestors()); assertEquals(ENTITY1, datastore.get(ENTITY1.getKey())); assertEquals(entity2, datastore.get(entity2.getKey())); assertEquals(ENTITY3, datastore.get(ENTITY3.getKey())); Entity entity = datastore.get(entities.get(3).getKey()); assertEquals(entities.get(3), entity); }
@Test public void testPut() { Entity updatedEntity = Entity.newBuilder(ENTITY1).set("new_property", 42L).build(); assertEquals(updatedEntity, DATASTORE.put(updatedEntity)); assertEquals(updatedEntity, DATASTORE.get(updatedEntity.getKey())); Entity entity2 = Entity.newBuilder(ENTITY2).clear().set("bla", new NullValue()).build(); assertNotEquals(ENTITY2, entity2); List<Entity> entities = DATASTORE.put(ENTITY1, entity2, ENTITY3, PARTIAL_ENTITY1); assertEquals(ENTITY1, entities.get(0)); assertEquals(entity2, entities.get(1)); assertEquals(ENTITY3, entities.get(2)); assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(3).getNames()); assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(3).getKey().getAncestors()); assertEquals(ENTITY1, DATASTORE.get(ENTITY1.getKey())); assertEquals(entity2, DATASTORE.get(entity2.getKey())); assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey())); Entity entity = DATASTORE.get(entities.get(3).getKey()); assertEquals(entities.get(3), entity); for (Entity entityToDelete : entities) { DATASTORE.delete(entityToDelete.getKey()); } }
@Test public void testGetArrayNoDeferredResults() { DATASTORE.put(ENTITY3); Iterator<Entity> result = DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
@Test public void testGetArrayNoDeferredResults() { datastore.put(ENTITY3); Iterator<Entity> result = datastore.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
assertFalse(results1.hasNext()); DATASTORE.put(ENTITY3); Query<? extends Entity> query2 = Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND2 + " order by __key__")
@Test public void testStartStopReset() throws IOException, InterruptedException, TimeoutException { LocalDatastoreHelper helper = LocalDatastoreHelper.create(); helper.start(); Datastore datastore = helper.getOptions().getService(); Key key = datastore.newKeyFactory().setKind("kind").newKey("name"); datastore.put(Entity.newBuilder(key).build()); assertNotNull(datastore.get(key)); helper.reset(); assertNull(datastore.get(key)); helper.stop(Duration.ofMinutes(1)); thrown.expect(DatastoreException.class); datastore.get(key); } }
@Test public void testTransactionWithRead() { Transaction transaction = datastore.newTransaction(); assertNull(transaction.get(KEY3)); transaction.add(ENTITY3); transaction.commit(); assertEquals(ENTITY3, datastore.get(KEY3)); transaction = datastore.newTransaction(); assertEquals(ENTITY3, transaction.get(KEY3)); // update entity3 during the transaction datastore.put(Entity.newBuilder(ENTITY3).clear().build()); transaction.update(ENTITY2); try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("ABORTED", expected.getReason()); } }
@Test public void testTransactionWithRead() { Transaction transaction = DATASTORE.newTransaction(); assertNull(transaction.get(KEY3)); transaction.add(ENTITY3); transaction.commit(); assertEquals(ENTITY3, DATASTORE.get(KEY3)); transaction = DATASTORE.newTransaction(); assertEquals(ENTITY3, transaction.get(KEY3)); // update entity3 during the transaction DATASTORE.put(Entity.newBuilder(ENTITY2).clear().set("from", "datastore").build()); transaction.update(Entity.newBuilder(ENTITY2).clear().set("from", "transaction").build()); try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("ABORTED", expected.getReason()); } }
@Test public void testTransactionWithQuery() { Query<Entity> query = Query.newEntityQueryBuilder() .setKind(KIND2) .setFilter(PropertyFilter.hasAncestor(KEY2)) .build(); Transaction transaction = datastore.newTransaction(); QueryResults<Entity> results = transaction.run(query); assertEquals(ENTITY2, results.next()); assertFalse(results.hasNext()); transaction.add(ENTITY3); transaction.commit(); assertEquals(ENTITY3, datastore.get(KEY3)); transaction = datastore.newTransaction(); results = transaction.run(query); assertEquals(ENTITY2, results.next()); transaction.delete(ENTITY3.getKey()); // update entity2 during the transaction datastore.put(Entity.newBuilder(ENTITY2).clear().build()); try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("ABORTED", expected.getReason()); } }
transaction.delete(ENTITY3.getKey()); DATASTORE.put(Entity.newBuilder(ENTITY2).clear().build()); try { transaction.commit();
public static void main(String... args) throws Exception { // Instantiates a client Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // The kind for the new entity String kind = "Task"; // The name/ID for the new entity String name = "sampletask1"; // The Cloud Datastore key for the new entity Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); // Prepares the new entity Entity task = Entity.newBuilder(taskKey) .set("description", "Buy milk") .build(); // Saves the entity datastore.put(task); System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); //Retrieve entity Entity retrieved = datastore.get(taskKey); System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); } }
/** * Adds a task entity to the Datastore. * * @param description The task description * @return The {@link Key} of the entity * @throws DatastoreException if the ID allocation or put fails */ Key addTask(String description) { Key key = datastore.allocateId(keyFactory.newKey()); Entity task = Entity.newBuilder(key) .set("description", StringValue.newBuilder(description).setExcludeFromIndexes(true).build()) .set("created", Timestamp.now()) .set("done", false) .build(); datastore.put(task); return key; } // [END datastore_add_entity]
assertFalse(results1.hasNext()); datastore.put(ENTITY3); Query<? extends Entity> query2 = Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND2 + " order by __key__")
@Test public void test1() throws IOException, InterruptedException { Datastore ds = DatastoreOptions.builder().host("http://localhost:9999").projectId("my-project").build().service(); com.google.cloud.datastore.Key key = ds.newKeyFactory().kind("MyEntity").newKey("mykey"); com.google.cloud.datastore.Entity entity = com.google.cloud.datastore.Entity.builder(key).set("p1", "Hello World!").build(); entity = ds.put(entity); entity = ds.get(key); System.out.println(entity); }
public void processElement(ProcessContext c) { final Datastore datastore = DatastoreOptions.defaultInstance().service(); final KeyFactory keyFactory = datastore.newKeyFactory().kind("Task"); Key key = datastore.allocateId(keyFactory.newKey()); Entity task = Entity.builder(key) .set("description", StringValue.builder(":D").excludeFromIndexes(true).build()) .set("created", DateTime.now()) .set("done", false) .build(); datastore.put(task); }