Cosmos DBにSQL Serverのデータをインポート
Cosmos DBのDocumentDB APIで使用するデータをインポートします。今回はローカルSQL Serverのデータ(AdventureWorks2016CTP3)がソースです。 方法はいろいろありますが、今回試したのは簡単にできるツールとPowerShellの2つ。
データ移行ツールを使う
データ移行ツール使って簡単・高速でインポートできます。いろいろなデータソースに対応していますし、データベースやコレクションも同時に作成してくれる便利なツールです。
詳しい説明は、DocumentDB API 用に Azure Cosmos DB にデータをインポートする方法をご覧ください。
上記サイトのサンプルでクエリを外部ファイルと指定したするとこんな感じです。 使用するクエリで気をつけるところもIDをvarcharにCAST、階層構造をNestingSeparatorで指定した".“にする程度です。
コマンド
dt.exe /s:SQL /s.ConnectionString:"Data Source=.\;Initial Catalog=AdventureWorks2016CTP3;Integrated Security=true;" /s.QueryFile:"..\ex1.sql" /s.NestingSeparator:. /t:DocumentDBBulk /t.ConnectionString:"AccountEndpoint=https://cosmosdbtest.documents.azure.com:443/;AccountKey=XXXXXXXXXXXXXXXXXXXXXXXX==;Database=testdb2;" /t.Collection:testcoll1 /t.IdField:Id
クエリ(ex1.sql)
SELECT CAST(BusinessEntityID AS varchar) as Id, Name, AddressType as [Address.AddressType], AddressLine1 as [Address.AddressLine1], City as [Address.Location.City], StateProvinceName as [Address.Location.StateProvinceName], PostalCode as [Address.PostalCode], CountryRegionName as [Address.CountryRegionName] FROM Sales.vStoreWithAddresses WHERE AddressType='Main Office'
適当にPowerShellを書いて実行
PowerShellでもSQL Serverから読み取ってAPIでドキュメント作っても簡易的なインポートはできます。単純に1つ1つ登録しているので速度的なものは考慮なし。
PowerShell
Generate-MasterKeyAuthorizationSignatureとCreateは前回と同じなので省略。
Set-Location SQLSERVER:\SQL\localhost\DEFAULT\Databases\AdventureWorks2016CTP3 $result = Invoke-Sqlcmd -InputFile "ex2.sql" $items = $result.ItemArray | ConvertFrom-Json ForEach ($item in $items) { $json = $item| ConvertTo-Json Create -EndPoint $EndPoint -DataBaseId $DataBaseId -MasterKey $Keys.primaryMasterKey -ResourceType "docs" -ResourceLink "dbs/$DatabaseId/colls/$CollectionId" -BodyJson $json }
クエリ
クエリは列名「Id」を「id」に、クエリ結果がJSONで欲しいので FOR JSON PATH を追加。
[ex2.sql]
SELECT CAST(e.BusinessEntityID AS varchar) as id, NationalIDNumber, LoginID, JobTitle, JSON_QUERY(HistoryDepartment.HistoryDepartmentID, '$') AS HistoryDepartmentID FROM HumanResources.Employee e INNER JOIN (SELECT BusinessEntityID, '[' + REPLACE((SELECT DepartmentID AS [data()] FROM HumanResources.EmployeeDepartmentHistory WHERE H.BusinessEntityID = BusinessEntityID FOR XML PATH('')), ' ', ',') + ']' AS HistoryDepartmentID FROM HumanResources.EmployeeDepartmentHistory AS H GROUP BY BusinessEntityID) AS HistoryDepartment ON HistoryDepartment.BusinessEntityID = e.BusinessEntityID FOR JSON PATH
dt.exeだと配列を文字型として処理するので[1, 2, 3]が"[1, 2, 3]“となってしまったので、こちらを使用。例えば下記のような配列情報を持つデータのHistoryDepartmentIDも配列としてインポートできます。
id | NationalIDNumber | LoginID | JobTitle | HistoryDepartmentID |
---|---|---|---|---|
3 | 509647174 | adventure-works\roberto0 | Engineering Manager | [1] |
4 | v112457891 | adventure-works\rob0 | Senior Tool Designer | [1,2] |
登録結果
{ "id": "4", "NationalIDNumber": "112457891", "LoginID": "adventure-works\\rob0", "JobTitle": "Senior Tool Designer", "HistoryDepartmentID": [ 1, 2 ], "_rid": "AAAAAAAAAAAAAAAAAAAA==", "_self": "dbs/3mwJAA==/colls/0000000=/docs/AAAAAAAAAAAAAAAAAAAA==/", "_etag": "\"00000000-0000-0000-0000-000000000000\"", "_attachments": "attachments/", "_ts": 1503725821 }
Cosmos DB はじめました
Cosmos DB をちょっと使い始めてみたので書き付け。 最初にCosmos DBはAzureのNoSQLデータベースサービスという程度の知識しかないので、以下のサイトでお勉強。
Azure Cosmos DB入門 - ryuichi111stdの技術日記
ざっくり概要や特徴などがわかったところで、手を動かして実際に試してみます。
たくさんデータモデルがあるようですが、今回はドキュメント データモデルでDocumentDB APIを使います。最初なので、まずはAzure上にリソースを作成。作ったり・消したりするのでPowerShellでさくっと準備。
データベース アカウントの作成
# ログイン Login-AzureRmAccount # サブスクリプション選択 Select-AzureRmSubscription -SubscriptionId "00000000-0000-0000-0000-000000000000" # リソースグループの作成 New-AzureRmResourceGroup -Name "RG01" -Location "Japan West" # データベース アカウントの作成 $locations = @(@{"locationName"="Japan West"; "failoverPriority"=0}) $ipRangeFilter = "" $consistencyPolicy = @{"defaultConsistencyLevel"="Session"; "maxIntervalInSeconds"= "5"; "maxStalenessPrefix"= "100"} $dbProperties = @{ "databaseAccountOfferType"="Standard"; "locations"=$locations; "consistencyPolicy"=$consistencyPolicy; "ipRangeFilter"=$ipRangeFilter } New-AzureRmResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2016-03-31" -ResourceGroupName "RG01" -Location "Japan West" -Name "cosmosdbtest" -PropertyObject $dbProperties
ちなみにApiVersionは以下で確認。
((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.DocumentDb).ResourceTypes | Where-Object ResourceTypeName -eq databaseAccounts).ApiVersions
ということでリソースの準備完了。ついでなので、PowerShellからデータベース、コレクション、ドキュメントもREST APIを使って作成してみます。Authorizationを生成するコードなどは、How to query Azure Cosmos DB resources using the REST API by PowerShellから拝借。ApiVersionはSupported REST API Versionsで確認。
Add-Type -AssemblyName System.Web # generate authorization key Function Generate-MasterKeyAuthorizationSignature { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$verb, [Parameter(Mandatory=$false)][String]$resourceLink, [Parameter(Mandatory=$true)][String]$resourceType, [Parameter(Mandatory=$true)][String]$dateTime, [Parameter(Mandatory=$true)][String]$key, [Parameter(Mandatory=$true)][String]$keyType, [Parameter(Mandatory=$true)][String]$tokenVersion ) $hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256 $hmacSha256.Key = [System.Convert]::FromBase64String($key) $payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n" $hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad)) $signature = [System.Convert]::ToBase64String($hashPayLoad); [System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature") } # いろいろCreate Function Create { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$EndPoint, [Parameter(Mandatory=$true)][String]$DataBaseId, [Parameter(Mandatory=$true)][String]$MasterKey, [Parameter(Mandatory=$true)][String]$ResourceType, [Parameter(Mandatory=$false)][String]$ResourceLink, [Parameter(Mandatory=$true)][String]$BodyJson ) $verb = "POST" $dateTime = [DateTime]::UtcNow.ToString("r") $authHeader = Generate-MasterKeyAuthorizationSignature -verb $verb -resourceLink $ResourceLink -resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime $header = @{authorization=$authHeader;"x-ms-version"="2017-02-22";"x-ms-date"=$dateTime} $contentType= "application/json" $queryUri = "$EndPoint$ResourceLink/$ResourceType" $result = Invoke-RestMethod -Method $verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $bodyJson -Debug $result | ConvertTo-Json -Depth 10 } # キーを取得 $Keys = Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2016-03-31" -ResourceGroupName "RG01" -Name "cosmosdbtest" # 作るモノの設定 $EndPoint = "https://cosmosdbtest.documents.azure.com/" $DataBaseId = "hogedb1" $CollectionId = "hogecoll1" $DocumentId = "doc01" # データベース作成 $json = @{"id"="$DataBaseId"} | ConvertTo-Json Create -EndPoint $EndPoint -DataBaseId $DataBaseId -MasterKey $Keys.primaryMasterKey -ResourceType "dbs" -ResourceLink "" -BodyJson $json # コレクション作成 $json = @{"id"="$CollectionId"} | ConvertTo-Json Create -EndPoint $EndPoint -DataBaseId $DataBaseId -MasterKey $Keys.primaryMasterKey -ResourceType "colls" -ResourceLink "dbs/$DatabaseId" -BodyJson $json # ドキュメント作成 $json = @{ "id"="$DocumentId"; "name"="hogehoge"; "age" = 24; } | ConvertTo-Json Create -EndPoint $EndPoint -DataBaseId $DataBaseId -MasterKey $Keys.primaryMasterKey -ResourceType "docs" -ResourceLink "dbs/$DatabaseId/colls/$CollectionId" -BodyJson $json
という感じで基本的な環境の作成は完了。疲れたのでまるっと削除して本日は終了。
Remove-AzureRmResourceGroup -ResourceGroupName "RG01"