SQL Data Services (SDS) 迁移工具包
将数据迁移到 Microsoft SQL Data Services 的过程和工具包。
引言
Microsoft SQL Data Services 是一种新的数据托管服务概念,它使公司和开发人员在数据托管和处理方面具有灵活性和可伸缩性。然而,它与普通的关系型数据库管理系统(RDBMS)完全不同,它不包含表和字段,而是包含权限(Authority)和实体(Entity)。这个工具包帮助开发人员或 DBA 将现有数据迁移到 SDS。
该库是 **ROH Project: The Automata Storytelling Engine and Its Prototype** 组件的一部分。
主要思想
SDS 有两个类别定义数据所在位置:权限(Authorities)和容器(Containers)。实体(Entities)类似于记录(Records),包含各种类型的数据。但是,存在一些限制。SDS 是一种简单的数据库,因此没有索引,也没有实体之间的关系信息。

这是思路。如果我们像上面那样映射,SDS 可以像 RDBMS 一样工作。
- 数据库 > 权限(Authority)
- 表 > 容器(Container)
- 记录 > 实体(Entity)
工作原理

这个工具是用 VBA(Visual Basic for Application)制作的,特别是在 Microsoft Excel 中。
整个过程分为两部分:迁移前准备和迁移。
在迁移前准备部分,您需要将数据库导入 Excel。有几种类型的数据库,如 Microsoft SQL Server、Access、Oracle、mySQL 等,其中大多数都有导出数据到其他格式(如 CSV 或 XLS)的方法。导出到 Excel 后,您需要清理数据以适应 SDS。
在迁移部分,您应该将发布的迁移工具包 VBA 文件附加到您的 Excel 文件中。当您运行代码时,应用程序会询问您的 SDS 账户信息(ID、密码)和要创建的权限名称。迁移工具包会自动生成格式为 SSDS XML 格式的批量数据 XML 文件,并调用 Microsoft SQL Data Services SDK 中的 *st.exe* 管理实用程序,然后请求创建权限、容器和实体。
要求
- SDS 账户:Windows Azure Platform 或 SQL Data Services 目前为 SDS 提供免费的 beta 测试(SQL Data Services 需要信用卡信息)。
- Microsoft Office 2003 或更高版本
- Microsoft SQL Data Services SDK
迁移前准备过程
导出数据库
我无法解释所有 RDBMS 的整个过程,但会以使用 Microsoft SQL Server 导入和导出向导导出数据库为例。

运行 SQL Server 导入和导出向导。

选择要迁移的数据源和数据库。

选择 Excel 数据源,并选择其文件名作为目标。

