Table of Contents
问题内容
解决方法
Home Backend Development Golang CosmosDB partition key value mismatch between document and header causes error even if values ​​match

CosmosDB partition key value mismatch between document and header causes error even if values ​​match

Feb 11, 2024 pm 10:40 PM
overflow lsp

即使值匹配,文档和标头之间的 CosmosDB 分区键值不匹配也会出现错误

php小编西瓜在介绍CosmosDB时指出,即使值匹配,文档和标头之间的分区键值不匹配也会出现错误。CosmosDB是一种全球分布式数据库服务,它使用分区键将数据分布在不同的物理分区上。分区键是指在写入文档时指定的某个值,它决定了文档将被存储在哪个分区中。如果文档的分区键值与标头中指定的分区键值不匹配,将会导致错误的查询结果或者查询失败。因此,在使用CosmosDB时,我们需要确保文档的分区键值与标头中指定的分区键值保持一致,以避免出现错误。

问题内容

我正在使用 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"
}
--------------------------------------------------------------------------------
Copy after login

我的代码如下:

// 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)
Copy after login

这遵循 azcosmos 示例测试中创建项目的模式(请参阅examplecontainerclient_createitem 函数)。 azure 门户中容器分区键的屏幕截图。我如何继续收到此错误?看来pk值与我相符,也许我错过了一些东西。

解决方法

您的问题已上线:

pk := azcosmos.newpartitionkeystring("/_partitionkey")
Copy after login

这与其他问题的情况相同:https://www.php.cn/link/e8c1bdc555e17fd06b44ea4d3b4adbda

容器具有分区键定义/路径,该分区键定义/路径是在创建容器期间设置的,它是将包含分区键值的属性的 json 路径。

如果您的路径是 /_partitionkey 那么:

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

就您而言,您的主体没有 _partitionkey 属性:

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

所以您需要添加它。

或者重新评估 /_partitionkey 是否确实是适合您的容器/用例的正确分区键定义/路径,也许它是不同的。无论是哪一个,“项目”操作都需要

The above is the detailed content of CosmosDB partition key value mismatch between document and header causes error even if values ​​match. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

How to convert xml into image file How to convert xml into image file Apr 03, 2025 am 08:27 AM

There are many ways to convert XML into image files: using software: Altova XMLSpy, Oxygen XML Editor, XML Viewer, etc. Use online tools: XML to Image, Convert XML to Image, Free Online XML to Image Converter, etc.

How to control the top and end of pages in browser printing settings through JavaScript or CSS? How to control the top and end of pages in browser printing settings through JavaScript or CSS? Apr 05, 2025 pm 10:39 PM

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

Why are the inline-block elements misaligned? How to solve this problem? Why are the inline-block elements misaligned? How to solve this problem? Apr 04, 2025 pm 10:39 PM

Regarding the reasons and solutions for misaligned display of inline-block elements. When writing web page layout, we often encounter some seemingly strange display problems. Compare...

How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? Apr 04, 2025 pm 11:45 PM

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...

How to customize the resize symbol through CSS and make it uniform with the background color? How to customize the resize symbol through CSS and make it uniform with the background color? Apr 05, 2025 pm 02:30 PM

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

How to open xml format How to open xml format Apr 02, 2025 pm 09:00 PM

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

How to convert xml to json How to convert xml to json Apr 03, 2025 am 09:09 AM

Methods to convert XML to JSON include: writing scripts or programs in programming languages ​​(such as Python, Java, C#) to convert; pasting or uploading XML data using online tools (such as XML to JSON, Gojko's XML converter, XML online tools) and selecting JSON format output; performing conversion tasks using XML to JSON converters (such as Oxygen XML Editor, Stylus Studio, Altova XMLSpy); converting XML to JSON using XSLT stylesheets; using data integration tools (such as Informatic

See all articles