Java Code Examples for com.google.cloud.datastore.Query

The following examples show how to use com.google.cloud.datastore.Query. These examples are extracted from open source projects. 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
/**
 * Removes all deleted blobs tracked in this index.
 *
 */
void removeData() {
  log.warn("removing all entries in the index of soft-deleted blobs...");
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setNamespace(namespace)
      .setKind(DELETED_BLOBS)
      .build();
  QueryResults<Entity> results = gcsDatastore.run(query);
  List<Key> keys = StreamSupport.stream(
      Spliterators.spliteratorUnknownSize(results, Spliterator.ORDERED),
      false).map(entity -> entity.getKey()).collect(Collectors.toList());

  // datastore has a hard limit of 500 keys in a single delete
  List<List<Key>> partitions = Lists.partition(keys, 500);
  partitions.stream().forEach(partition -> gcsDatastore.delete(partition.toArray(new Key[partition.size()])) );

  log.warn("deleted {} blobIds from the soft-deleted blob index", keys.size());
}
 
Example 2
Source Project: java-docs-samples   Source File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void wipeDatastore() {
  Datastore datastore = getDatastore();
  QueryResults<Key> guestbooks =
      datastore.run(Query.newKeyQueryBuilder().setKind("Greeting").build());
  ArrayList<Key> keys = Lists.newArrayList(guestbooks);

  if (!keys.isEmpty()) {
    datastore.delete(keys.toArray(new Key[keys.size()]));
  }
}
 
Example 3
Source Project: spring-cloud-gcp   Source File: PartTreeDatastoreQuery.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * In case the return type is a closed projection (only the projection fields are necessary
 * to construct an entity object), this method returns a {@link ProjectionEntityQuery.Builder}
 * with projection fields only.
 * Otherwise returns a {@link EntityQuery.Builder}.
 */
private Builder<?> getEntityOrProjectionQueryBuilder() {
	ProjectionInformation projectionInformation =
			this.projectionFactory.getProjectionInformation(this.queryMethod.getReturnedObjectType());

	if (projectionInformation != null &&
			projectionInformation.getType() != this.entityType
			&& projectionInformation.isClosed()) {
		ProjectionEntityQuery.Builder projectionEntityQueryBuilder = Query.newProjectionEntityQueryBuilder();
		projectionInformation.getInputProperties().forEach(
				propertyDescriptor ->
						projectionEntityQueryBuilder.addProjection(mapToFieldName(propertyDescriptor)));
		return projectionEntityQueryBuilder;
	}
	return StructuredQuery.newEntityQueryBuilder();
}
 
Example 4
Source Project: java-docs-samples   Source File: MessageRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<String> retrieveClaims(int limit) {
  // Get claim saved in Datastore
  Datastore datastore = getDatastoreInstance();
  Query<Entity> query = Query.newEntityQueryBuilder().setKind(claimsKind).setLimit(limit).build();
  QueryResults<Entity> results = datastore.run(query);

  List<String> claims = new ArrayList<>();
  while (results.hasNext()) {
    Entity entity = results.next();
    String claim = entity.getString("claim");
    if (claim != null) {
      claims.add(claim);
    }
  }
  return claims;
}
 
Example 5
Source Project: google-cloud-java   Source File: DatastoreSnippets.java    License: Apache License 2.0 6 votes vote down vote up
/** Example of running a query to find all entities of one kind. */
// [TARGET run(Query, ReadOption...)]
// [VARIABLE "my_kind"]
public List<Entity> runQuery(String kind) {
  // TODO change so that it's not necessary to hold the entities in a list for integration testing
  // [START runQuery]
  StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build();
  QueryResults<Entity> results = datastore.run(query);
  List<Entity> entities = Lists.newArrayList();
  while (results.hasNext()) {
    Entity result = results.next();
    // do something with result
    entities.add(result);
  }
  // [END runQuery]
  return entities;
}
 
