在Azure Cosmos DB中,无法将分区键标头中的项目替换为中文单词。

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

Unable to replace item with chinese word in partition key header in Azure Cosmos DB

问题

我正在尝试使用golang和github.com/vippsas/go-cosmosdb包在Azure Cosmos DB中进行CURD操作。

除了在x-ms-documentdb-partitionkey中使用中文字符来创建和替换文档之外,其他都正常工作。

文档示例数据,分区键为/method:

{
  "id": "9289b3f5-5830-4589-ab7d-6c0efbb4150e",
  "method": "日",
  "endpoint": "/api/savecsv?operator=jimmylin",
  "operator": "jimmy",
  "execTime": 1622867229,
  "payload": "abc.json"
}

这是我的代码:

client := cosmosapi.New()
_, _, err = client.CreateDocument(ctx, "dbid", "contid", &log, cosmosapi.CreateDocumentOptions{PartitionKeyValue: log.Method})
if err != nil {
    err = tracerr.New(err.Error())
    tracerr.PrintSourceColor(err, 2)
}

我还打印出了在/vippsas/go-cosmosdb包中处理的分区键,输出结果为:

x-ms-documentdb-partitionkey:["日"]

这是我遇到的错误:

The JSON, SQL, or JavaScript in the request body is invalid

如果我将/method更改为英文,就可以正常工作。

如果我使用JS SDK也可以正常工作(中文字符分区键)。

我认为无论是JS SDK还是我使用的这个包都是基于Azure Cosmos DB的RESTful API。我认为这个包可能缺少一些东西。

英文:

I'm trying to use golang to do CURD operation in Azure Cosmos db using github.com/vippsas/go-cosmosdb package.

Everything works fine except trying to Create、Replace documents with chinese character in the x-ms-documentdb-partitionkey.

Document sample data, partition key is /method

{
 "id": "9289b3f5-5830-4589-ab7d-6c0efbb4150e",
 "method": "日",
 "endpoint": "/api/savecsv?operator=jimmylin",
 "operator": "jimmy",
 "execTime": 1622867229,
 "payload": "abc.json"
}

Here is my code:

    client := cosmosapi.New()
	_, _, err = client.CreateDocument(ctx, "dbid", "contid", &log, cosmosapi.CreateDocumentOptions{PartitionKeyValue: log.Method})
	if err != nil {
		err = tracerr.New(err.Error())
		tracerr.PrintSourceColor(err, 2)
	}

I also printed out the partition key that is handled in /vippsas/go-cosmosdb package. the output is:

x-ms-documentdb-partitionkey:["日"]

Here is error I have:

The JSON, SQL, or JavaScript in the request body is invalid

If I change /method to English, It works just fine.

If I use JS SDKs works fine as well(chinese character partition key).

I believe either JS SDKs or the package I use are all based on Azure Cosmos DB Restful APIs.
I think this package is likely to be missing something.

答案1

得分: 0

Azure Cosmos DB仅在x-ms-documentdb-partitionkey中支持Unicode或ASCII,而github.com/vippsas/go-cosmosdb包使用的json.Marshal会自动将Unicode转换为中文字符。

解决此问题的唯一方法是在创建文档时使用英文作为分区键。

英文:

Azure Cosmos db is only supporting Unicode or ASCII in x-ms-documentdb-partitionkey while github.com/vippsas/go-cosmosdb package is using json.Marshal which internally transforms Unicode to Chinese characters automatically.

The only way to solve it is using English as partition key when creating documents.

huangapple
  • 本文由 发表于 2021年6月5日 10:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/67845767.html
匿名

发表评论

匿名网友

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

确定