public void ProjectionQuery() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; // Sample: ProjectionQuery RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, Query = new Query("Task") { Projection = { "priority", "percentage_complete" } } }; DatastoreClient client = DatastoreClient.Create(); RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Entity entity = result.Entity; Console.WriteLine($"{(int)entity["priority"]}: {(double?)entity["percentage_complete"]}"); } // End sample }
public void PaginateWithCursor() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; ByteString pageCursor = null; int pageSize = 5; // Sample: PaginateWithCursor RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, ReadOptions = new ReadOptions { ReadConsistency = ReadConsistency.Eventual }, Query = new Query("Task") { Limit = pageSize, StartCursor = pageCursor ?? ByteString.Empty } }; DatastoreClient client = DatastoreClient.Create(); RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Entity entity = result.Entity; // Do something with the task entity } ByteString nextPageCursor = response.Batch.EndCursor; // End sample }
public void PropertyQuery() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; PartitionId partitionId = new PartitionId(projectId, namespaceId); // Sample: PropertyQuery DatastoreClient client = DatastoreClient.Create(); RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, Query = new Query(DatastoreConstants.PropertyKind) { Projection = { DatastoreConstants.KeyProperty } } }; RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Key key = result.Entity.Key; string propertyName = key.Path.Last().Name; string kind = key.GetParent().Path.Last().Name; Console.WriteLine($"Kind: {kind}; Property: {propertyName}"); } // End sample }
public void CompositeFilterQuery() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; // Sample: CompositeFilter Query query = new Query("Task") { Filter = Filter.And( Filter.Equal("done", false), Filter.GreaterThanOrEqual("priority", 4) ), Order = { { "priority", Direction.Descending } } }; DatastoreClient client = DatastoreClient.Create(); RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, ReadOptions = new ReadOptions { ReadConsistency = ReadConsistency.Eventual }, Query = query }; RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Entity entity = result.Entity; Console.WriteLine((string)entity["description"]); } // TODO: Results beyond this batch? // End sample }
public void GqlQuery() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; // Snippet: RunQuery(string,PartitionId,ReadOptions,GqlQuery,CallSettings) DatastoreClient client = DatastoreClient.Create(); GqlQuery gqlQuery = new GqlQuery { QueryString = "SELECT * FROM book WHERE author = @author", NamedBindings = { { "author", new GqlQueryParameter { Value = "Jane Austen" } } }, }; RunQueryResponse response = client.RunQuery( projectId, partitionId, new ReadOptions { ReadConsistency = ReadConsistency.Eventual }, gqlQuery); foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity); } // End snippet Assert.Equal(1, response.Batch.EntityResults.Count); Entity entity = response.Batch.EntityResults[0].Entity; Assert.Equal("Jane Austen", (string)entity["author"]); Assert.Equal("Pride and Prejudice", (string)entity["title"]); }
public void StructuredQuery() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; // Snippet: RunQuery(string,PartitionId,ReadOptions,Query,CallSettings) DatastoreClient client = DatastoreClient.Create(); Query query = new Query("book") { Filter = Filter.Equal("author", "Jane Austen") }; RunQueryResponse response = client.RunQuery( projectId, partitionId, new ReadOptions { ReadConsistency = ReadConsistency.Eventual }, query); foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity); } // End snippet Assert.Equal(1, response.Batch.EntityResults.Count); Entity entity = response.Batch.EntityResults[0].Entity; Assert.Equal("Jane Austen", (string)entity["author"]); Assert.Equal("Pride and Prejudice", (string)entity["title"]); }
public void RunQuery2() { // Snippet: RunQuery(string,PartitionId,ReadOptions,GqlQuery,CallSettings) // Create client DatastoreClient datastoreClient = DatastoreClient.Create(); // Initialize request argument(s) string projectId = ""; PartitionId partitionId = new PartitionId(); ReadOptions readOptions = new ReadOptions(); GqlQuery gqlQuery = new GqlQuery(); // Make the request RunQueryResponse response = datastoreClient.RunQuery(projectId, partitionId, readOptions, gqlQuery); // End snippet }
public void RunQuery() { string projectId = _fixture.ProjectId; PartitionId partitionId = _fixture.PartitionId; // Snippet: RunQuery(RunQueryRequest,*) DatastoreClient client = DatastoreClient.Create(); RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, ReadOptions = new ReadOptions { ReadConsistency = ReadConsistency.Eventual }, }; // Structured query request.Query = new Query("book") { Filter = Filter.Equal("author", "Jane Austen") }; RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity); } // Equivalent GQL query request.GqlQuery = new GqlQuery { QueryString = "SELECT * FROM book WHERE author = @author", NamedBindings = { { "author", new GqlQueryParameter { Value = "Jane Austen" } } }, }; foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity); } // End snippet Assert.Equal(1, response.Batch.EntityResults.Count); Entity entity = response.Batch.EntityResults[0].Entity; Assert.Equal("Jane Austen", (string)entity["author"]); Assert.Equal("Pride and Prejudice", (string)entity["title"]); }
/// <summary>Snippet for RunQuery</summary> public void RunQuery_RequestObject() { // Snippet: RunQuery(RunQueryRequest,CallSettings) // Create client DatastoreClient datastoreClient = DatastoreClient.Create(); // Initialize request argument(s) RunQueryRequest request = new RunQueryRequest { ProjectId = "", PartitionId = new PartitionId(), }; // Make the request RunQueryResponse response = datastoreClient.RunQuery(request); // End snippet }
public void NamespaceQuery() { string projectId = _fixture.ProjectId; // Sample: NamespaceQuery DatastoreClient client = DatastoreClient.Create(); PartitionId partitionId = new PartitionId(projectId); RunQueryResponse response = client.RunQuery(projectId, partitionId, null, new Query(DatastoreConstants.NamespaceKind)); foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity.Key.Path.Last().Name); } // End sample }
public void KindQuery() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; PartitionId partitionId = new PartitionId(projectId, namespaceId); // Sample: KindQuery DatastoreClient client = DatastoreClient.Create(); RunQueryRequest request = new RunQueryRequest { ProjectId = projectId, PartitionId = partitionId, Query = new Query(DatastoreConstants.KindKind) }; RunQueryResponse response = client.RunQuery(request); foreach (EntityResult result in response.Batch.EntityResults) { Console.WriteLine(result.Entity.Key.Path.Last().Name); } // End sample }