将CSV文件导入Cosmos DB Table Api

时间:2018-02-23 06:34:41

标签: azure-cosmosdb

我需要批量导入将100条记录写入Cosmos DB。

我发现dt.exe,但没有帮助。使用表api将csv导入cosmos db时会抛出错误。

我无法找到任何可靠的方法来自动执行此过程。

1 个答案:

答案 0 :(得分:0)

  

命令行Azure Cosmos DB数据迁移工具(dt.exe)可以   用于将现有Azure表存储数据导入Table API   GA帐户,或将表API(预览)帐户中的数据迁移到   表API GA帐户。目前不支持其他来源。该   目前不支持基于UI的数据迁移工具(dtui.exe)   表API帐户。

根据以上official statement,似乎不支持将其他来源(例如csv文件)迁移到Azure Table API帐户。您可以采用一种解决方法:在程序中读取csv文件,然后将数据导入Azure表存储。

请参阅我在此thread中所做的示例python代码。

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
import csv
import sys
import codecs

table_service = TableService(connection_string='***')

reload(sys)
sys.setdefaultencoding('utf-8')
filename = "E:/jay.csv"

with codecs.open(filename, 'rb', encoding="utf-8") as f_input:
    csv_reader = csv.reader(f_input)
    for row in csv_reader:
        task = Entity()
        task.PartitionKey = row[0]
        task.RowKey = row[1]
        task.description = row[2]
        task.priority = EntityProperty(EdmType.INT32, row[3])
        task.logtime = EntityProperty(EdmType.DATETIME, row[4])
        table_service.insert_entity('tasktable', task)

或者您可以提交反馈here

希望它对你有所帮助。

仅用于小更新:

如果您使用python 3.1,则reload(sys)sys.setdefaultencoding('utf-8')不需要'r' filename = r"E:/jay.csv"