/
Export-ToParquet.ps1
210 lines (174 loc) · 9.38 KB
/
Export-ToParquet.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#======================================================================================================================#
# #
# AzureSynapseScriptsAndAccelerators - PowerShell and T-SQL Utilities #
# #
# This utility was developed to aid SMP/MPP migrations to Azure Synapse Analytics. #
# It is not an officially supported Microsoft application or tool. #
# #
# The utility and any script outputs are provided on "AS IS" basis and #
# there are no warranties, express or implied, including, but not limited to implied warranties of merchantability #
# or fitness for a particular purpose. #
# #
# The utility is therefore not guaranteed to generate perfect code or output. The output needs carefully reviewed. #
# #
# USE AT YOUR OWN RISK. #
# #
#======================================================================================================================#
#
###################################################################################################################################
###################################################################################################################################
#
# Author: Andrey Mirskiy
# January 2022
# Description: The script extracts data from SQL Server or APS databases to Parquet files for further import into Azure Synapse Analytics.
# The script uses one of these libraries to generate Parquet-files.
# ParquetSharp NuGet package - https://www.nuget.org/packages/ParquetSharp
# Parquet.Net NuGet package - https://www.nuget.org/packages/Parquet.Net
#
###################################################################################################################################
#Requires -Version 7.0
param(
[Parameter(Mandatory=$false, HelpMessage="Configuration file path")]
[ValidateScript({
if( -Not ($_ | Test-Path -PathType Leaf) ) { throw "File $_ does not exist" }
return $true
})]
[string] $ConfigFile = $("$PSScriptRoot\ConfigFile.csv"),
[Parameter(Mandatory=$true, HelpMessage="The name of SQL Server / APS / PDW instance")]
[string] $ServerName, # = "synapse-demo.sql.azuresynapse.net",
[Parameter(Mandatory=$true, HelpMessage="User name")]
[string] $UserName, # = "sqladminuser",
[Parameter(Mandatory=$true, HelpMessage="Password")]
[SecureString] $Password, # = $(ConvertTo-SecureString "******" -AsPlainText -Force),
[Parameter(Mandatory=$false, HelpMessage="Connection timeout")]
[int] $ConnectionTimeout = 10,
[Parameter(Mandatory=$false, HelpMessage="Command timeout")]
[int] $CommandTimeout = 30,
[Parameter(Mandatory=$false, HelpMessage="Maximum number of rows per rowgroup")]
[int] $RowsPerRowGroup = 10000000 ,
[Parameter(Mandatory=$false, HelpMessage="Maximum number of simultaneous jobs")]
[int] $MaxJobsCount = 4,
[Parameter(Mandatory=$false, HelpMessage="Use Parquet.NET library or ParquetSharp (default)")]
[switch] $UseParquetNet
)
Function Get-AbsolutePath
{
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)] [string]$Path
)
if ([System.IO.Path]::IsPathRooted($Path) -eq $false) {
if ($PSScriptRoot) {
return [IO.Path]::GetFullPath( (Join-Path -Path $PSScriptRoot -ChildPath $Path) )
} else {
return [IO.Path]::GetFullPath( (Join-Path -Path $(Get-Location) -ChildPath $Path) )
}
} else {
return $Path
}
}
function Download-ParquetSharp
{
if (-not(Test-Path "$PSScriptRoot\parquetsharp.5.0.0.nupkg\lib\netstandard2.1\ParquetSharp.dll")) {
$zipFile = "$PSScriptRoot\parquetsharp.5.0.0.nupkg.zip"
if (-not(Test-Path "$PSScriptRoot\parquetsharp.5.0.0.nupkg.zip")) {
$url = "https://www.nuget.org/api/v2/package/ParquetSharp/4.0.0"
Invoke-WebRequest -Uri $url -OutFile $zipFile
}
Expand-Archive -Path $zipFile -DestinationPath "$PSScriptRoot\parquetsharp.5.0.0.nupkg" -Force
}
# if (-not(Test-Path "$PSScriptRoot\parquetsharp.6.0.1-beta1.nupkg\lib\netstandard2.1\ParquetSharp.dll")) {
# $zipFile = "$PSScriptRoot\parquetsharp.6.0.1-beta1.nupkg.zip"
# if (-not(Test-Path "$PSScriptRoot\parquetsharp.6.0.1-beta1.nupkg.zip")) {
# $url = "https://www.nuget.org/api/v2/package/ParquetSharp/6.0.1-beta1"
# Invoke-WebRequest -Uri $url -OutFile $zipFile
# }
# Expand-Archive -Path $zipFile -DestinationPath "$PSScriptRoot\parquetsharp.6.0.1-beta1.nupkg" -Force
# }
}
function Download-ParquetNet
{
if (-not(Test-Path "$PSScriptRoot\parquet.net.3.9.1.nupkg\lib\net5.0\Parquet.dll")) {
$zipFile = "$PSScriptRoot\parquet.net.3.9.1.nupkg.zip"
if (-not(Test-Path "$PSScriptRoot\parquet.net.3.9.1.nupkg.zip")) {
$url = "https://www.nuget.org/api/v2/package/Parquet.Net/3.9.1"
Invoke-WebRequest -Uri $url -OutFile $zipFile
}
Expand-Archive -Path $zipFile -DestinationPath "$PSScriptRoot\parquet.net.3.9.1.nupkg" -Force
}
if (-not(Test-Path "$PSScriptRoot\ironsnappy.1.3.0.nupkg\lib\netstandard2.1\IronSnappy.dll")) {
$zipFile = "$PSScriptRoot\ironsnappy.1.3.0.nupkg.zip"
if (-not(Test-Path "$PSScriptRoot\ironsnappy.1.3.0.nupkg.zip")) {
$url = "https://www.nuget.org/api/v2/package/IronSnappy/1.3.0"
Invoke-WebRequest -Uri $url -OutFile $zipFile
}
Expand-Archive -Path $zipFile -DestinationPath "$PSScriptRoot\ironsnappy.1.3.0.nupkg" -Force
}
}
###############################################################################################
# Main logic here
###############################################################################################
# Download Parquet libraries
if ($UseParquetNet) {
Download-ParquetNet
} else {
Download-ParquetSharp
}
$startTime = Get-Date
$ConfigFile = Get-AbsolutePath $ConfigFile
$configData = Import-Csv -Path $ConfigFile
$jobs = @( )
foreach ($record in ($configData | Where-Object {$_.Enabled -eq 1})) {
$database = $record.Database
$jobName = $record.JobName
$query = $record.Query
$filePath = Get-AbsolutePath $record.FilePath
#Export-Table -Database $database -Table $table -Query $query -FilePath $filePath
# create export folder in parent script to avoid conflicts when executing simultaneous jobs
$exportFolderPath = Split-Path -Path $filePath -Parent
if (-not (Test-Path -Path $exportFolderPath)) {
New-Item -Path $exportFolderPath -ItemType Directory | Out-Null
}
$jobCreated = $false
while (!$jobCreated) {
$runningJobs = Get-Job -State Running
if ($runningJobs.Count -lt $MaxJobsCount) {
if ($UseParquetNet) {
$jobs += Start-Job -Name $jobName `
-File $PSScriptRoot\Export-ParquetNet.ps1 `
-WorkingDirectory $PSScriptRoot `
-ArgumentList $ServerName,$database,$UserName,$Password,$jobName,$query,$filePath,$ConnectionTimeout,$CommandTimeout,$RowsPerRowGroup
} else {
$jobs += Start-Job -Name $jobName `
-File $PSScriptRoot\Export-ParquetSharp.ps1 `
-WorkingDirectory $PSScriptRoot `
-ArgumentList $ServerName,$database,$UserName,$Password,$jobName,$query,$filePath,$ConnectionTimeout,$CommandTimeout,$RowsPerRowGroup
}
$jobCreated = $true
} else {
Start-Sleep -Milliseconds $(Get-Random -Min 50 -Max 200)
}
# Print all new output
Get-Job | Where-Object {$_.HasMoreData} | Receive-Job
#Get-Job -State Running | Receive-Job
# Print out all completed jobs
#Get-Job -State Completed | Receive-Job
# Remove completed jobs
#Get-Job -State Completed | Remove-Job
}
}
# Print output of all remaining jobs
while ($jobsWithOutput = Get-Job | Where-Object {$_.HasMoreData}) {
$jobsWithOutput | Receive-Job
Start-Sleep -Milliseconds $(Get-Random -Min 50 -Max 200)
}
# Remove completed jobs. Use $jobs variable to inspect all jobs.
Get-Job -State Completed | Remove-Job
# Forcefully remove jobs
#Get-Job | Remove-Job -Force
# Print all jobs
$jobs
$finishTime = Get-Date
Write-Host "Program Start Time: ", $startTime -ForegroundColor Green
Write-Host "Program Finish Time: ", $finishTime -ForegroundColor Green
Write-Host "Program Elapsed Time: ", ($finishTime-$startTime) -ForegroundColor Green