Java Code Examples for com.google.cloud.datastore.QueryResults#next()

The following examples show how to use com.google.cloud.datastore.QueryResults#next() . 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: DatastoreSnippets.java    From google-cloud-java with 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 2
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private Cursor cursorPaging(int pageSize, Cursor pageCursor) {
  // [START datastore_cursor_paging]
  EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind("Task")
      .setLimit(pageSize);
  if (pageCursor != null) {
    queryBuilder.setStartCursor(pageCursor);
  }
  QueryResults<Entity> tasks = datastore.run(queryBuilder.build());
  while (tasks.hasNext()) {
    Entity task = tasks.next();
    // do something with the task
  }
  Cursor nextPageCursor = tasks.getCursorAfter();
  // [END datastore_cursor_paging]
  return nextPageCursor;
}
 
Example 4
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
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 5
Source File: DatastoreStatsTest.java    From catatumbo with 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 6
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunProjectionQuery() {
  setUpQueryTests();
  Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder()
      .setKind("Task")
      .setProjection("priority", "percent_complete")
      .build();
  // [START datastore_run_query_projection]
  List<Long> priorities = new LinkedList<>();
  List<Double> percentCompletes = new LinkedList<>();
  QueryResults<ProjectionEntity> tasks = datastore.run(query);
  while (tasks.hasNext()) {
    ProjectionEntity task = tasks.next();
    priorities.add(task.getLong("priority"));
    percentCompletes.add(task.getDouble("percent_complete"));
  }
  // [END datastore_run_query_projection]
  assertEquals(ImmutableList.of(4L), priorities);
  assertEquals(ImmutableList.of(10.0), percentCompletes);
}
 
Example 7
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of running a query to find all entities with an ancestor. */
// [TARGET run(Query)]
// [VARIABLE "my_parent_key_name"]
public List<Entity> run(String parentKeyName) {
  Datastore datastore = transaction.getDatastore();
  // [START run]
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind");
  Key parentKey = keyFactory.newKey(parentKeyName);
  // Build a query
  Query<Entity> query =
      Query.newEntityQueryBuilder()
          .setKind("MyKind")
          .setFilter(PropertyFilter.hasAncestor(parentKey))
          .build();
  QueryResults<Entity> results = transaction.run(query);
  List<Entity> entities = Lists.newArrayList();
  while (results.hasNext()) {
    Entity result = results.next();
    // do something with result
    entities.add(result);
  }
  transaction.commit();
  // [END run]
  return entities;
}
 
Example 8
Source File: DatastoreStatsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetKindsNs() {
  List<StatKindNs> statEntities = stats.getKindsNs();
  Query query = Query.newEntityQueryBuilder().setKind(StatConstants.STAT_KIND_NS).build();
  QueryResults<com.google.cloud.datastore.Entity> results = datastore.run(query);
  int i = 0;
  while (results.hasNext()) {
    StatKindNs statEntity = statEntities.get(i);
    com.google.cloud.datastore.Entity nativeEntity = results.next();
    assertTrue(equals(statEntity, nativeEntity));
    i++;
  }
}
 
Example 9
Source File: DatastoreStatsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompositeIndexesNs_Namespace() {
  final String namespace = "junit";
  List<StatCompositeIndexNs> statEntities = stats.getCompositeIndexesNs(namespace);
  Query query = Query.newEntityQueryBuilder().setNamespace(namespace)
      .setKind(StatConstants.STAT_COMPOSITE_INDEX_NS).build();
  QueryResults<com.google.cloud.datastore.Entity> results = datastore.run(query);
  int i = 0;
  while (results.hasNext()) {
    StatCompositeIndexNs statEntity = statEntities.get(i);
    com.google.cloud.datastore.Entity nativeEntity = results.next();
    assertTrue(equals(statEntity, nativeEntity));
    i++;
  }
}
 
Example 10
Source File: DatastoreStatsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompositeIndexesNs() {
  List<StatCompositeIndexNs> statEntities = stats.getCompositeIndexesNs();
  Query query = Query.newEntityQueryBuilder().setKind(StatConstants.STAT_COMPOSITE_INDEX_NS)
      .build();
  QueryResults<com.google.cloud.datastore.Entity> results = datastore.run(query);
  int i = 0;
  while (results.hasNext()) {
    StatCompositeIndexNs statEntity = statEntities.get(i);
    com.google.cloud.datastore.Entity nativeEntity = results.next();
    assertTrue(equals(statEntity, nativeEntity));
    i++;
  }
}
 
