文档和标头之间存在不匹配的CosmosDB分区键值错误,尽管这些值是匹配的。

huangapple go评论118阅读模式
英文:

Error with mismatched CosmosDB partition key values between the document and header even though the values match

问题

我正在使用Azure-SDK-for-Go包中的azcosmos来在CosmosDB容器中创建一个项目。这是我目前遇到的错误:

--------------------------------------------------------------------------------
RESPONSE 400: 400 Bad Request
ERROR CODE: BadRequest
--------------------------------------------------------------------------------
{
  "code": "BadRequest",
  "message": "Message: {\"Errors\":[\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more: https://aka.ms/CosmosDB/sql/errors/wrong-pk-value\"]}\r\nActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, Request URI: /apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, RequestStats: \r\nRequestStartTime: 2023-02-23T20:53:15.4424439Z, RequestEndTime: 2023-02-23T20:53:15.4424439Z,  Number of regions attempted:1\r\n{\"systemHistory\":[{\"dateUtc\":\"2023-02-23T20:52:06.4715437Z\",\"cpu\":1.012,\"memory\":479419988.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0224,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:16.4816322Z\",\"cpu\":2.342,\"memory\":480026956.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0193,\"availableThreads\":32761,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:26.4918299Z\",\"cpu\":1.534,\"memory\":480000572.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0158,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":437},{\"dateUtc\":\"2023-02-23T20:52:36.5019603Z\",\"cpu\":1.490,\"memory\":480000576.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0161,\"availableThreads\":32737,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:52:46.5121122Z\",\"cpu\":1.306,\"memory\":479989504.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0204,\"availableThreads\":32762,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:53:06.5323276Z\",\"cpu\":1.561,\"memory\":479914676.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0113,\"availableThreads\":32763,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":424}]}\r\nRequestStart: 2023-02-23T20:53:15.4424439Z; ResponseTime: 2023-02-23T20:53:15.4424439Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westus1-fd44.documents.azure.com:14323/apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, LSN: 
9427, GlobalCommittedLsn: 9427, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 1001, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#9427, UsingLocalLSN: False, TransportException: null, BELatencyMs: 1.004, ActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, RetryAfterInMs: , 
TransportRequestTimeline: {\"requestTimeline\":[{\"event\": \"Created\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424439Z\", \"durationInMs\": 0.0115},{\"event\": \"ChannelAcquisitionStarted\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424554Z\", \"durationInMs\": 0.0114},{\"event\": \"Pipelined\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424668Z\", \"durationInMs\": 0.1556},{\"event\": \"Transit Time\", \"startTimeUtc\": \"2023-02-23T20:53:15.4426224Z\", \"durationInMs\": 1.8731},{\"event\": \"Received\", \"startTimeUtc\": \"2023-02-23T20:53:15.4444955Z\", \"durationInMs\": 0.1408},{\"event\": \"Completed\", \"startTimeUtc\": \"2023-02-23T20:53:15.4446363Z\", \"durationInMs\": 0}],\"serviceEndpointStats\":{\"inflightRequests\":1,\"openConnections\":1},\"connectionStats\":{\"waitforConnectionInit\":\"False\",\"callsPendingReceive\":0,\"lastSendAttempt\":\"2023-02-23T20:35:31.6351618Z\",\"lastSend\":\"2023-02-23T20:35:31.6351618Z\",\"lastReceive\":\"2023-02-23T20:35:31.6351618Z\"},\"requestSizeInBytes\":551,\"requestBodySizeInBytes\":26,\"responseMetadataSizeInBytes\":186,\"responseBodySizeInBytes\":166};\r\n ResourceType: Document, OperationType: Create\r\n, SDK: Microsoft.Azure.Documents.Common/2.14.0"
}
--------------------------------------------------------------------------------

我的代码如下:

// 加载.env文件
err := godotenv.Load()
handle(err)

// 创建CosmosDB凭据
endpoint := os.Getenv("AZURE_COSMOS_ENDPOINT")
key := os.Getenv("AZURE_COSMOS_KEY")
cred, err := azcosmos.NewKeyCredential(key)
handle(err)

