com.google.appengine.api.datastore.DatastoreService Java Examples
The following examples show how to use
com.google.appengine.api.datastore.DatastoreService.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestMetadataServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
List<String> getNamespaces(DatastoreService ds, String startNamespace, String endNamespace) { Query q = new Query(Query.NAMESPACE_METADATA_KIND); if (startNamespace != null) { q.addFilter( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN_OR_EQUAL, makeNamespaceKey(startNamespace)); } if (endNamespace != null) { q.addFilter( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.LESS_THAN_OR_EQUAL, makeNamespaceKey(endNamespace)); } ArrayList<String> results = new ArrayList<String>(); for (Entity e : ds.prepare(q).asIterable()) { if (e.getKey().getId() != 0) { results.add(""); } else { results.add(e.getKey().getName()); } } return results; }
Example #2
Source File: TestReport.java From appengine-tck with Apache License 2.0 | 6 votes |
/** * Finds the {@code TestReport} with the given build type id ordered by build id in the descendant order. * * @param buildTypeId the build type id. * @param limit the optional fetch limit, by default {@link com.google.appengine.tck.site.endpoints.TestReport#DEFAULT_FETCH_LIMIT}. * @param reports the reports entry point * @return the matching test reports list or an empty one if none. */ @SuppressWarnings("unchecked") public static List<TestReport> findByBuildTypeIdOrderByBuildIdDesc(String buildTypeId, Optional<Integer> limit, Reports reports) { final MemcacheService memcacheService = reports.getMemcacheService(); List<TestReport> results = (List<TestReport>) memcacheService.get(buildTypeId); if (results == null) { final Filter buildTypeFilter = new Query.FilterPredicate("buildTypeId", FilterOperator.EQUAL, buildTypeId); final Query query = new Query(TEST_REPORT).setFilter(buildTypeFilter).addSort("buildId", DESCENDING); final DatastoreService datastoreService = reports.getDatastoreService(); final PreparedQuery preparedQuery = datastoreService.prepare(query); final List<Entity> entities = preparedQuery.asList(FetchOptions.Builder.withLimit(limit.or(DEFAULT_FETCH_LIMIT))); results = new ArrayList<>(); for (Entity oneEntity : entities) { final TestReport report = from(oneEntity); results.add(report); } memcacheService.put(buildTypeId, results); } return results; }
Example #3
Source File: LocalDatastoreSmoketestServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity e = new Entity("foo"); e.setProperty("foo", 23); Key key = ds.put(e); try { e = ds.get(key); } catch (EntityNotFoundException e1) { throw new ServletException(e1); } e.setProperty("bar", 44); ds.put(e); Query q = new Query("foo"); q.addFilter("foo", Query.FilterOperator.GREATER_THAN_OR_EQUAL, 22); Iterator<Entity> iter = ds.prepare(q).asIterator(); iter.next(); }
Example #4
Source File: TestMetadataServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
private void populate(DatastoreService ds, String namespace) { NamespaceManager.set(namespace); Entity e = new Entity("Fun"); e.setProperty("me", "yes"); e.setProperty("you", 23); e.setUnindexedProperty("haha", 0); ds.put(e); Entity s = new Entity("Strange"); ArrayList nowhereList = new ArrayList<Integer>(); nowhereList.add(1); nowhereList.add(2); nowhereList.add(3); s.setProperty("nowhere", nowhereList); ds.put(s); Entity s2 = new Entity("Stranger"); s2.setProperty("missing", new ArrayList<Integer>()); ds.put(s2); }
Example #5
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
/** * Runs a series of tests using keys with both local app ids and remote app ids. */ private void doTest(LocalKeysHolder localKeysHolder, LocalEntitiesHolder localEntitiesHolder) { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); List<RemoteApiUnitTest> tests = Lists.newLinkedList( new PutAndGetTester(), new PutAndGetInTransactionTester(), new QueryTester(), new DeleteTester(), new XgTransactionTester()); // Run each test once with local keys and once with remote keys. for (RemoteApiUnitTest test : tests) { if (localKeysHolder != null) { test.run( ds, localKeysHolder.createSupplierForFreshKind(), localEntitiesHolder.createSupplierForFreshKind()); logger.info("Test passed with local keys: " + test.getClass().getName()); } test.run(ds, new RemoteKeySupplier(), new RemoteEntitySupplier()); logger.info("Test passed with remote keys: " + test.getClass().getName()); } }
Example #6
Source File: DemoEntityManagerNoSql.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 6 votes |
@Override public T deleteEntity(T demoEntity) { Utils.assertTrue(demoEntity != null, "entity cannot be null"); DemoEntityNoSql entityNoSql = downCastEntity(demoEntity); DatastoreService ds = getDatastoreService(); Transaction txn = ds.beginTransaction(); try { if (checkEntityForDelete(ds, entityNoSql)) { ds.delete(entityNoSql.getEntity().getKey()); txn.commit(); logger.info("entity deleted."); return demoEntity; } } catch (Exception e) { logger.severe("Failed to delete entity from datastore:" + e.getMessage()); } finally { if (txn.isActive()) { txn.rollback(); } } return null; }
Example #7
Source File: DatastoreSessionStore.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public Map<String, SessionData> getAllSessions() { final String originalNamespace = NamespaceManager.get(); NamespaceManager.set(""); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); PreparedQuery pq = ds.prepare(new Query(SESSION_ENTITY_TYPE)); Map<String, SessionData> sessions = new HashMap<>(); for (Entity entity : pq.asIterable()) { sessions.put(entity.getKey().getName(), createSessionFromEntity(entity)); } return sessions; } finally { NamespaceManager.set(originalNamespace); } }
Example #8
Source File: IndexesServletTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@SuppressWarnings("VariableDeclarationUsageDistance") @Test public void doGet_repeatedPropertyEntities_writesWidgets() throws Exception { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); // [START exploding_index_example_3] Entity widget = new Entity("Widget"); widget.setProperty("x", Arrays.asList(1, 2, 3, 4)); widget.setProperty("y", Arrays.asList("red", "green", "blue")); widget.setProperty("date", new Date()); datastore.put(widget); // [END exploding_index_example_3] servletUnderTest.doGet(mockRequest, mockResponse); assertWithMessage("IndexesServlet response") .that(responseWriter.toString()) .isEqualTo("Got 1 widgets.\n"); }
Example #9
Source File: MyEndpoint.java From endpoints-codelab-android with GNU General Public License v3.0 | 6 votes |
@ApiMethod(name = "clearTasks") public void clearTasks() { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Transaction txn = datastoreService.beginTransaction(); try { Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Query query = new Query(taskBeanParentKey); List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults()); for (Entity result : results) { datastoreService.delete(result.getKey()); } txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example #10
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 6 votes |
public static <T extends TempData> T getLastTempData(Class<T> type) { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); String kind = getKind(type); PreparedQuery pq = ds.prepare(new Query(kind).addSort("timestamp", Query.SortDirection.DESCENDING)); Iterator<Entity> iter = pq.asIterator(); if (iter.hasNext()) { Entity entity = iter.next(); return readTempData(type, entity, ds); } else { return null; } } catch (Exception e) { throw new IllegalStateException(e); } }
Example #11
Source File: MyEndpoint.java From endpoints-codelab-android with GNU General Public License v3.0 | 6 votes |
@ApiMethod(name = "storeTask") public void storeTask(TaskBean taskBean) { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Transaction txn = datastoreService.beginTransaction(); try { Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Entity taskEntity = new Entity("TaskBean", taskBean.getId(), taskBeanParentKey); taskEntity.setProperty("data", taskBean.getData()); datastoreService.put(taskEntity); txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example #12
Source File: TransactionAbandoningServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { mostRecentTxnId = null; // clear out any old values DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); mostRecentTxnId = datastore.beginTransaction().getId(); if (datastore.getActiveTransactions().size() != 1) { throw new RuntimeException("Active transactions did not equal 1"); } if (httpServletRequest.getParameter("throwExceptionBeforeReturn") != null) { throw new RuntimeException("boom"); } httpServletResponse.setContentType("text/plain"); httpServletResponse.getWriter().print(mostRecentTxnId); }
Example #13
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { // Put a fresh entity. Entity originalEntity = new Entity(getFreshKindName()); originalEntity.setProperty("prop1", 75L); ds.put(originalEntity); Key key = originalEntity.getKey(); // Prepare a new version of it with a different property value. Entity mutatedEntity = new Entity(key); mutatedEntity.setProperty("prop1", 76L); // Test Get/Put within a transaction. Transaction txn = ds.beginTransaction(); assertGetEquals(ds, key, originalEntity); ds.put(mutatedEntity); // Write the mutated Entity. assertGetEquals(ds, key, originalEntity); // Within a txn, the put is not yet reflected. txn.commit(); // Now that the txn is committed, the mutated entity will show up in Get. assertGetEquals(ds, key, mutatedEntity); }
Example #14
Source File: RemoteApiExample.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * 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(); } }
Example #15
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { // Note that we can't use local keys here. Query will fail if you set an ancestor whose app // id does not match the "global" AppIdNamespace. // TODO(xx): Consider making it more lenient, but it's not a big deal. Users can // just use a Key that was created after installing the Remote API. Entity entity = new Entity(getFreshKindName()); entity.setProperty("prop1", 99L); ds.put(entity); // Make sure we can retrieve it via a query. Query query = new Query(entity.getKind()); query.setAncestor(entity.getKey()); query.setFilter( new Query.FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN_OR_EQUAL, entity.getKey())); Entity queryResult = ds.prepare(query).asSingleEntity(); // Queries return the Entities with the remote app id. assertRemoteAppId(queryResult.getKey()); assertEquals(99L, queryResult.getProperty("prop1")); }
Example #16
Source File: TransactionsTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void creatingAnEntityInASpecificEntityGroup() throws Exception { String boardName = "my-message-board"; // [START creating_an_entity_in_a_specific_entity_group] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); String messageTitle = "Some Title"; String messageText = "Some message."; Date postDate = new Date(); Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName); Entity message = new Entity("Message", messageBoardKey); message.setProperty("message_title", messageTitle); message.setProperty("message_text", messageText); message.setProperty("post_date", postDate); Transaction txn = datastore.beginTransaction(); datastore.put(txn, message); txn.commit(); // [END creating_an_entity_in_a_specific_entity_group] }
Example #17
Source File: AllocateIdsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testCheckKeyRange() throws Exception { long initialValue = getInitialValue("OtherKind"); KeyRange kr1 = new KeyRange(null, "OtherKind", 1 + initialValue, 5 + initialValue); DatastoreService.KeyRangeState state1 = service.allocateIdRange(kr1); Assert.assertNotNull(state1); // imo, it could be either -- depending on the impl Assert.assertTrue(DatastoreService.KeyRangeState.CONTENTION == state1 || DatastoreService.KeyRangeState.EMPTY == state1); KeyRange kr2 = service.allocateIds("OtherKind", 6); Assert.assertNotNull(kr2); KeyRange kr3 = new KeyRange(null, "OtherKind", 2 + initialValue, 5 + initialValue); DatastoreService.KeyRangeState state2 = service.allocateIdRange(kr3); Assert.assertNotNull(state2); // can it be both, depending on the impl? Assert.assertTrue(DatastoreService.KeyRangeState.COLLISION == state2 || DatastoreService.KeyRangeState.CONTENTION == state2); }
Example #18
Source File: MetadataEntityGroupTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static void printEntityGroupVersions(DatastoreService ds, PrintWriter writer) { Entity entity1 = new Entity("Simple"); Key key1 = ds.put(entity1); Key entityGroupKey = Entities.createEntityGroupKey(key1); // Print entity1's entity group version writer.println("version " + getEntityGroupVersion(ds, null, key1)); // Write to a different entity group Entity entity2 = new Entity("Simple"); ds.put(entity2); // Will print the same version, as entity1's entity group has not changed writer.println("version " + getEntityGroupVersion(ds, null, key1)); // Change entity1's entity group by adding a new child entity Entity entity3 = new Entity("Simple", entity1.getKey()); ds.put(entity3); // Will print a higher version, as entity1's entity group has changed writer.println("version " + getEntityGroupVersion(ds, null, key1)); }
Example #19
Source File: EntitiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void kindExample_writesEntity() throws Exception { // [START kind_example] Entity employee = new Entity("Employee", "asalieri"); employee.setProperty("firstName", "Antonio"); employee.setProperty("lastName", "Salieri"); employee.setProperty("hireDate", new Date()); employee.setProperty("attendedHrTraining", true); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(employee); // [END kind_example] Entity got = datastore.get(employee.getKey()); assertWithMessage("got.firstName") .that((String) got.getProperty("firstName")) .isEqualTo("Antonio"); assertWithMessage("got.lastName") .that((String) got.getProperty("lastName")) .isEqualTo("Salieri"); assertWithMessage("got.hireDate").that((Date) got.getProperty("hireDate")).isNotNull(); assertWithMessage("got.attendedHrTraining") .that((boolean) got.getProperty("attendedHrTraining")) .isTrue(); }
Example #20
Source File: Datastore.java From webauthndemo with Apache License 2.0 | 5 votes |
public static DatastoreService getDatastore() { if (datastore == null) { synchronized (Datastore.class) { if (datastore == null) { datastore = DatastoreServiceFactory.getDatastoreService(); } } } return datastore; }
Example #21
Source File: TestMetadataServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
public void testMetadata(DatastoreService ds, HttpServletResponse response) throws Exception { List<String> propsOfFun = propertiesOfKind(ds, "Fun"); assertEquals("", 2, propsOfFun.size(), response); assertEquals("", "me", propsOfFun.get(0), response); assertEquals("", "you", propsOfFun.get(1), response); Object[] repsOfFunMe = representationsOf(ds, "Fun", "me").toArray(); assertEquals("", 1, repsOfFunMe.length, response); assertEquals("", "STRING", repsOfFunMe[0], response); List<String> namespaces = getNamespaces(ds, null, null); assertEquals("", 3, namespaces.size(), response); assertEquals("", "", namespaces.get(0), response); assertEquals("", "ns1", namespaces.get(1), response); assertEquals("", "ns2", namespaces.get(2), response); Query q = new Query(Query.KIND_METADATA_KIND); q.addFilter( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.LESS_THAN_OR_EQUAL, makeKindKey("M")); assertEquals( "", "Fun", ds .prepare(q) .asSingleEntity() .getKey() .getName(), response); }
Example #22
Source File: ConcurrentTxServlet.java From appengine-tck with Apache License 2.0 | 5 votes |
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String entityGroup = req.getParameter("eg"); String counter = req.getParameter("c"); String parent = req.getParameter("p"); boolean xg = Boolean.parseBoolean(req.getParameter("xg")); Key parentKey = "2".equals(parent) ? ROOT_2.getKey() : ROOT_1.getKey(); Entity entity = new Entity(entityGroup, parentKey); entity.setProperty("foo", RANDOM.nextInt()); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); final Transaction tx = ds.beginTransaction(TransactionOptions.Builder.withXG(xg)); try { log.warning("Before put ... " + counter); putEntity(ds, entity); log.warning("After put ... " + counter); tx.commit(); resp.getWriter().write("OK" + counter); } catch (Exception e) { log.warning("Error ... " + e); tx.rollback(); resp.getWriter().write("ERROR" + counter + ":" + e.getClass().getName()); error(counter); } finally { cleanup(counter); } }
Example #23
Source File: FetchMessagesServlet.java From cloud-pubsub-samples-java with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public final void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { // First retrieve messages from the memcache MemcacheService memcacheService = MemcacheServiceFactory .getMemcacheService(); List<String> messages = (List<String>) memcacheService.get(Constants.MESSAGE_CACHE_KEY); if (messages == null) { // If no messages in the memcache, look for the datastore DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); PreparedQuery query = datastore.prepare( new Query("PubsubMessage").addSort("receipt-time", Query.SortDirection.DESCENDING)); messages = new ArrayList<>(); for (Entity entity : query.asIterable( FetchOptions.Builder.withLimit(MAX_COUNT))) { String message = (String) entity.getProperty("message"); messages.add(message); } // Store them to the memcache for future use. memcacheService.put(Constants.MESSAGE_CACHE_KEY, messages); } ObjectMapper mapper = new ObjectMapper(); resp.setContentType("application/json; charset=UTF-8"); mapper.writeValue(resp.getWriter(), messages); resp.getWriter().close(); }
Example #24
Source File: DemoEntityManagerNoSql.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
/** * Looks up an entity by key. * * @param ds the datastore service objct. * @param key the entity key. * @return the entity; null if the key could not be found. */ protected Entity getDatastoreEntity(DatastoreService ds, Key key) { try { return ds.get(key); } catch (EntityNotFoundException e) { logger.fine("No entity found:" + key.toString()); } return null; }
Example #25
Source File: AppEngineCredentialStore.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
@Override public boolean load(String userId, Credential credential) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key key = KeyFactory.createKey(KIND, userId); try { Entity entity = datastore.get(key); credential.setAccessToken((String) entity.getProperty("accessToken")); credential.setRefreshToken((String) entity.getProperty("refreshToken")); credential.setExpirationTimeMilliseconds((Long) entity.getProperty("expirationTimeMillis")); return true; } catch (EntityNotFoundException exception) { return false; } }
Example #26
Source File: AppEngineCredentialStore.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
@Override public void store(String userId, Credential credential) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Entity entity = new Entity(KIND, userId); entity.setProperty("accessToken", credential.getAccessToken()); entity.setProperty("refreshToken", credential.getRefreshToken()); entity.setProperty("expirationTimeMillis", credential.getExpirationTimeMilliseconds()); datastore.put(entity); }
Example #27
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
public static void deleteTempData(Class<? extends TempData> type) { // check if in-container if (isInContainer() == false) { return; } String kind = getKind(type); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); final List<Entity> list = ds.prepare(new Query(kind)).asList(FetchOptions.Builder.withDefaults()); for (Entity e : list) { deleteTempDataInTx(ds, e, type); } }
Example #28
Source File: TestUtils.java From appengine-tck with Apache License 2.0 | 5 votes |
public static void clean() { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); TestLifecycleEvent event = TestLifecycles.createServiceLifecycleEvent(null, ds); TestLifecycles.after(event); } catch (Exception e) { throw new RuntimeException(e); } }
Example #29
Source File: ReceiveMessageServlet.java From cloud-pubsub-samples-java with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public final void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { // Validating unique subscription token before processing the message String subscriptionToken = System.getProperty( Constants.BASE_PACKAGE + ".subscriptionUniqueToken"); if (!subscriptionToken.equals(req.getParameter("token"))) { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); resp.getWriter().close(); return; } ServletInputStream inputStream = req.getInputStream(); // Parse the JSON message to the POJO model class JsonParser parser = JacksonFactory.getDefaultInstance() .createJsonParser(inputStream); parser.skipToKey("message"); PubsubMessage message = parser.parseAndClose(PubsubMessage.class); // Store the message in the datastore Entity messageToStore = new Entity("PubsubMessage"); messageToStore.setProperty("message", new String(message.decodeData(), "UTF-8")); messageToStore.setProperty("receipt-time", System.currentTimeMillis()); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(messageToStore); // Invalidate the cache MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(); memcacheService.delete(Constants.MESSAGE_CACHE_KEY); // Acknowledge the message by returning a success code resp.setStatus(HttpServletResponse.SC_OK); resp.getWriter().close(); }
Example #30
Source File: MetadataNamespacesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
void printAllNamespaces(DatastoreService ds, PrintWriter writer) { Query q = new Query(Entities.NAMESPACE_METADATA_KIND); for (Entity e : ds.prepare(q).asIterable()) { // A nonzero numeric id denotes the default namespace; // see <a href="#Namespace_Queries">Namespace Queries</a>, below if (e.getKey().getId() != 0) { writer.println("<default>"); } else { writer.println(e.getKey().getName()); } } }