Example 11
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyByKindRunQuery() {
  setUpQueryTests();
  // [START datastore_property_by_kind_run_query]
  Key key = datastore.newKeyFactory().setKind("__kind__").newKey("Task");
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("__property__")
      .setFilter(PropertyFilter.hasAncestor(key))
      .build();
  QueryResults<Entity> results = datastore.run(query);
  Map<String, Collection<String>> representationsByProperty = new HashMap<>();
  while (results.hasNext()) {
    Entity result = results.next();
    String propertyName = result.getKey().getName();
    List<StringValue> representations = result.getList("property_representation");
    Collection<String> currentRepresentations = representationsByProperty.get(propertyName);
    if (currentRepresentations == null) {
      currentRepresentations = new HashSet<>();
      representationsByProperty.put(propertyName, currentRepresentations);
    }
    for (StringValue value : representations) {
      currentRepresentations.add(value.get());
    }
  }
  // [END datastore_property_by_kind_run_query]
  Map<String, Collection<String>> expected = ImmutableMap.<String, Collection<String>>builder()
      .put("category", Collections.singleton("STRING"))
      .put("done", Collections.singleton("BOOLEAN"))
      .put("completed", Collections.singleton("BOOLEAN"))
      .put("priority", Collections.singleton("INT64"))
      .put("created", Collections.singleton("INT64"))
      .put("percent_complete", Collections.singleton("DOUBLE"))
      .put("tag", Collections.singleton("STRING"))
      .build();
  assertEquals(expected, representationsByProperty);
}
 