数据清理
成功将数据导出到 Excel 后,您需要净化数据。有以下一些规则:
- 权限、容器、实体名称中不能有空格,也不能有下划线。(连字符“-”可以)
- 第一行是字段名。
- 工作表名称是容器的名称。
String
字段应设置为文本格式(图 2)。Double
或Float
应设置为数字格式,并限制小数位数(图 3)。- 第一列是 ID,应该是数字。
- 实体 ID 定义为“工作表名称”+“ID”。
迁移后处理
- 下载工具包文件
- 打开已清理的 Excel 文件
- 打开 Visual Basic 编辑器
- 右键单击“模块”,然后选择“文件”->“导入”
- 选择工具包文件(*modSDS.bas*)
- 保存并以正常安全级别重新打开(图 4)。
- 运行模块。有 2 种类型的函数,如下所示:
- 输入 SDS ID、密码和权限。
代码
首先,定义 Microsoft SQL Data Services SDK 的路径。
'Location of Microsoft SQL Data Services SDK
Const strSDSSDKPath = "C:\Program Files\Microsoft SQL Data Services SDK"
'Microsoft SQL Data Services SDK Admin File
Const strSDSSDKFilename = "st.exe"
接下来,构建一个创建权限的函数。
Sub SDSCreateAuthority(ByVal Authority As String, _
ByVal ID As String, ByVal Password As String)
ExecuteAndWait (strSDSSDKPath & Application.PathSeparator & _
strSDSSDKFilename & " create " & Authority & " /user:" & _
ID & " /password:" & Password & " /soap /verbose")
End Sub
然后,构建一个创建容器的函数。当权限中存在同名容器时会发生错误。(但不会停止。)
Sub SDSCreateContainer(ByVal Authority As String, _
ByVal Container, ByVal ID As String, ByVal Password As String)
ExecuteAndWait (strSDSSDKPath & Application.PathSeparator & _
strSDSSDKFilename & " create " & Authority & " " & _
Container & " /user:" & ID & " /password:" & Password & " /soap /verbose")
End Sub
要上传批量数据,您必须将数据转换为 XML 数据库架构。
Sub BuildXML(ByVal Worksheet As Worksheet, ByVal Filename As String)
Dim inFile As Integer
Dim j, k As Integer
Dim strStartEntity As String
Dim strLine As String
Dim strType As String
Dim strValue
inFile = FreeFile
Open (ActiveWorkbook.Path & Application.PathSeparator & _
Filename & ".xml") For Output As inFile
j = 2
strStartEntity = "<" & Filename & _
" xmlns:s=""http://schemas.microsoft.com/sitka/2008/03/"" " & _
"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
"xmlns:xs=""http://www.w3.org/2001/XMLSchema"">"
While (Worksheet.Cells(j, 1)) <> ""
k = 1
Print #inFile, strStartEntity
strLine = " <s:Id>" & Filename & Worksheet.Cells(j, 1) & "</s:Id>"
Print #inFile, strLine
While (Worksheet.Cells(1, k)) <> ""
strType = ""
strLine = ""
strValue = Worksheet.Cells(j, k)
If Worksheet.Cells(j, k).NumberFormat = "General" Then
If IsNumeric(Worksheet.Cells(j, k)) = True Then
strType = "xs:decimal"
If Int(Val(strValue)) <> Val(strValue) Then
strValue = Format(Worksheet.Cells(j, k), "#0.00000000000000")
End If
Else
If IsDate(Worksheet.Cells(j, k)) = True Then
strType = "xs:dateTime"
strValue = Format(Worksheet.Cells(j, k), _
"yyyy-mm-ddThh:mm:ssZ")
Else
strType = "xs:string"
End If
End If
Else
If Worksheet.Cells(j, k).NumberFormat = "@" Then
strType = "xs:string"
Else
If IsNumeric(Worksheet.Cells(j, k)) = True Then
strType = "xs:decimal"
If Int(Val(strValue)) <> Val(strValue) Then
strValue = Format(Worksheet.Cells(j, k), _
"#0.00000000000000")
End If
Else
If IsDate(Worksheet.Cells(j, k)) = True Then
strType = "xs:dateTime"
strValue = Format(Worksheet.Cells(j, k), _
"yyyy-mm-ddThh:mm:ssZ")
Else
strType = "xs:string"
End If
End If
End If
End If
If (strType <> "") And (strValue <> "") Then
strLine = " <" & Worksheet.Cells(1, k) & _
" xsi:type=""" & strType & """>" & _
strValue & "</" & Worksheet.Cells(1, k) & ">"
Print #inFile, strLine
End If
k = k + 1
Wend
j = j + 1
strLine = "</" & Filename & ">"
Print #inFile, strLine
Wend
Close inFile
End Sub
此函数将工作表转换为 XML 文件。我使用工作表名称作为文件名。完成 XML 文件制作后,使用此函数上传文件。
Sub SDSBatchUpload(ByVal Authority As String, ByVal Container As String, _
ByVal Filename, ByVal ID As String, ByVal Password As String)
ExecuteAndWait (strSDSSDKPath & Application.PathSeparator & _
strSDSSDKFilename & " loadbatch " & Authority & " " & _
Container & " " & Filename & " /user:" & ID & " /password:" & _
Password & " /soap /verbose /overwrite /parallel:4")
End Sub
您可以使用以下函数轻松完成这些过程:
函数列表
ConvertActiveSheetToSDSXML
:将当前工作表转换为 SDS 并上传。ConvertToSDSXML
:将所有工作表转换为 SDS 并上传。
迁移后处理
您可以使用 Omega.SDSClient(由 Silverlight 驱动的 SDS 浏览器)检查迁移结果。
有用链接
历史
- (2009/02/27) 首个版本
- (2009/03/04) 添加了代码介绍