// 创建CosmosDB客户端
client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
handle(err)
log.Println("CosmosDB客户端已成功创建...")

// 创建Container实例以执行读写操作
container, err := client.NewContainer("vaporwave", "employees")
handle(err)
log.Println("容器已成功创建...")

// 生成PartitionKey和示例项目
pk := azcosmos.NewPartitionKeyString("/_partitionKey")
item := map[string]string{
		"id":	"1",
		"value": "2",
}
marshalled, err := json.Marshal(item)
handle(err)

// 创建容器项目
itemResponse, err := container.CreateItem(context.Background(), pk, marshalled, nil)
if err != nil {
	var responseErr *azcore.ResponseError
	errors.As(err, &responseErr)
	log.Fatal(responseErr)
}
log.Printf("项目已创建。ActivityId %s 消耗 %v RU\n", itemResponse.ActivityID, itemResponse.RequestCharge)

这遵循了在azcosmos示例测试中创建项目的模式(请参见ExampleContainerClient_CreateItem函数)。从Azure门户的容器的分区键的屏幕截图。为什么我还在收到这个错误?在我看来,PK值是匹配的,也许我漏掉了什么。

英文:

I am using the Azure-SDK-for-Go package azcosmos to create an item in a CosmosDB container. This is the error I currently get:

--------------------------------------------------------------------------------
RESPONSE 400: 400 Bad Request
ERROR CODE: BadRequest
--------------------------------------------------------------------------------
{
  "code": "BadRequest",
  "message": "Message: {\"Errors\":[\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more: https:\\/\\/aka.ms\\/CosmosDB\\/sql\\/errors\\/wrong-pk-value\"]}\r\nActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, Request URI: /apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, RequestStats: \r\nRequestStartTime: 2023-02-23T20:53:15.4424439Z, RequestEndTime: 2023-02-23T20:53:15.4424439Z,  Number of regions attempted:1\r\n{\"systemHistory\":[{\"dateUtc\":\"2023-02-23T20:52:06.4715437Z\",\"cpu\":1.012,\"memory\":479419988.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0224,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:16.4816322Z\",\"cpu\":2.342,\"memory\":480026956.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0193,\"availableThreads\":32761,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:26.4918299Z\",\"cpu\":1.534,\"memory\":480000572.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0158,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":437},{\"dateUtc\":\"2023-02-23T20:52:36.5019603Z\",\"cpu\":1.490,\"memory\":480000576.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0161,\"availableThreads\":32737,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:52:46.5121122Z\",\"cpu\":1.306,\"memory\":479989504.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0204,\"availableThreads\":32762,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:53:06.5323276Z\",\"cpu\":1.561,\"memory\":479914676.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0113,\"availableThreads\":32763,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":424}]}\r\nRequestStart: 2023-02-23T20:53:15.4424439Z; ResponseTime: 2023-02-23T20:53:15.4424439Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westus1-fd44.documents.azure.com:14323/apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, LSN: 
9427, GlobalCommittedLsn: 9427, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 1001, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#9427, UsingLocalLSN: False, TransportException: null, BELatencyMs: 1.004, ActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, RetryAfterInMs: , 
TransportRequestTimeline: {\"requestTimeline\":[{\"event\": \"Created\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424439Z\", \"durationInMs\": 0.0115},{\"event\": \"ChannelAcquisitionStarted\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424554Z\", \"durationInMs\": 0.0114},{\"event\": \"Pipelined\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424668Z\", \"durationInMs\": 0.1556},{\"event\": \"Transit Time\", \"startTimeUtc\": \"2023-02-23T20:53:15.4426224Z\", \"durationInMs\": 1.8731},{\"event\": \"Received\", \"startTimeUtc\": \"2023-02-23T20:53:15.4444955Z\", \"durationInMs\": 0.1408},{\"event\": \"Completed\", \"startTimeUtc\": \"2023-02-23T20:53:15.4446363Z\", \"durationInMs\": 0}],\"serviceEndpointStats\":{\"inflightRequests\":1,\"openConnections\":1},\"connectionStats\":{\"waitforConnectionInit\":\"False\",\"callsPendingReceive\":0,\"lastSendAttempt\":\"2023-02-23T20:35:31.6351618Z\",\"lastSend\":\"2023-02-23T20:35:31.6351618Z\",\"lastReceive\":\"2023-02-23T20:35:31.6351618Z\"},\"requestSizeInBytes\":551,\"requestBodySizeInBytes\":26,\"responseMetadataSizeInBytes\":186,\"responseBodySizeInBytes\":166};\r\n ResourceType: Document, OperationType: Create\r\n, SDK: Microsoft.Azure.Documents.Common/2.14.0"
}
--------------------------------------------------------------------------------

