Skip to content

Cosmos DB: Add single partition query support#17657

Merged
ealsur merged 31 commits intoAzure:mainfrom
ealsur:users/ealsur/cosmosquerypk
Apr 22, 2022
Merged

Cosmos DB: Add single partition query support#17657
ealsur merged 31 commits intoAzure:mainfrom
ealsur:users/ealsur/cosmosquerypk

Conversation

@ealsur
Copy link
Copy Markdown
Member

@ealsur ealsur commented Apr 19, 2022

Cosmos DB single partition query support

This PR adds support for the Go Cosmos DB client to execute single partition (scoped to a PartitionKey) queries.

Example API usage:

pk := azcosmos.NewPartitionKeyString("newPartitionKey")

queryPager := container.NewQueryItemsPager("select * from docs c", pk, nil)
for queryPager.More() {
	queryResponse, err := queryPager.NextPage(context.Background())
	if err != nil {
		var responseErr *azcore.ResponseError
		errors.As(err, &responseErr)
		panic(responseErr)
	}

	for _, item := range queryResponse.Items {
		var itemResponseBody map[string]interface{}
		json.Unmarshal(item, &itemResponseBody)
		// consume itemResponseBody content
	}

	// If there is need to stop and paginate, extract queryResponse.ContinuationToken and stop
}

To continue a previously stopped query:

pk := azcosmos.NewPartitionKeyString("newPartitionKey")
previouslySavedToken := "<value of a previously saved continuation token>"
queryPager := container.NewQueryItemsPager("select * from docs c", pk, &QueryOptions{ContinuationToken = previouslySavedToken})
for queryPager.More() {
	queryResponse, err := queryPager.NextPage(context.Background())
	if err != nil {
		var responseErr *azcore.ResponseError
		errors.As(err, &responseErr)
		panic(responseErr)
	}

	for _, item := range queryResponse.Items {
		var itemResponseBody map[string]interface{}
		json.Unmarshal(item, &itemResponseBody)
		// consume itemResponseBody content
	}
}

API Review diff

https://apiview.dev/Assemblies/Review/2bf4306717ca4c90b69d2d5a79e3f8e6/0161e96437934dd7a0bd657e96ab2994?diffRevisionId=471bcfa777274336bb7b4bfe03c41dad&doc=False&diffOnly=True

(Not sure why some of the APIs are marked as diffs, they were just moved to a different place in the new file, the key new APIs are ContainerClient.QueryItems, QueryOptions, QueryItemResponse)

Additional changes

  • CODEOWNERS updated
  • Docs/examples added
  • Updated to Go 1.18, including updating the Emulator tests YML to target 1.18
  • Bumped dependency of azcore to 0.23.0
  • Added unit tests for the new feature and unit tests on older APIs to increase test coverage
  • Added emulator tests for the new feature

@ealsur ealsur self-assigned this Apr 19, 2022
@ealsur ealsur linked an issue Apr 19, 2022 that may be closed by this pull request
@ealsur ealsur added the Cosmos label Apr 20, 2022
@jhendrixMSFT
Copy link
Copy Markdown
Member

@JeffreyRichter do you want to look at the API diff?

@JeffreyRichter
Copy link
Copy Markdown
Member

No, I don't think I have to. I scanned this PR quickly and picked the APIs out.

@ealsur ealsur merged commit d2f6ad6 into Azure:main Apr 22, 2022
@ealsur ealsur deleted the users/ealsur/cosmosquerypk branch April 22, 2022 16:54
@ealsur ealsur mentioned this pull request Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Azcosmos Core/SQL API Query support

5 participants