Search code examples
pythonazure-cosmosdbazure-cosmosdb-mongoapidata-ingestion

Azure Cosmos DB CSV upload


I am opening a CSV file in Python in Pycharm, then I want to upload it to my Container in Cosmos DB. It's not working.

if os.path.exists(csv_file):
    with codecs.open(csv_file, 'rb', encoding="utf-8") as csv:
        csv_reader = DictReader(csv)
        for row in csv_reader:
            upsert_item(row)

Can I just open a CSV file, then upload it to CosmosDB like the above and below? It's not working.

def upsert_item(row):
     container.upsert_item(row)

Solution

  • It's failing because Cosmos DB expects the data contained in your row variable to be in JSON format.

    If you're just uploading a small amount of data in CSV format, the easiest way is to use the Data Migration Tool.

    You learn more about this tool and download it from here, Tutorial: Use Data migration tool to migrate your data to Azure Cosmos DB