My code is as follows:

// load .env
err := godotenv.Load()
handle(err)

// create CosmosDB credentials
endpoint := os.Getenv("AZURE_COSMOS_ENDPOINT")
key := os.Getenv("AZURE_COSMOS_KEY")
cred, err := azcosmos.NewKeyCredential(key)
handle(err)

// create CosmosDB client
client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
handle(err)
log.Println("CosmosDB client has been successfully created...")

// create Container instance to perform read-write operations
container, err := client.NewContainer("vaporwave", "employees")
handle(err)
log.Println("Container has been successfully created...")

// generate a PartitionKey and example item
pk := azcosmos.NewPartitionKeyString("/_partitionKey")
item := map[string]string{
		"id":	"1",
		"value": "2",
}
marshalled, err := json.Marshal(item)
handle(err)

// create container item
itemResponse, err := container.CreateItem(context.Background(), pk, marshalled, nil)
if err != nil {
	var responseErr *azcore.ResponseError
	errors.As(err, &responseErr)
	log.Fatal(responseErr)
}
log.Printf("Item created. ActivityId %s consuming %v RU\n", itemResponse.ActivityID, itemResponse.RequestCharge)

This follows the pattern of creating an item in the azcosmos example test (see the ExampleContainerClient_CreateItem function). Screenshot of the container's partition key from the Azure portal. How am I continuing to get this error? It seems the PK values match to me, maybe I'm missing something.

答案1

得分: 0

您的问题出在以下代码行:

pk := azcosmos.NewPartitionKeyString("/_partitionKey")

这与另一个问题的情况相同:https://stackoverflow.com/a/75536240/5641598

容器具有分区键定义/路径,在创建容器时设置,它是指向包含分区键值的属性的 JSON 路径。

如果您的路径是 /_partitionKey,那么:

  1. 文档正文中应该有一个名为 _partitionKey 的属性。
  2. 您的代码应该是 pk := azcosmos.NewPartitionKeyString("<该属性的值>")

在您的情况下,您的正文中没有 _partitionKey 属性:

item := map[string]string{
        "id":   "1",
        "value": "2",
}

所以您需要添加它。

或者重新评估 /_partitionKey 是否真的是您容器/用例的正确分区键定义/路径,也许它是另一个不同的路径。无论哪种情况,"item" 操作都需要

英文:

Your problem is on line:

pk := azcosmos.NewPartitionKeyString(&quot;/_partitionKey&quot;)

This is the same scenario as this other question: https://stackoverflow.com/a/75536240/5641598

A container has a Partition Key Definition/Path, which is set during the creation of the container and it's the JSON path to the property that will contain the Partition Key Value.

If your path is /_partitionKey then:

  1. There should be a property in the document's body named _partitionKey
  2. Your code should be pk := azcosmos.NewPartitionKeyString(&quot;&lt;the value of that property&quot;)

In your case, your body has no _partitionKey property:

item := map[string]string{
&quot;id&quot;:   &quot;1&quot;,
&quot;value&quot;: &quot;2&quot;,
}

So you would need to add it.

Or re-evaluate if /_partitionKey is really the right Partition Key Definition/Path for your container/use case, maybe it is a different one. Whichever it is, the "item" operations, require the value.

huangapple
  • 本文由 发表于 2023年2月24日 05:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定