Example 12
Source File: DatastoreStatsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetKind() {
  final String kindName = "StringField";
  StatKind statEntity = stats.getKind(kindName);
  Query query = Query.newEntityQueryBuilder().setNamespace("").setKind(StatConstants.STAT_KIND)
      .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 13
Source File: DatastoreStatsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetKinds() {
  List<StatKind> statEntities = stats.getKinds();
  Query query = Query.newEntityQueryBuilder().setNamespace("").setKind(StatConstants.STAT_KIND)
      .build();
  QueryResults<com.google.cloud.datastore.Entity> results = datastore.run(query);
  int i = 0;
  while (results.hasNext()) {
    StatKind statEntity = statEntities.get(i);
    com.google.cloud.datastore.Entity nativeEntity = results.next();
    assertTrue(equals(statEntity, nativeEntity));
    i++;
  }
}
 
Example 14
Source File: DefaultEntityManager.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public long deleteAll(String kind) {
  if (Utility.isNullOrEmpty(kind)) {
    throw new IllegalArgumentException("kind cannot be null or blank");
  }
  String query = "SELECT __key__ FROM " + kind;
  try {
    GqlQuery<Key> gqlQuery = Query.newGqlQueryBuilder(Query.ResultType.KEY, query)
        .setNamespace(getEffectiveNamespace()).build();
    QueryResults<Key> keys = datastore.run(gqlQuery);
    Key[] nativeKeys = new Key[DEFAULT_DELETE_ALL_BATCH_SIZE];
    long deleteCount = 0;
    int i = 0;
    while (keys.hasNext()) {
      nativeKeys[i++] = keys.next();
      if (i % DEFAULT_DELETE_ALL_BATCH_SIZE == 0) {
        datastore.delete(nativeKeys);
        deleteCount += DEFAULT_DELETE_ALL_BATCH_SIZE;
        i = 0;
      }
    }
    if (i > 0) {
      datastore.delete(Arrays.copyOfRange(nativeKeys, 0, i));
      deleteCount += i;
    }
    return deleteCount;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example 15
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given {@link KeyQueryRequest} and returns the response.
 * 
 * @param request
 *          the key query request
 * @return the query response
 */
public QueryResponse<DatastoreKey> executeKeyQueryRequest(KeyQueryRequest request) {
  try {
    GqlQuery.Builder<Key> queryBuilder = Query.newGqlQueryBuilder(ResultType.KEY,
        request.getQuery());
    queryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    queryBuilder.setAllowLiteral(request.isAllowLiterals());
    QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings());
    QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings());
    GqlQuery<Key> gqlQuery = queryBuilder.build();
    QueryResults<Key> results = nativeReader.run(gqlQuery);
    List<DatastoreKey> entities = new ArrayList<>();
    DefaultQueryResponse<DatastoreKey> response = new DefaultQueryResponse<>();
    response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    while (results.hasNext()) {
      Key result = results.next();
      DatastoreKey datastoreKey = new DefaultDatastoreKey(result);
      entities.add(datastoreKey);
    }
    response.setResults(entities);
    response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    response.setQueryResponseMetadata(
        new DefaultQueryResponseMetadata(
            QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults())));
    return response;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example 16
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> retrieve(int limit) {
  // Get Message saved in Datastore
  Datastore datastore = getDatastoreInstance();
  Query<Entity> query =
      Query.newEntityQueryBuilder()
          .setKind(messagesKind)
          .setLimit(limit)
          .addOrderBy(StructuredQuery.OrderBy.desc("publishTime"))
          .build();
  QueryResults<Entity> results = datastore.run(query);

  List<Message> messages = new ArrayList<>();
  while (results.hasNext()) {
    Entity entity = results.next();
    Message message = new Message(entity.getString("messageId"));
    String data = entity.getString("data");
    if (data != null) {
      message.setData(data);
    }
    String publishTime = entity.getString("publishTime");
    if (publishTime != null) {
      message.setPublishTime(publishTime);
    }
    messages.add(message);
  }
  return messages;
}
 
Example 17
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> retrieve(int limit) {
  // Get Message saved in Datastore
  Datastore datastore = getDatastoreInstance();
  Query<Entity> query =
      Query.newEntityQueryBuilder()
          .setKind(messagesKind)
          .setLimit(limit)
          .addOrderBy(StructuredQuery.OrderBy.desc("publishTime"))
          .build();
  QueryResults<Entity> results = datastore.run(query);

  List<Message> messages = new ArrayList<>();
  while (results.hasNext()) {
    Entity entity = results.next();
    Message message = new Message(entity.getString("messageId"));
    String data = entity.getString("data");
    if (data != null) {
      message.setData(data);
    }
    String publishTime = entity.getString("publishTime");
    if (publishTime != null) {
      message.setPublishTime(publishTime);
    }
    messages.add(message);
  }
  return messages;
}
 
Example 18
Source File: DatastoreServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  // store only the first two octets of a users ip address
  String userIp = req.getRemoteAddr();
  InetAddress address = InetAddress.getByName(userIp);
  if (address instanceof Inet6Address) {
    // nest indexOf calls to find the second occurrence of a character in a string
    // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
    userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
  } else if (address instanceof Inet4Address) {
    userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
  }

  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("visit");
  IncompleteKey key = keyFactory.setKind("visit").newKey();

  // Record a visit to the datastore, storing the IP and timestamp.
  FullEntity<IncompleteKey> curVisit =
      FullEntity.newBuilder(key).set("user_ip", userIp).set("timestamp", Timestamp.now()).build();
  datastore.add(curVisit);

  // Retrieve the last 10 visits from the datastore, ordered by timestamp.
  Query<Entity> query =
      Query.newEntityQueryBuilder()
          .setKind("visit")
          .setOrderBy(StructuredQuery.OrderBy.desc("timestamp"))
          .setLimit(10)
          .build();
  QueryResults<Entity> results = datastore.run(query);

  resp.setContentType("text/plain");
  PrintWriter out = resp.getWriter();
  out.print("Last 10 visits:\n");
  while (results.hasNext()) {
    Entity entity = results.next();
    out.format(
        "Time: %s Addr: %s\n", entity.getTimestamp("timestamp"), entity.getString("user_ip"));
  }
}
 
Example 19
Source File: DatastoreExample.java    From google-cloud-java with Apache License 2.0 4 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.printf("User '%s' does not exist.%n", userKey.getName());
    return;
  }
  if (user.contains("contact")) {
    FullEntity<IncompleteKey> contact = user.getEntity("contact");
    String email = contact.getString("email");
    String phone = contact.getString("phone");
    System.out.printf(
        "User '%s' email is '%s', phone is '%s'.%n", userKey.getName(), email, phone);
  }
  System.out.printf("User '%s' has %d comment[s].%n", userKey.getName(), user.getLong("count"));
  int limit = 200;
  Map<Timestamp, String> sortedComments = new TreeMap<>();
  StructuredQuery<Entity> query =
      Query.newEntityQueryBuilder()
          .setNamespace(NAMESPACE)
          .setKind(COMMENT_KIND)
          .setFilter(PropertyFilter.hasAncestor(userKey))
          .setLimit(limit)
          .build();
  while (true) {
    QueryResults<Entity> results = tx.run(query);
    int resultCount = 0;
    while (results.hasNext()) {
      Entity result = results.next();
      sortedComments.put(result.getTimestamp("timestamp"), result.getString("content"));
      resultCount++;
    }
    if (resultCount < limit) {
      break;
    }
    query = query.toBuilder().setStartCursor(results.getCursorAfter()).build();
  }
  // We could have added "ORDER BY timestamp" to the query to avoid sorting, but that would
  // require adding an ancestor index for timestamp.
  // See: https://cloud.google.com/datastore/docs/tools/indexconfig
  for (Map.Entry<Timestamp, String> entry : sortedComments.entrySet()) {
    System.out.printf("\t%s: %s%n", entry.getKey(), entry.getValue());
  }
}
 
Example 20
Source File: AddEntitiesAndRunQuery.java    From google-cloud-java with Apache License 2.0 4 votes vote down vote up
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("[email protected]");
  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("[email protected]");
  Entity janeEntity =
      Entity.newBuilder(janeKey)
          .set("name", "Jane Doe")
          .set("age", 44)
          .set("favorite_food", "pizza")
          .build();
  Key joeKey = keyFactory.newKey("[email protected]");
  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!");
  }
}