如何将JSON导入Cosmos DB

时间:2018-01-25 18:21:27

标签: javascript azure azure-cosmosdb

我有一个包含多个数据库的现有CouchDB实例。使用Postman,我将10个文档导出到.json文件。使用DocumentDB数据迁移工具我能够将此文件的内容导入Cosmos。

但是,我似乎已经有效地导入了一个文档。 Azure Data Explorer表示我的"集合"。

中有一个单独的文档

在CouchDB中,最高级别的标识符是doc._id。我已经尝试指定" doc._id"," id"和" _id"作为我的身份证领域,一切都无济于事。我究竟做错了什么?如何单独导入每个JSON文档?

示例文件:

{
    "_id": "3147cb0e74449e1c28c6ded2b4a3fa45e0d65481-bd_RXMARTINEZ@emailaddress.com_2017-11-30T13:38:33.955Z", 
    "_rev": "3-99aef1458fe1a8f310c83156b9d06a69", 
    "delivery": {
        "application": "EnvTest", 
        "sender": {
            "id": "RXMARTINEZ@emailaddress.com", 
            "type": "user"
        }, 
        "recipients": [
            {
                "type": "email", 
                "recipient": "\"Artzer, Daniel J\" <DJArtzer@emailaddress.com>", 
                "sentTS": "2018-01-30T19:46:31.515Z", 
                "id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"
            }
        ]
    }, 
    "payload": {
        "startTS": "2017-11-30T07:38:33-06:00", 
        "equipmentOrLineType": "gathering system", 
        "scheduledIndicator": "upset", 
        "field": "Gulf Plains", 
        "eventDateTime": "2017-11-30T07:31:39-06:00", 
        "numCopies": "1", 
        "region": "South", 
        "facility": "", 
        "equipment": "", 
        "causeForRelease": "", 
        "correctiveAction": "", 
        "hgbUsed": "no-hgb-available", 
        "esdActivated": "no", 
        "submitTS": "2017-11-30T13:38:33.940Z", 
        "lineSegment": [
            {
                "lineName": "GPL-1", 
                "Diameter": "12.725", 
                "Length": "1000", 
                "duration": "no", 
                "volume": "3"
            }
        ], 
        "creator": "RXMARTINEZ@emailaddress.com", 
        "savedTS": "2017-11-30T13:38:33.955Z", 
        "trainingDoc": true, 
        "_id": "bd_RXMARTINEZ@emailaddress.com_2017-11-30T13:38:33.955Z", 
        "syncTS": "2017-11-30T13:38:34.794Z"
    }
}

2 个答案:

答案 0 :(得分:1)

我使用DocumentDB数据迁移工具导入一个包含4个文档的.json文件,对我来说效果很好。

我的.json文件:

[
    {
        "name":"jay1",
        "age":20
    },
    {
        "name":"jay2",
        "age":20
    },
    {
        "name":"jay3",
        "age":20
    },
    {
        "name":"jay4",
        "age":20
    }
]

文件将分别导入集合中。

enter image description here

  

JSON文件源导入程序选项允许您导入一个或多个   单个文档JSON文件或JSON文件,每个文件包含一个数组   JSON文档。添加包含要导入的JSON文件的文件夹时   您可以选择递归搜索子文件夹中的文件。

您可以检查是否遵循了official tutorial中所述的上述规则来创建.json文件。 (例如,您的文档是否在数组中?)

希望它对你有所帮助。

更新答案:

我厌倦了导入数据,这些数据遵循你在回复cosmos db。中提到的格式。

我的更新.json文件:

[
    {
        "id":"001",
        "name":"jay1",
        "age":20,
        "array": {
            "a":1,
            "b":2,
            "c":{
                "d":3,
                "e": {
                    "f":4
                }
            }
        }
    },
    {
        "id":"002",
        "name":"jay2",
        "age":20,
        "array": {
            "a":1,
            "b":2,
            "c":{
                "d":3,
                "e": {
                    "f":4
                }
            }
        }
    },
    {
        "id":"003",
        "name":"jay3",
        "age":20,
        "array": {
            "a":1,
            "b":2,
            "c":{
                "d":3,
                "e": {
                    "f":4
                }
            }
        }
    }
]

并且文档已成功导入集合中。

enter image description here

更新答案2:

我按照您提供的文档格式,在我的.json文件中创建了3个示例文档。(我只是将'_id'属性更新为'testX')

我的.json文件:

