通过代码启用 Cosmos DB 中的 Mongo DB 聚合管道

4
Azure Cosmos DB现在也支持聚合管道,这使得它成为我们使用的可行替代Mongo DB容器的选择,但我未能找到通过代码启用该功能的方法(如何在门户中执行此操作请参见:https://azure.microsoft.com/en-gb/blog/azure-cosmosdb-extends-support-for-mongodb-aggregation-pipeline-unique-indexes-and-more/)。
我们需要这个功能来创建每天从头开始部署流水线生成的集成和测试环境,支持聚合管道的Cosmos DB实例必须作为后备。
我已经检查了https://learn.microsoft.com/en-us/rest/api/documentdb/的API文档,以及az cosmosdb命令行工具,但我找不到正确的设置传递方式。
这是因为该功能尚未推出,还是我漏掉了什么?

2
目前,此功能仅可通过Azure门户使用。Azure CLI支持启用预览功能的功能即将推出。一旦上线,我将分享文档链接。 - Siddhesh Vethe
@SiddheshVethe 好的,谢谢。这将是一个很棒的东西。 - donmartin
@SiddheshVethe,这个有什么消息吗?不幸的是,这变得很烦人。 - donmartin
2个回答

4

运行此命令后,我遇到了以下问题:参数'resource_group_name'必须符合以下模式:'^[-\w\._\(\)]+$'。 Traceback(最近的调用): File“/usr/local/Cellar/azure-cli/2.0.43/libexec/lib/python3.7/site-packages/knack/cli.py”,第197行,在invoke中 cmd_result = self.invocation.execute(args) File“/usr/local/Cellar/azure-cli/2.0.43/libexec/lib/python3.7/site-packages/azure/cli/core/commands/init.py”,第369行,在execute中 six.reraise(* sys.exc_info()).... - Sudhir
有没有可能用ARM启用这个功能?我尝试了“capabilities”:{“name”:“EnableAggregationPipeline”},但它没有任何作用。 - Dan
您也可以通过 Azure 门户启用此功能。转到 Mongo 预览功能,您会看到选项。 - hui chen

2

不要这样做 - 请查看被接受的答案。

我的一个同事找到了以下临时解决方案,使用了 Azure 的可能未经记录的 API(这是一个 bash 脚本)。传入 LOCATIONRESOURCE_GROUPBM_ACCOUNT,此脚本将创建启用聚合管道的 Mongo API Cosmos DB 帐户。

TOKEN=$(az account get-access-token | jq ".accessToken" | tr -d '"')

if [ -z "$LOCATION" ]; then
    export LOCATION="NorthEurope"
fi
echo "INFO [cosmos]: Using location: $LOCATION"
echo "INFO [cosmos]: Creating bookmarks DB"
BM_ACCOUNT="name-of-your-bookmark-db"
az cosmosdb create --resource-group $RESOURCE_GROUP \
    --name $BM_ACCOUNT \
    --kind MongoDB \
    --locations "$LOCATION=0"

curl -X PATCH \
     -H "Authorization: Bearer ${TOKEN}" \
     -H 'Content-Type: application/json' \
     --data '{"properties":{"capabilities":[{"name":"EnableAggregationPipeline","description":null}]}}' \
     "https://management.azure.com/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.DocumentDb/databaseAccounts/${BM_ACCOUNT}/?api-version=2015-04-08" 

WAIT_FOR=12
SUCCESS=0
while [ $WAIT_FOR -gt 0 ]; do
    sleep 10

    RESULT=$(curl -H "Authorization: Bearer ${TOKEN}" \
                  -H 'Content-Type: application/json' \
                  "https://management.azure.com/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.DocumentDb/databaseAccounts/${BM_ACCOUNT}/?api-version=2015-04-08" \
                  | jq ".properties.capabilities[].name" \
                  | tr -d '"')

    if [ "$RESULT" == "EnableAggregationPipeline" ]; then
        SUCCESS=1
        break;
    fi

    echo "INFO [cosmos]: Waiting another ${WAIT_FOR} tries for 'EnableAggregationPipeline' capability..."
    ((WAIT_FOR--))
done

if [ $SUCCESS -eq 0 ]; then
    echo "ERROR [cosmos]: Did not get required CosmosDB capability of 'EnableAggregationPipeline' in time for account ${BM_ACCOUNT} - giving up." >&2
    exit 1
fi

我可能不建议在生产环境中使用这个,但就我们所看到的情况来看,它确实有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接