public static void main(String[] args) throws IOException { String serverString = args[0]; RemoteApiOptions options; if (serverString.equals("localhost")) { options = new RemoteApiOptions().server(serverString, 8080).useDevelopmentServerCredential(); } else { options = new RemoteApiOptions().server(serverString, 443).useApplicationDefaultCredential(); } RemoteApiInstaller installer = new RemoteApiInstaller(); installer.install(options); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!"))); } finally { installer.uninstall(); } } }
/** * A simple API client. * @param args . * @throws IOException . */ public static void main(String[] args) throws IOException { String serverString = args[0]; RemoteApiOptions options; if (serverString.equals("localhost")) { options = new RemoteApiOptions().server(serverString, 8080).useDevelopmentServerCredential(); } else { options = new RemoteApiOptions().server(serverString, 443).useApplicationDefaultCredential(); } RemoteApiInstaller installer = new RemoteApiInstaller(); installer.install(options); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!"))); } finally { installer.uninstall(); } } }
/** * Puts the specified App Engine Datastore entities into the datastore * within the specified {@code Transaction}. * * @param transaction The transaction to put the entities. * @param entities The entities to be put. * @return The keys allocated to the stored entities. */ public List<Key> put(Transaction transaction, Iterable<Entity> entities) { return datastore.put(transaction, entities); }
/** * Puts the specified App Engine Datastore entity into the datastore within * the specified {@code Transaction}. * * @param transaction The transaction to put the entity. * @param entity The entity to be put. * @return The {@code Key} allocated to the stored entity. */ public Key put(Transaction transaction, Entity entity) { return datastore.put(transaction, entity); }
@Api(name = "user") public class UserApi { @ApiMethod(name = "users.insert") public void insert(MyUser user) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); e = user.getEntity(); datastore.put(e); } }
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); for (Integer n = 0 ; n < 7 ; ++n ) { Entity entity = new Entity("E15", n.toString()); datastore.put(entity); }
DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity e = new Entity("data"); e.setProperty("jsonString", "myValue"); Key key = ds.put(e); Entity entity = ds.get(key); System .out.println(entity);
public Key put(Transaction transaction, Entity entity) { try { return inner.put(transaction, entity); } catch (IllegalArgumentException e) { throw wrapIllegalArgumentException(e); } catch (ConcurrentModificationException e) { throw wrapConcurrentModificationException(e); } catch (DatastoreFailureException e) { throw wrapDatastoreFailureException(e); } }
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query q = new Query("Entity"); PreparedQuery pq = datastore.prepare(q); for (com.google.appengine.api.datastore.Entity result : pq.asIterable()) { String content = (String) result.getProperty("content"); if (content != null) { result.setProperty("content", new com.google.appengine.api.datastore.Text(content)); datastore.put(result); } }
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query q = new Query("Users").addFilter("value", Query.FilterOperator.EQUAL, 1); PreparedQuery pq = datastore.prepare(q); if(pq!=null) { for (com.google.appengine.api.datastore.Entity result : pq.asIterable()) { nodeID = result.getProperty("nodeId").toString(); result.setProperty("value",0); datastore.put(result); break; } }
String name = "Paul"; int age =19; DatastoreService service= DatastoreServiceFactory.getDatastoreService(); Entity entity = new Entity("Person",1); entity.setProperty("name",name); entity.setProperty("alter",age); service.put(entity); service.delete()
@Test public void putStoresEntity() throws Exception { Entity entity = createTestEntity(); service.put(entity); assertStoreContains(entity); }
@Test public void putStoresAllGivenEntities() throws Exception { Collection<Entity> entities = createTestEntities(); for (Entity e : entities) { service.put(e); } assertStoreContainsAll(entities); }
@Test public void batchGetReturnsOnlyExistingKeysInMap() throws Exception { Key existingKey = KeyFactory.createKey("batch", "existing"); Key nonExistingKey = KeyFactory.createKey("batch", "nonExisting"); service.put(new Entity(existingKey)); Map<Key, Entity> map = service.get(Arrays.asList(existingKey, nonExistingKey)); assertEquals(1, map.size()); assertTrue(map.containsKey(existingKey)); }
@Test public void testNoIdKey() throws Exception { Entity entity = new Entity("NO_ID"); Key key = service.put(entity); assertTrue(key.isComplete()); }
@Test public void testEntityGainsNoAdditionalPropertiesWhenStored() throws Exception { clearData(kindName); Entity entity = new Entity(kindName); Key key = service.put(entity); entity = service.get(key); assertEquals(Collections.<String, Object>emptyMap(), entity.getProperties()); } }
@Test public void deleteRemovesEntityFromStore() throws Exception { Entity entity = createTestEntity(); Key key = entity.getKey(); service.put(entity); service.delete(key); assertStoreDoesNotContain(key); }
@Test public void testRollbackWhenPuttingEntity() throws Exception { Entity entity = createTestEntity("ROLLBACK", 1); Transaction tx = service.beginTransaction(); service.put(tx, entity); tx.rollback(); // should not be there due to rollback assertStoreDoesNotContain(entity); }
@Test public void queriesDontReturnDeletedEntities() throws Exception { String methodName = "queriesDontReturnDeletedEntities"; Entity entity = createTestEntityWithUniqueMethodNameKey(SMOKE_TEST_ENTITY, methodName); Key key = entity.getKey(); service.put(entity); service.delete(key); List<Entity> entities = service.prepare(new Query(SMOKE_TEST_ENTITY).setAncestor(key)) .asList(FetchOptions.Builder.withDefaults()); assertEquals(0, entities.size()); }