[
    {
        "_id": "test1", 
        "_rev": "3-99aef1458fe1a8f310c83156b9d06a69", 
        "delivery": {
            "application": "EnvTest", 
            "sender": {
                "id": "RXMARTINEZ@emailaddress.com", 
                "type": "user"
            }, 
            "recipients": [
                {
                    "type": "email", 
                    "recipient": "\"Artzer, Daniel J\" <DJArtzer@emailaddress.com>", 
                    "sentTS": "2018-01-30T19:46:31.515Z", 
                    "id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"
                }
            ]
        }, 
        "payload": {
            "startTS": "2017-11-30T07:38:33-06:00", 
            "equipmentOrLineType": "gathering system", 
            "scheduledIndicator": "upset", 
            "field": "Gulf Plains", 
            "eventDateTime": "2017-11-30T07:31:39-06:00", 
            "numCopies": "1", 
            "region": "South", 
            "facility": "", 
            "equipment": "", 
            "causeForRelease": "", 
            "correctiveAction": "", 
            "hgbUsed": "no-hgb-available", 
            "esdActivated": "no", 
            "submitTS": "2017-11-30T13:38:33.940Z", 
            "lineSegment": [
                {
                    "lineName": "GPL-1", 
                    "Diameter": "12.725", 
                    "Length": "1000", 
                    "duration": "no", 
                    "volume": "3"
                }
            ], 
            "creator": "RXMARTINEZ@emailaddress.com", 
            "savedTS": "2017-11-30T13:38:33.955Z", 
            "trainingDoc": true, 
            "_id": "bd_RXMARTINEZ@emailaddress.com_2017-11-30T13:38:33.955Z", 
            "syncTS": "2017-11-30T13:38:34.794Z"
        }
    },
    {
        "_id": "test2", 
        "_rev": "3-99aef1458fe1a8f310c83156b9d06a69", 
        "delivery": {
            "application": "EnvTest", 
            "sender": {
                "id": "RXMARTINEZ@emailaddress.com", 
                "type": "user"
            }, 
            "recipients": [
                {
                    "type": "email", 
                    "recipient": "\"Artzer, Daniel J\" <DJArtzer@emailaddress.com>", 
                    "sentTS": "2018-01-30T19:46:31.515Z", 
                    "id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"
                }
            ]
        }, 
        "payload": {
            "startTS": "2017-11-30T07:38:33-06:00", 
            "equipmentOrLineType": "gathering system", 
            "scheduledIndicator": "upset", 
            "field": "Gulf Plains", 
            "eventDateTime": "2017-11-30T07:31:39-06:00", 
            "numCopies": "1", 
            "region": "South", 
            "facility": "", 
            "equipment": "", 
            "causeForRelease": "", 
            "correctiveAction": "", 
            "hgbUsed": "no-hgb-available", 
            "esdActivated": "no", 
            "submitTS": "2017-11-30T13:38:33.940Z", 
            "lineSegment": [
                {
                    "lineName": "GPL-1", 
                    "Diameter": "12.725", 
                    "Length": "1000", 
                    "duration": "no", 
                    "volume": "3"
                }
            ], 
            "creator": "RXMARTINEZ@emailaddress.com", 
            "savedTS": "2017-11-30T13:38:33.955Z", 
            "trainingDoc": true, 
            "_id": "bd_RXMARTINEZ@emailaddress.com_2017-11-30T13:38:33.955Z", 
            "syncTS": "2017-11-30T13:38:34.794Z"
        }
    },
    {
        "_id": "test3", 
        "_rev": "3-99aef1458fe1a8f310c83156b9d06a69", 
        "delivery": {
            "application": "EnvTest", 
            "sender": {
                "id": "RXMARTINEZ@emailaddress.com", 
                "type": "user"
            }, 
            "recipients": [
                {
                    "type": "email", 
                    "recipient": "\"Artzer, Daniel J\" <DJArtzer@emailaddress.com>", 
                    "sentTS": "2018-01-30T19:46:31.515Z", 
                    "id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"
                }
            ]
        }, 
        "payload": {
            "startTS": "2017-11-30T07:38:33-06:00", 
            "equipmentOrLineType": "gathering system", 
            "scheduledIndicator": "upset", 
            "field": "Gulf Plains", 
            "eventDateTime": "2017-11-30T07:31:39-06:00", 
            "numCopies": "1", 
            "region": "South", 
            "facility": "", 
            "equipment": "", 
            "causeForRelease": "", 
            "correctiveAction": "", 
            "hgbUsed": "no-hgb-available", 
            "esdActivated": "no", 
            "submitTS": "2017-11-30T13:38:33.940Z", 
            "lineSegment": [
                {
                    "lineName": "GPL-1", 
                    "Diameter": "12.725", 
                    "Length": "1000", 
                    "duration": "no", 
                    "volume": "3"
                }
            ], 
            "creator": "RXMARTINEZ@emailaddress.com", 
            "savedTS": "2017-11-30T13:38:33.955Z", 
            "trainingDoc": true, 
            "_id": "bd_RXMARTINEZ@emailaddress.com_2017-11-30T13:38:33.955Z", 
            "syncTS": "2017-11-30T13:38:34.794Z"
        }
    }
]

将文档导入数据库并且没有发生错误。

enter image description here

如果你和我之间有任何分歧,请告诉我。

答案 1 :(得分:0)

另一个答案大约是一年前,它使用dtui.exe

dtui.exe是一个GUI工具
dt.exe是命令行工具

两个都可以从https://www.microsoft.com/en-us/download/details.aspx?id=46436下载到今天

以下解决方案使用 dt.exe (使您的流程自动化,以便其他人可以重复该流程,以后也不会出现问题)

"C:\path-to-your-dt-exe\dt.exe" /s:JsonFile /s.Files:"C:\path-to-your-json-files-of-one-collection-only\*.json" /t:DocumentDB /t.ConnectionString:"AccountEndpoint=https://localhost:8081/;AccountKey=<big account key here>;Database=<your db name here>" /t.Collection:<your collection name here> /t.PartitionKey:<your partition key here> /t.CollectionThroughput:2500

注意
-上面的数据库(<your db name here>)和集合(<your collection name here>)必须已经存在
-将<...>替换为上面的数据,然后在cmd中运行。
-/s是源,/t是目标,这里源是JSON文件,目标是本地documentBD

希望有帮助。