php小編西瓜在介紹CosmosDB時(shí)指出,即使值匹配,文檔和標(biāo)頭之間的分區(qū)鍵值不匹配也會(huì)出現(xiàn)錯(cuò)誤。CosmosDB是一種全球分布式數(shù)據(jù)庫服務(wù),它使用分區(qū)鍵將數(shù)據(jù)分布在不同的物理分區(qū)上。分區(qū)鍵是指在寫入文檔時(shí)指定的某個(gè)值,它決定了文檔將被存儲(chǔ)在哪個(gè)分區(qū)中。如果文檔的分區(qū)鍵值與標(biāo)頭中指定的分區(qū)鍵值不匹配,將會(huì)導(dǎo)致錯(cuò)誤的查詢結(jié)果或者查詢失敗。因此,在使用CosmosDB時(shí),我們需要確保文檔的分區(qū)鍵值與標(biāo)頭中指定的分區(qū)鍵值保持一致,以避免出現(xiàn)錯(cuò)誤。
問題內(nèi)容
我正在使用 azure-sdk-for-go 包 azcosmos 在 cosmosdb 容器中創(chuàng)建項(xiàng)目。這是我當(dāng)前收到的錯(cuò)誤:
--------------------------------------------------------------------------------
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"
}
--------------------------------------------------------------------------------
登錄后復(fù)制
我的代碼如下:
// 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)
登錄后復(fù)制
這遵循 azcosmos 示例測(cè)試中創(chuàng)建項(xiàng)目的模式(請(qǐng)參閱examplecontainerclient_createitem 函數(shù))。 azure 門戶中容器分區(qū)鍵的屏幕截圖。我如何繼續(xù)收到此錯(cuò)誤?看來pk值與我相符,也許我錯(cuò)過了一些東西。
解決方法
您的問題已上線:
pk := azcosmos.newpartitionkeystring("/_partitionkey")
登錄后復(fù)制
這與其他問題的情況相同:https://www.php.cn/link/e8c1bdc555e17fd06b44ea4d3b4adbda
容器具有分區(qū)鍵定義/路徑,該分區(qū)鍵定義/路徑是在創(chuàng)建容器期間設(shè)置的,它是將包含分區(qū)鍵值的屬性的 json 路徑。
如果您的路徑是 /_partitionkey 那么:
-
文檔正文中應(yīng)該有一個(gè)名為
_partitionkey 的屬性您的代碼應(yīng)為
pk := azcosmos.newpartitionkeystring("<該屬性的值")
就您而言,您的主體沒有 _partitionkey 屬性:
item := map[string]string{
"id": "1",
"value": "2",
}
登錄后復(fù)制
所以您需要添加它。
或者重新評(píng)估 /_partitionkey 是否確實(shí)是適合您的容器/用例的正確分區(qū)鍵定義/路徑,也許它是不同的。無論是哪一個(gè),“項(xiàng)目”操作都需要值。






