【问题标题】:Import CSV file into Cosmos DB Table Api将 CSV 文件导入 Cosmos DB 表 Api
【发布时间】:2018-02-27 11:22:10
【问题描述】:

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

我找到了dt.exe,但没有帮助。使用 table api 将 csv 导入 cosmos db 时会引发错误。

我找不到任何可靠的方法来自动化这个过程。

【问题讨论】:

    标签: azure-cosmosdb


    【解决方案1】:

    命令行 Azure Cosmos DB 数据迁移工具 (dt.exe) 可以 用于将现有 Azure 表存储数据导入表 API GA 帐户,或将数据从 Table 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"

    【讨论】:

    • @SathishNaga 好的,等待您的回复。
    • 谢谢,这行得通,对 python 3 1 脚本的小更新,不需要reload(sys) and sys.setdefaultencoding('utf-8') 2,在文件路径前加上'r' filename = r"E:/jay.csv"}
    • @SathishNaga 感谢您的小更新。我已经在我的答案中添加了它。
    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2014-08-04
    相关资源
    最近更新 更多