Example 6
Source Project: catatumbo   Source File: DefaultDatastoreMetadata.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getKinds(boolean excludeSystemKinds) {
  try {
    String query = "SELECT __key__ FROM " + ENTITY_KINDS + " ORDER BY __key__";
    GqlQuery.Builder<Key> gqlQueryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, query);
    gqlQueryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    GqlQuery<Key> gqlQuery = gqlQueryBuilder.build();
    QueryResults<Key> results = entityManager.getDatastore().run(gqlQuery);
    List<String> kinds = new ArrayList<>(50);
    while (results.hasNext()) {
      Key key = results.next();
      String kind = key.getName();
      if (excludeSystemKinds && kind.startsWith("__")) {
        continue;
      }
      kinds.add(key.getName());
    }
    return kinds;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example 7
Source Project: java-docs-samples   Source File: MessageRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<String> retrieveTokens(int limit) {
  // Get token saved in Datastore
  Datastore datastore = getDatastoreInstance();
  Query<Entity> query = Query.newEntityQueryBuilder().setKind(tokensKind).setLimit(limit).build();
  QueryResults<Entity> results = datastore.run(query);

  List<String> tokens = new ArrayList<>();
  while (results.hasNext()) {
    Entity entity = results.next();
    String token = entity.getString("token");
    if (token != null) {
      tokens.add(token);
    }
  }
  return tokens;
}
 
Example 8
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPropertyRunQuery() {
  setUpQueryTests();
  // [START datastore_property_run_query]
  Query<Key> query = Query.newKeyQueryBuilder().setKind("__property__").build();
  QueryResults<Key> keys = datastore.run(query);
  Map<String, Collection<String>> propertiesByKind = new HashMap<>();
  while (keys.hasNext()) {
    Key key = keys.next();
    String kind = key.getParent().getName();
    String propertyName = key.getName();
    Collection<String> properties = propertiesByKind.get(kind);
    if (properties == null) {
      properties = new HashSet<>();
      propertiesByKind.put(kind, properties);
    }
    properties.add(propertyName);
  }
  // [END datastore_property_run_query]
  Map<String, ImmutableSet<String>> expected = ImmutableMap.of("Task", ImmutableSet.of(
      "done", "category", "done", "completed", "priority", "created", "percent_complete", "tag"));
  assertEquals(expected, propertiesByKind);
}
 
Example 9
Source Project: catatumbo   Source File: DatastoreStatsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetKindNs_Namespace_KindName() {
  final String namespace = "junit";
  final String kindName = "StringField";
  StatKindNs statEntity = stats.getKindNs(namespace, kindName);
  Query query = Query.newEntityQueryBuilder().setNamespace(namespace)
      .setKind(StatConstants.STAT_KIND_NS)
      .setFilter(StructuredQuery.PropertyFilter.eq(StatConstants.PROP_KIND_NAME, kindName))
      .build();
  QueryResults<com.google.cloud.datastore.Entity> results = datastore.run(query);
  com.google.cloud.datastore.Entity nativeEntity = null;
  if (results.hasNext()) {
    nativeEntity = results.next();
  }
  assertTrue(equals(statEntity, nativeEntity));
}
 
Example 10
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Datastore and cleans out any residual values.  Also initializes global variables
 * used for testing.
 */
@Before
public void setUp() {
  datastore = HELPER.getOptions().toBuilder().setNamespace("ghijklmnop").build().getService();
  StructuredQuery<Key> query = Query.newKeyQueryBuilder().build();
  QueryResults<Key> result = datastore.run(query);
  datastore.delete(Iterators.toArray(result, Key.class));
  keyFactory = datastore.newKeyFactory().setKind("Task");
  taskKey = keyFactory.newKey("some-arbitrary-key");
  testEntity = Entity.newBuilder(taskKey, TEST_FULL_ENTITY).build();
  Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  calendar.set(1990, JANUARY, 1);
  startDate = Timestamp.of(calendar.getTime());
  calendar.set(2000, JANUARY, 1);
  endDate = Timestamp.of(calendar.getTime());
  calendar.set(1999, DECEMBER, 31);
  includedDate = Timestamp.of(calendar.getTime());
}
 
Example 11
Source Project: tomcat-runtime   Source File: DatastoreStore.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a new session usable by Tomcat, from a serialized session in a Datastore Entity.
 * @param sessionKey The key associated with the session metadata and attributes.
 * @return A new session containing the metadata and attributes stored in the entity.
 * @throws ClassNotFoundException Thrown if a class serialized in the entity is not available in
 *                                this context.
 * @throws IOException Thrown when an error occur during the deserialization.
 */
private DatastoreSession deserializeSession(Key sessionKey)
    throws ClassNotFoundException, IOException {
  TraceContext loadingSessionContext = startSpan("Fetching the session from Datastore");
  Iterator<Entity> entities = datastore.run(Query.newEntityQueryBuilder()
      .setKind(sessionKind)
      .setFilter(PropertyFilter.hasAncestor(sessionKey))
      .build());
  endSpan(loadingSessionContext);

  DatastoreSession session = null;
  if (entities.hasNext()) {
    session = (DatastoreSession) manager.createEmptySession();
    TraceContext deserializationContext = startSpan("Deserialization of the session");
    session.restoreFromEntities(sessionKey, Lists.newArrayList(entities));
    endSpan(deserializationContext);
  }
  return session;
}
 
Example 12
Source Project: styx   Source File: DatastoreStorage.java    License: Apache License 2.0 6 votes vote down vote up
private EntityQuery.Builder backfillQueryBuilder(boolean showAll, Filter... filters) {
  final EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind(KIND_BACKFILL);

  final List<Filter> andedFilters = Lists.newArrayList(filters);

  if (!showAll) {
    andedFilters.add(PropertyFilter.eq(PROPERTY_ALL_TRIGGERED, false));
    andedFilters.add(PropertyFilter.eq(PROPERTY_HALTED, false));
  }

  if (!andedFilters.isEmpty()) {
    final Filter head = andedFilters.get(0);
    final Filter[] tail = andedFilters.stream().skip(1).toArray(Filter[]::new);
    queryBuilder.setFilter(CompositeFilter.and(head, tail));
  }

  return queryBuilder;
}
 
Example 13
Source Project: getting-started-java   Source File: DatastoreDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()       // Build the Query
      .setKind("Book5")                                     // We only care about Books
      .setLimit(10)                                         // Only show 10 at a time
      .setStartCursor(startCursor)                          // Where we left off
      .setOrderBy(OrderBy.asc(Book.TITLE))                  // Use default Index "title"
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example 14
Source Project: getting-started-java   Source File: DatastoreDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()          // Build the Query
      .setKind("Book5")                                        // We only care about Books
      .setFilter(PropertyFilter.eq(Book.CREATED_BY_ID, userId))// Only for this user
      .setLimit(10)                                            // Only show 10 at a time
      .setStartCursor(startCursor)                             // Where we left off
      // a custom datastore index is required since you are filtering by one property
      // but ordering by another
      .setOrderBy(OrderBy.asc(Book.TITLE))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the Query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example 15
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example 16
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 17
Source Project: getting-started-java   Source File: DatastoreDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()       // Build the Query
      .setKind("Book2")                                     // We only care about Books
      .setLimit(10)                                         // Only show 10 at a time
      .setStartCursor(startCursor)                          // Where we left off
      .setOrderBy(OrderBy.asc(Book.TITLE))                  // Use default Index "title"
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example 18
Source Project: google-cloud-java   Source File: DatastoreExample.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run(Transaction tx, Key userKey, Void arg) {
  Entity user = tx.get(userKey);
  if (user == null) {
    System.out.println("Nothing to delete, user does not exist.");
    return;
  }
  Query<Key> query =
      Query.newKeyQueryBuilder()
          .setNamespace(NAMESPACE)
          .setKind(COMMENT_KIND)
          .setFilter(PropertyFilter.hasAncestor(userKey))
          .build();
  QueryResults<Key> comments = tx.run(query);
  int count = 0;
  while (comments.hasNext()) {
    tx.delete(comments.next());
    count++;
  }
  tx.delete(userKey);
  System.out.printf("Deleting user '%s' and %d comment[s].%n", userKey.getName(), count);
}
 
Example 19
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 20
Source Project: getting-started-java   Source File: DatastoreDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()       // Build the Query
      .setKind("Book3")                                     // We only care about Books
      .setLimit(10)                                         // Only show 10 at a time
      .setStartCursor(startCursor)                          // Where we left off
      .setOrderBy(OrderBy.asc(Book.TITLE))                  // Use default Index "title"
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example 21
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example 22
Source Project: getting-started-java   Source File: DatastoreDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()       // Build the Query
      .setKind("Book4")                                     // We only care about Books
      .setLimit(10)                                         // Only show 10 at a time
      .setStartCursor(startCursor)                          // Where we left off
      .setOrderBy(OrderBy.asc(Book.TITLE))                  // Use default Index "title"
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example 23
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example 24
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNamespaceRunQuery() {
  setUpQueryTests();
  // [START datastore_namespace_run_query]
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("__namespace__");
  Key startNamespace = keyFactory.newKey("g");
  Key endNamespace = keyFactory.newKey("h");
  Query<Key> query = Query.newKeyQueryBuilder()
      .setKind("__namespace__")
      .setFilter(CompositeFilter.and(
          PropertyFilter.gt("__key__", startNamespace),
          PropertyFilter.lt("__key__", endNamespace)))
      .build();
  List<String> namespaces = new ArrayList<>();
  QueryResults<Key> results = datastore.run(query);
  while (results.hasNext()) {
    namespaces.add(results.next().getName());
  }
  // [END datastore_namespace_run_query]
  assertEquals(ImmutableList.of("ghijklmnop"), namespaces);
}
 
Example 25
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testUnindexedPropertyQuery() {
  setUpQueryTests();
  // [START datastore_unindexed_property_query]
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("Task")
      .setFilter(PropertyFilter.eq("description", "A task description"))
      .build();
  // [END datastore_unindexed_property_query]
  QueryResults<Entity> results = datastore.run(query);
  assertFalse(results.hasNext());
}
 
Example 26
private QueryResults<Entity> getShards() {
  Query<Entity> shardQuery = Query.newEntityQueryBuilder()
      .setFilter(PropertyFilter.hasAncestor(shardRoot))
      .setNamespace(namespace)
      .setKind(SHARD)
      .build();

  return datastore.run(shardQuery);
}
 
Example 27
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProjectionQuery() {
  setUpQueryTests();
  // [START datastore_projection_query]
  Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder()
      .setKind("Task")
      .setProjection("priority", "percent_complete")
      .build();
  // [END datastore_projection_query]
  assertValidQuery(query);
}
 
Example 28
Source Project: data-transfer-project   Source File: GoogleJobStore.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Integer> getCounts(UUID jobId) {
  Query<Entity> query = getCountsQuery(jobId);
  QueryResults<Entity> results = datastore.run(query);
  ImmutableMap.Builder<String, Integer> countsMapBuilder = ImmutableMap.builder();

  while (results.hasNext()) {
    Entity result = results.next();
    String dataType = result.getKey().getName();
    long count = result.getLong(COUNTS_FIELD);
    countsMapBuilder.put(dataType, (int) count);
  }

  return countsMapBuilder.build();
}
 
Example 29
Source Project: data-transfer-project   Source File: GoogleJobStore.java    License: Apache License 2.0 5 votes vote down vote up
private Query<Entity> getCountsQuery(UUID jobId) {
  // Use the main job as the ancestor to all the errors, see:
  // http://cloud/datastore/docs/concepts/queries#ancestor_queries
  Key ancestorKey = datastore.newKeyFactory().setKind(JOB_KIND).newKey(jobId.toString());

  return Query.newEntityQueryBuilder()
      .setKind(COUNTS_KIND)
      .setFilter(PropertyFilter.hasAncestor(ancestorKey))
      .build();
}
 
Example 30
Source Project: google-cloud-java   Source File: QuerySnippets.java    License: Apache License 2.0 5 votes vote down vote up
/** Example of creating and running a key query. */
// [TARGET newKeyQueryBuilder()]
// [VARIABLE "my_kind"]
public QueryResults<Key> newKeyQuery(String kind) {
  // [START newKeyQuery]
  Query<Key> query = Query.newKeyQueryBuilder().setKind(kind).build();
  QueryResults<Key> results = datastore.run(query);
  // Use results
  // [END newKeyQuery]
  return results;
}