Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Bu örnek PowerShell betiği, kaynaktan havuza verilerin ilk tam kopyasından sonra kaynak veri deposundan havuz veri deposuna yalnızca yeni veya güncelleştirilmiş kayıtları yükler.
Not
Azure ile etkileşim kurmak için Azure Az PowerShell modülünü kullanmanızı öneririz. Başlamak için bkz . Azure PowerShell'i yükleme. Az PowerShell modülüne nasıl geçeceğinizi öğrenmek için bkz. Azure PowerShell’i AzureRM’den Az’ye geçirme.
Bu örnek için Azure PowerShell gerekir. Sürümü bulmak için Get-Module -ListAvailable Az komutunu çalıştırın.
Yüklemeniz veya yükseltmeniz gerekirse, bkz. Azure PowerShell Modülü yükleme.
Azure'a bağlanmak için Bağlan-AzAccount cmdlet'ini çalıştırın.
Bu örneği çalıştırma önkoşulları için bkz . öğretici: artımlı kopya .
Örnek betik
Önemli
Bu betik, c:\ klasöründeki sabit sürücünüzde Data Factory varlıklarını (bağlı hizmet, veri kümesi ve işlem hattı) tanımlayan JSON dosyaları oluşturur.
# Set variables with your own values
$resourceGroupName = "<azure resource group name>" # group will be created if it does not exist already
$dataFactoryName = "<data factory name>" # must be globally unique
$dataFactoryRegion = "East US"
$storageAccountName = "<Az.Storage account name>"
$storageAccountKey = "<Az.Storage account key>"
$azureSqlServer = "<azure sql server name>"
$azureSqlDatabase = "<azure sql database name>"
$azureSqlUser = "<azure sql server - user name>"
$azureSqlPassword = "<azure sql server - user password>"
$outputBlobFolderPath = "<azure blob container>/<folder>" # output folder where the records are copied.
$azureStorageLinkedServiceName = "AzureStorageLinkedService"
$azureSqlDatabaseLinkedServiceName = "AzureSQLDatabaseLinkedService"
$sourceDatasetName = "SourceDataset"
$sinkDatasetName = "SinkDataset"
$sourceSqlTableName = "datasource"
$watermarkDatasetName = "WatermarkDataset"
$pipelineName = "IncrementalCopyPipeline"
# Create a resource group
New-AzResourceGroup -Name $resourceGroupName -Location $dataFactoryRegion
# Create a data factory
$df = Set-AzDataFactoryV2 -ResourceGroupName $resourceGroupName -Location $dataFactoryRegion -Name $dataFactoryName
# Create an Az.Storage linked service in the data factory
## JSON definition of the linked service.
$storageLinkedServiceDefinition = @"
{
"name": "$azureStorageLinkedServiceName",
"properties": {
"type": "AzureStorage",
"typeProperties": {
"connectionString": {
"value": "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$storageAccountKey",
"type": "SecureString"
}
}
}
}
"@
## IMPORTANT: stores the JSON definition in a file that will be used by the Set-AzDataFactoryV2LinkedService command.
$storageLinkedServiceDefinition | Out-File ./$azureStorageLinkedServiceName.json
## Creates a linked service in the data factory
Set-AzDataFactoryV2LinkedService -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$azureStorageLinkedServiceName" -File ./$azureStorageLinkedServiceName.json
## JSON definition of the linked service.
$azureSQLDatabaseLinkedServiceDefinition = @"
{
"name": "$azureSqlDatabaseLinkedService",
"properties": {
"type": "AzureSqlDatabase",
"typeProperties": {
"connectionString": {
"value": "Server = tcp:$azureSqlServer.database.windows.net,1433;Initial Catalog=$azureSqlDatabase; Persist Security Info=False; User ID=$azureSqlUser; Password=$azureSqlPassword; MultipleActiveResultSets = False; Encrypt = True; TrustServerCertificate = False; Connection Timeout = 30;",
"type": "SecureString"
}
}
}
}
"@
## IMPORTANT: stores the JSON definition in a file that will be used by the Set-AzDataFactoryV2LinkedService command.
$azureSQLDatabaseLinkedServiceDefinition | Out-File ./$azureSqlDatabaseLinkedServiceName.json
## Creates a linked service in the data factory
Set-AzDataFactoryV2LinkedService -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$azureSqlDatabaseLinkedServiceName" -File ./$azureSqlDatabaseLinkedServiceName.json
# Create an Azure SQL dataset in the data factory
## JSON definition of the dataset
$sourceDataset = @"
{
"name": "$sourceDatasetName",
"properties": {
"type": "AzureSqlTable",
"typeProperties": {
"tableName": "$sourceSqlTableName"
},
"linkedServiceName": {
"referenceName": "$azureSqlDatabaseLinkedServiceName",
"type": "LinkedServiceReference"
}
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Dataset command.
$sourceDataset | Out-File ./$sourceDatasetName.json
## Create a dataset in the data factory
Set-AzDataFactoryV2Dataset -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$sourceDatasetName" -File "./$sourceDatasetName.json"
# Create an Azure Blob dataset in the data factory
## JSON definition of the dataset
$sinkDataset = @"
{
"name": "$sinkDatasetName",
"properties": {
"type": "AzureBlob",
"typeProperties": {
"folderPath": "$outputBlobFolderPath",
"format": {
"type": "TextFormat"
}
},
"linkedServiceName": {
"referenceName": "$azureStorageLinkedServiceName",
"type": "LinkedServiceReference"
}
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Dataset command.
$sinkDataset | Out-File ./$sinkDatasetName.json
## Create a dataset in the data factory
Set-AzDataFactoryV2Dataset -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$sinkDatasetName" -File "./$sinkDatasetName.json"
## JSON definition of the dataset
$watermarkDataset = @"
{
"name": "$watermarkDatasetName",
"properties": {
"type": "AzureSqlTable",
"typeProperties": {
"tableName": "watermarktable"
},
"linkedServiceName": {
"referenceName": "$azureSqlDatabaseLinkedServiceName",
"type": "LinkedServiceReference"
}
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Dataset command.
$watermarkDataset | Out-File ./$watermarkDatasetName.json
## Create a dataset in the data factory
Set-AzDataFactoryV2Dataset -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$watermarkDatasetName" -File "./$watermarkDatasetName.json"
# Create a pipeline in the data factory
## JSON definition of the pipeline
$pipelineDefinition = @"
{
"name": "$pipelineName",
"properties": {
"activities": [
{
"name": "LookupWaterMarkActivity",
"type": "Lookup",
"typeProperties": {
"source": {
"type": "SqlSource",
"sqlReaderQuery": "select * from watermarktable"
},
"dataset": {
"referenceName": "$watermarkDatasetName",
"type": "DatasetReference"
}
}
},
{
"name": "LookupMaxValuefromSourceActivity",
"type": "Lookup",
"typeProperties": {
"source": {
"type": "SqlSource",
"sqlReaderQuery": "SELECT MAX(LastModifytime) as NewWatermarkvalue FROM dbo.datasource"
},
"dataset": {
"referenceName": "$sourceDatasetName",
"type": "DatasetReference"
}
}
},
{
"name": "IncrementalCopyActivity",
"type": "Copy",
"typeProperties": {
"source": {
"type": "SqlSource",
"sqlReaderQuery": "select * from dbo.datasource where LastModifytime > '@{activity('LookupWaterMarkActivity').output.WatermarkValue}' and LastModifytime <= '@{activity('LookupMaxValuefromSourceActivity').output.NewWatermarkvalue}'"
},
"sink": {
"type": "BlobSink"
}
},
"dependsOn": [
{
"activity": "LookupMaxValuefromSourceActivity",
"dependencyConditions": [
"Succeeded"
]
},
{
"activity": "LookupWaterMarkActivity",
"dependencyConditions": [
"Succeeded"
]
}
],
"inputs": [
{
"referenceName": "$sourceDatasetName",
"type": "DatasetReference"
}
],
"outputs": [
{
"referenceName": "$sinkDatasetName",
"type": "DatasetReference"
}
]
},
{
"name": "StoredProceduretoWriteWatermarkActivity",
"type": "SqlServerStoredProcedure",
"typeProperties": {
"storedProcedureName": "sp_write_watermark",
"storedProcedureParameters": {
"LastModifiedtime": {"value": "@{activity('LookupMaxValuefromSourceActivity').output.NewWatermarkvalue}", "type": "datetime" },
"TableName": { "value":"@{activity('LookupWaterMarkActivity').output.TableName}", "type":"String"}
}
},
"linkedServiceName": {
"referenceName": "$azureSqlDatabaseLinkedServiceName",
"type": "LinkedServiceReference"
},
"dependsOn": [
{
"activity": "IncrementalCopyActivity",
"dependencyConditions": [
"Succeeded"
]
}
]
}
],
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Pipeline command.
$pipelineDefinition | Out-File ./$pipelineName.json
## Create a pipeline in the data factory
Set-AzDataFactoryV2Pipeline -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "$pipelineName" -File "./$pipelineName.json"
$RunId = Invoke-AzDataFactoryV2Pipeline -PipelineName "$pipelineName" -ResourceGroup $resourceGroupName -dataFactoryName $dataFactoryName
# Check the pipeline run status until it finishes the copy operation
Start-Sleep -Seconds 30
while ($True) {
$result = Get-AzDataFactoryV2ActivityRun -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -PipelineRunId $runId -RunStartedAfter (Get-Date).AddMinutes(-30) -RunStartedBefore (Get-Date).AddMinutes(30)
if (($result | Where-Object { $_.Status -eq "InProgress" } | Measure-Object).count -ne 0) {
Write-Host "Pipeline run status: In Progress" -foregroundcolor "Yellow"
Start-Sleep -Seconds 30
}
else {
Write-Host "Pipeline '"$pipelineName"' run finished. Result:" -foregroundcolor "Yellow"
$result
break
}
}
$result = Get-AzDataFactoryV2ActivityRun -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName `
-PipelineRunId $runId `
-RunStartedAfter (Get-Date).AddMinutes(-10) `
-RunStartedBefore (Get-Date).AddMinutes(10) `
-ErrorAction Stop
$result
if ($result.Status -eq "Succeeded") {`
$result.Output -join "`r`n"`
}`
else {`
$result.Error -join "`r`n"`
}
# INSERT INTO datasource
# VALUES (6, 'newdata','9/6/2017 2:23:00 AM')
# INSERT INTO datasource
# VALUES (7, 'newdata','9/7/2017 9:01:00 AM')
# Inovke the pipeline and see that only new records are copied to the destination.
# To remove the data factory from the resource gorup
# Remove-AzDataFactoryV2 -Name $dataFactoryName -ResourceGroupName $resourceGroupName
#
# To remove the whole resource group
# Remove-AzResourceGroup -Name $resourceGroupName
Dağıtımı temizleme
Örnek betiği çalıştırdıktan sonra, kaynak grubunu ve onunla ilişkili tüm kaynakları kaldırmak için aşağıdaki komutu kullanabilirsiniz:
Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
Veri fabrikasını kaynak grubundan kaldırmak için aşağıdaki komutu çalıştırın:
Remove-AzDataFactoryV2 -Name $dataFactoryName -ResourceGroupName $resourceGroupName
Betik açıklaması
Bu betik şu komutları kullanır:
| Command | Notlar |
|---|---|
| New-AzResourceGroup | Tüm kaynakların depolandığı bir kaynak grubu oluşturur. |
| Set-AzDataFactoryV2 | Veri fabrikası oluşturma. |
| Set-AzDataFactoryV2LinkedService | Veri fabrikasında bağlı bir hizmet oluşturur. Bağlı hizmet, bir veri deposunu veya hesaplamayı bir veri fabrikasına bağlar. |
| Set-AzDataFactoryV2Dataset | Veri fabrikasında bir veri kümesi oluşturur. Veri kümesi, işlem hattındaki bir etkinliğin giriş/çıkışını temsil eder. |
| Set-AzDataFactoryV2Pipeline | Veri fabrikasında bir işlem hattı oluşturur. İşlem hattı, belirli bir işlemi gerçekleştiren bir veya daha fazla etkinlik içerir. Bu işlem hattında, kopyalama etkinliği verileri bir konumdan Azure Blob Depolama başka bir konuma kopyalar. |
| Invoke-AzDataFactoryV2Pipeline | İşlem hattı için bir çalıştırma oluşturur. Başka bir deyişle işlem hattını çalıştırır. |
| Get-AzDataFactoryV2ActivityRun | İşlem hattındaki etkinliğin (etkinlik çalıştırması) çalıştırılmasıyla ilgili ayrıntıları alır. |
| Remove-AzResourceGroup | Bir kaynak grubunu tüm iç içe geçmiş kaynaklar dahil siler. |
İlgili içerik
Azure PowerShell hakkında daha fazla bilgi için bkz. Azure PowerShell belgeleri.
Ek Azure Data Factory PowerShell betik örnekleri Azure Data Factory PowerShell betiklerinde bulunabilir.