本文整理了Java中com.google.cloud.datastore.Datastore.run()
方法的一些代码示例,展示了Datastore.run()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Datastore.run()
方法的具体详情如下:
包路径:com.google.cloud.datastore.Datastore
类名称:Datastore
方法名:run
[英]Submits a Query and returns its result. ReadOptions can be specified if desired.
Example of running a query to find all entities of one kind.
String kind = "my_kind";}
Example of running a query to find all entities with a matching property value.
String kind = "my_kind";}
[中]提交查询并返回其结果。如果需要,可以指定ReadOptions。
运行查询以查找一种类型的所有实体的示例。
String kind = "my_kind";}
运行查询以查找具有匹配属性值的所有实体的示例。
String kind = "my_kind";}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of creating and running a GQL query. */
// [TARGET newGqlQueryBuilder(String)]
// [VARIABLE "my_kind"]
public QueryResults<?> newQuery(String kind) {
// [START newQuery]
String gqlQuery = "select * from " + kind;
Query<?> query = Query.newGqlQueryBuilder(gqlQuery).build();
QueryResults<?> results = datastore.run(query);
// Use results
// [END newQuery]
return results;
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of creating and running a typed GQL query. */
// [TARGET newGqlQueryBuilder(ResultType, String)]
// [VARIABLE "my_kind"]
public QueryResults<Entity> newTypedQuery(String kind) {
// [START newTypedQuery]
String gqlQuery = "select * from " + kind;
Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gqlQuery).build();
QueryResults<Entity> results = datastore.run(query);
// Use results
// [END newTypedQuery]
return results;
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of creating and running an entity query. */
// [TARGET newEntityQueryBuilder()]
// [VARIABLE "my_kind"]
public QueryResults<Entity> newEntityQuery(String kind) {
// [START newEntityQuery]
Query<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build();
QueryResults<Entity> results = datastore.run(query);
// Use results
// [END newEntityQuery]
return results;
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
private <T> Iterator<T> getStronglyConsistentResults(Query scQuery, Query query)
throws InterruptedException {
// scQuery is equivalent to query, but with an ancestor filter in it
// this makes scQuery strongly consistent
QueryResults<T> scResults = DATASTORE.run(scQuery);
List<T> scResultsCopy = makeResultsCopy(scResults);
Set<T> scResultsSet = new HashSet<>(scResultsCopy);
int maxAttempts = 20;
while (maxAttempts > 0) {
--maxAttempts;
QueryResults<T> results = DATASTORE.run(query);
List<T> resultsCopy = makeResultsCopy(results);
Set<T> resultsSet = new HashSet<>(resultsCopy);
if (scResultsSet.size() == resultsSet.size() && scResultsSet.containsAll(resultsSet)) {
return resultsCopy.iterator();
}
Thread.sleep(500);
}
throw new RuntimeException(
"reached max number of attempts to get strongly consistent results.");
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of creating and running a projection entity query. */
// [TARGET newProjectionEntityQueryBuilder()]
// [VARIABLE "my_kind"]
// [VARIABLE "my_property"]
public QueryResults<ProjectionEntity> newProjectionEntityQuery(String kind, String property) {
// [START newProjectionEntityQuery]
Query<ProjectionEntity> query =
Query.newProjectionEntityQueryBuilder().setKind(kind).addProjection(property).build();
QueryResults<ProjectionEntity> results = datastore.run(query);
// Use results
// [END newProjectionEntityQuery]
return results;
}
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of running a query to find all entities with a matching property value. */
// [TARGET run(Query, ReadOption...)]
// [VARIABLE "my_kind"]
// [VARIABLE "my_property"]
// [VARIABLE "my_value"]
public List<Entity> runQueryOnProperty(String kind, String property, String value) {
// TODO change so that it's not necessary to hold the entities in a list for integration testing
// [START runQueryOnProperty]
StructuredQuery<Entity> query =
Query.newEntityQueryBuilder()
.setKind(kind)
.setFilter(PropertyFilter.eq(property, value))
.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 runQueryOnProperty]
return entities;
}
}
代码示例来源:origin: googleapis/google-cloud-java
/** Deletes all entities in the namespace associated with this {@link RemoteDatastoreHelper}. */
public void deleteNamespace() {
StructuredQuery<Key> query = Query.newKeyQueryBuilder().setNamespace(namespace).build();
QueryResults<Key> keys = datastore.run(query);
while (keys.hasNext()) {
datastore.delete(keys.next());
}
}
代码示例来源:origin: google/data-transfer-project
/**
* Finds the ID of the first {@link PortabilityJob} in state {@code jobState} in Datastore, or
* null if none found.
*
* <p>TODO(rtannenbaum): Order by creation time so we can process jobs in a FIFO manner. Trying
* to OrderBy.asc("created") currently fails because we don't yet have an index set up.
*/
@Override
public UUID findFirst(JobAuthorization.State jobState) {
Query<Key> query =
Query.newKeyQueryBuilder()
.setKind(KIND)
.setFilter(PropertyFilter.eq(PortabilityJob.AUTHORIZATION_STATE, jobState.name()))
// .setOrderBy(OrderBy.asc("created"))
.setLimit(1)
.build();
QueryResults<Key> results = datastore.run(query);
if (!results.hasNext()) {
return null;
}
Key key = results.next();
return UUID.fromString(key.getName());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testStructuredQueryPagination() throws DatastoreException {
List<RunQueryResponse> responses = buildResponsesForQueryPagination();
for (int i = 0; i < responses.size(); i++) {
EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class)))
.andReturn(responses.get(i));
}
EasyMock.replay(rpcFactoryMock, rpcMock);
Datastore datastore = rpcMockOptions.getService();
QueryResults<Key> results = datastore.run(Query.newKeyQueryBuilder().build());
int count = 0;
while (results.hasNext()) {
count += 1;
results.next();
}
assertEquals(count, 5);
EasyMock.verify(rpcFactoryMock, rpcMock);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testGqlQueryPagination() throws DatastoreException {
List<RunQueryResponse> responses = buildResponsesForQueryPagination();
for (int i = 0; i < responses.size(); i++) {
EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class)))
.andReturn(responses.get(i));
}
EasyMock.replay(rpcFactoryMock, rpcMock);
Datastore mockDatastore = rpcMockOptions.getService();
QueryResults<Key> results =
mockDatastore.run(
Query.newGqlQueryBuilder(ResultType.KEY, "select __key__ from *").build());
int count = 0;
while (results.hasNext()) {
count += 1;
results.next();
}
assertEquals(count, 5);
EasyMock.verify(rpcFactoryMock, rpcMock);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testSkippedResults() {
Query<Key> query = Query.newKeyQueryBuilder().setOffset(Integer.MAX_VALUE).build();
int numberOfEntities = DATASTORE.run(query).getSkippedResults();
assertEquals(2, numberOfEntities);
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testRunKeyQueryWithOffset() {
Query<Key> query = Query.newKeyQueryBuilder().setOffset(Integer.MAX_VALUE).build();
int numberOfEntities = datastore.run(query).getSkippedResults();
assertEquals(2, numberOfEntities);
}
代码示例来源:origin: googleapis/google-cloud-java
StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setLimit(limit).build();
while (true) {
QueryResults<Entity> results = datastore.run(query);
int resultCount = 0;
while (results.hasNext()) {
代码示例来源:origin: googleapis/google-cloud-java
@Before
public void setUp() {
rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
rpcMockOptions =
options
.toBuilder()
.setRetrySettings(ServiceOptions.getDefaultRetrySettings())
.setServiceRpcFactory(rpcFactoryMock)
.build();
EasyMock.expect(rpcFactoryMock.create(rpcMockOptions)).andReturn(rpcMock);
StructuredQuery<Key> query = Query.newKeyQueryBuilder().build();
QueryResults<Key> result = datastore.run(query);
datastore.delete(Iterators.toArray(result, Key.class));
datastore.add(ENTITY1, ENTITY2);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testEventualConsistencyQuery() {
ReadOptions readOption =
ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build();
com.google.datastore.v1.GqlQuery query =
com.google.datastore.v1.GqlQuery.newBuilder().setQueryString("FROM * SELECT *").build();
RunQueryRequest.Builder expectedRequest =
RunQueryRequest.newBuilder()
.setReadOptions(readOption)
.setGqlQuery(query)
.setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build());
EasyMock.expect(rpcMock.runQuery(expectedRequest.build()))
.andReturn(RunQueryResponse.newBuilder().build());
EasyMock.replay(rpcFactoryMock, rpcMock);
Datastore datastore = rpcMockOptions.getService();
datastore.run(
Query.newGqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency());
EasyMock.verify(rpcFactoryMock, rpcMock);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testRunGqlQueryWithCasting() {
@SuppressWarnings("unchecked")
Query<Entity> query1 =
(Query<Entity>) Query.newGqlQueryBuilder("select * from " + KIND1).build();
QueryResults<Entity> results1 = datastore.run(query1);
assertTrue(results1.hasNext());
assertEquals(ENTITY1, results1.next());
assertFalse(results1.hasNext());
Query<?> query2 = Query.newGqlQueryBuilder("select * from " + KIND1).build();
QueryResults<?> results2 = datastore.run(query2);
assertSame(Entity.class, results2.getResultClass());
@SuppressWarnings("unchecked")
QueryResults<Entity> results3 = (QueryResults<Entity>) results2;
assertTrue(results3.hasNext());
assertEquals(ENTITY1, results3.next());
assertFalse(results3.hasNext());
}
代码示例来源:origin: googleapis/google-cloud-java
Query.newGqlQueryBuilder("select * from " + KIND1).setNamespace(NAMESPACE).build();
QueryResults<?> results2 = DATASTORE.run(query2);
代码示例来源:origin: googleapis/google-cloud-java
Query<Entity> query =
Query.newEntityQueryBuilder().setKind(KIND1).setOrderBy(OrderBy.asc("__key__")).build();
QueryResults<Entity> results1 = datastore.run(query);
assertTrue(results1.hasNext());
assertEquals(ENTITY1, results1.next());
QueryResults<Key> results2 = datastore.run(keyOnlyQuery);
assertTrue(results2.hasNext());
assertEquals(ENTITY1.getKey(), results2.next());
QueryResults<ProjectionEntity> results3 = datastore.run(keyOnlyProjectionQuery);
assertTrue(results3.hasNext());
ProjectionEntity projectionEntity = results3.next();
.build();
QueryResults<ProjectionEntity> results4 = datastore.run(projectionQuery);
assertTrue(results4.hasNext());
ProjectionEntity entity = results4.next();
内容来源于网络,如有侵权,请联系作者删除!