How to populate a CosmosDB collection by command line?

Viewed 623

I am developing a set of Scripts for Azure and I would like to know how to populate a CosmosDB collection with az.

Currently, I know how to create a Database and Collection but how to initialize the Database?

  az cosmosdb create \
    --resource-group $RESOURCE_GROUP \
    --name $COSMOS_DB_ACCOUNT
  az cosmosdb database create \
    --resource-group-name $RESOURCE_GROUP \
    --name $COSMOS_DB_ACCOUNT \
    --db-name $COSMOS_DB_NAME
  az cosmosdb collection create \
    --resource-group-name $RESOURCE_GROUP \
    --name $COSMOS_DB_ACCOUNT \
    --collection-name $COSMOS_DB_COLLECTION_NAME \
    --db-name $COSMOS_DB_NAME \
    --partition-key-path $COSMOS_DB_COLLECTION_PARTITION_KEY

Reading the documentation, I didn´t see a solution.

1 Answers

az doesn't provide any data-movement options for Cosmos DB.

For the SQL API, you'll either need to create your own command-line tool, or use the Cosmos DB-supplied Data Migration Tool (Windows-only, unlike az), which provides a command-line interface. For example:

dt /s:JsonFile /s.Files:.\inputdata.json /t:DocumentDBBulk /t.ConnectionString:"AccountEndpoint=<name>;AccountKey=<key>;Database=<db>;" /t.Collection:<collname> /t.CollectionThroughput:<throughput>

This has support for the MongoDB API as well, but you can also use native command-line tools such as mongoimport.

Related