如何在将数据从 SQL 数据库迁移到 Cosmos DB 时避免转义字符
How to avoid escape character in Migrating the data from SQLDatabase to Cosmos DB
我想把整个SQL数据库迁移到CosmosDB.in这个过程
SQL table 列之一的序列化数据如下
[{"Id":"1","Type":"Phone","HeaderLabel":"HQ - Main Line","ContactNumber":"+9122222222"}]
序列化数据代表一个Class
public class ContactNumber
{
public string ContactNumberId { get; set; }
public string Type { get; set; }
public string HeaderLabel { get; set; }
public string ContactNumber { get; set; }
}
在 sql 中保存数据时,我必须为 class 执行序列化和反序列化,这是必需的。
public string _ContactNumbers { get; set; }
public List<ContactNumber> ContactNumbers
{
get { return _ContactNumbers == null ? null : JsonConvert.DeserializeObject<List<ContactNumber>>(_ContactNumbers); }
set { _ContactNumbers = value == null ? null : JsonConvert.SerializeObject(value); }
}
使用迁移工具后更新如下
"ContactNumbers":"[{\"Id\":\"1\",\"Type\":\"Phone\",\"HeaderLabel\":\"HQ - Main Line\",\"ContactNumber\":\"+9122222222\"}]"
class 保持不变。从 cosmos DB 获取数据时,我没有执行任何序列化和反序列化。
public List<ContactNumber> ContactNumbers
获取数据时抛出错误
Error converting value "[{"Id":"1","Type":"Phone","HeaderLabel":"HQ - Main Line","ContactNumber":"+9122222222"}]" to type 'System.Collections.Generic.List`1[CosmosDB.Models.ContactNumber]'. Path 'ContactNumber', line 1, position 2411.
错误是因为迁移后字符串中添加了额外的\字符。
我不想在 cosmos DB 中对 class 进行序列化和反序列化,因为没有必要这样做。
那么如何在将数据从 SQL 数据库迁移到 Cosmos DB 文档时避免额外的 \?
您可以使用 Azure Function Cosmos DB Trigger 来处理创建的每个文档。请参考我的功能代码:
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json.Linq;
namespace ProcessJson
{
public class Class1
{
[FunctionName("DocumentUpdates")]
public static void Run(
[CosmosDBTrigger(databaseName:"db",collectionName: "item", ConnectionStringSetting = "CosmosDBConnection",LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)]
IReadOnlyList<Document> documents,
TraceWriter log)
{
log.Verbose("Start.........");
String endpointUrl = "https://***.documents.azure.com:443/";
String authorizationKey = "***";
String databaseId = "db";
String collectionId = "import";
DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
for (int i = 0; i < documents.Count; i++)
{
Document doc = documents[i];
if((doc.alreadyFormat == Undefined.Value) ||(!doc.alreadyFormat)){
String info = doc.GetPropertyValue<String>("info");
JArray o = JArray.Parse(info);
doc.SetPropertyValue("info", o);
doc.SetPropertyValue("alreadyFormat", true);
client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, doc.Id), doc);
log.Verbose("Update document Id " + doc.Id);
}
}
}
}
}
另外请参考案例:
希望对你有帮助。
我想把整个SQL数据库迁移到CosmosDB.in这个过程 SQL table 列之一的序列化数据如下
[{"Id":"1","Type":"Phone","HeaderLabel":"HQ - Main Line","ContactNumber":"+9122222222"}]
序列化数据代表一个Class
public class ContactNumber
{
public string ContactNumberId { get; set; }
public string Type { get; set; }
public string HeaderLabel { get; set; }
public string ContactNumber { get; set; }
}
在 sql 中保存数据时,我必须为 class 执行序列化和反序列化,这是必需的。
public string _ContactNumbers { get; set; }
public List<ContactNumber> ContactNumbers
{
get { return _ContactNumbers == null ? null : JsonConvert.DeserializeObject<List<ContactNumber>>(_ContactNumbers); }
set { _ContactNumbers = value == null ? null : JsonConvert.SerializeObject(value); }
}
使用迁移工具后更新如下
"ContactNumbers":"[{\"Id\":\"1\",\"Type\":\"Phone\",\"HeaderLabel\":\"HQ - Main Line\",\"ContactNumber\":\"+9122222222\"}]"
class 保持不变。从 cosmos DB 获取数据时,我没有执行任何序列化和反序列化。
public List<ContactNumber> ContactNumbers
获取数据时抛出错误
Error converting value "[{"Id":"1","Type":"Phone","HeaderLabel":"HQ - Main Line","ContactNumber":"+9122222222"}]" to type 'System.Collections.Generic.List`1[CosmosDB.Models.ContactNumber]'. Path 'ContactNumber', line 1, position 2411.
错误是因为迁移后字符串中添加了额外的\字符。
我不想在 cosmos DB 中对 class 进行序列化和反序列化,因为没有必要这样做。
那么如何在将数据从 SQL 数据库迁移到 Cosmos DB 文档时避免额外的 \?
您可以使用 Azure Function Cosmos DB Trigger 来处理创建的每个文档。请参考我的功能代码:
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json.Linq;
namespace ProcessJson
{
public class Class1
{
[FunctionName("DocumentUpdates")]
public static void Run(
[CosmosDBTrigger(databaseName:"db",collectionName: "item", ConnectionStringSetting = "CosmosDBConnection",LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)]
IReadOnlyList<Document> documents,
TraceWriter log)
{
log.Verbose("Start.........");
String endpointUrl = "https://***.documents.azure.com:443/";
String authorizationKey = "***";
String databaseId = "db";
String collectionId = "import";
DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
for (int i = 0; i < documents.Count; i++)
{
Document doc = documents[i];
if((doc.alreadyFormat == Undefined.Value) ||(!doc.alreadyFormat)){
String info = doc.GetPropertyValue<String>("info");
JArray o = JArray.Parse(info);
doc.SetPropertyValue("info", o);
doc.SetPropertyValue("alreadyFormat", true);
client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, doc.Id), doc);
log.Verbose("Update document Id " + doc.Id);
}
}
}
}
}
另外请参考案例:
希望对你有帮助。