Aracılığıyla paylaş


Script bileşeni ile biçimleri standart metin dosyası ayrıştırma

Kaynak verileriniz bir standart olmayan biçimde düzenlendiğinde, o daha uygun-e doğru birleştirmek tüm ayrıştırma mantığını daha zincir üzere tek bir komut dosyası bulabilirsiniz birden çok Integration Servicesaynı sonucu elde etmek için dönüşümleri.

Örnek 1: Satır ayrılmış kayıtları ayrıştırma

Örnek 2: Üst ve alt kayıtların bölme

[!NOT]

Birden çok veri akışı görevler ve birden çok paket üzerinden daha kolay yeniden kullanabileceğiniz bileşen oluşturmak isterseniz, kodu bu Script bileşeni örnek başlangıç noktası olarak kullanarak özel veri akışı bileşeni için düşünün. Daha fazla bilgi için, bkz. Bir özel veri akışı bileşen geliştirme.

Örnek 1: Satır ayrılmış kayıtları ayrıştırma

Bu örnek, veri her sütunu ayrı bir satırda göründüğü bir metin dosyası almak ve Script bileşeni kullanarak hedef tabloya ayrıştırmak gösterilmiştir.

Veri akışı bir dönüşüm olarak kullanmak için Script bileşeni yapılandırma hakkında daha fazla bilgi için bkz: Zaman uyumlu dönüşümü Script bileşeni ile oluşturmave Uyumsuz bir dönüşümü Script bileşeni ile oluşturma.

Bu Script bileşeni örnek yapılandırmak için

  1. Oluşturup adlı bir metin dosyasına kaydetmeniz rowdelimiteddata.txtAşağıdaki kaynak verileri içeren:

    FirstName: Nancy
    LastName: Davolio
    Title: Sales Representative
    City: Seattle
    StateProvince: WA
    
    FirstName: Andrew
    LastName: Fuller
    Title: Vice President, Sales
    City: Tacoma
    StateProvince: WA
    
    FirstName: Steven
    LastName: Buchanan
    Title: Sales Manager
    City: London
    StateProvince:
    
    FirstName: Nancy
    LastName: Davolio
    Title: Sales Representative
    City: Seattle
    StateProvince: WA
    
    FirstName: Andrew
    LastName: Fuller
    Title: Vice President, Sales
    City: Tacoma
    StateProvince: WA
    
    FirstName: Steven
    LastName: Buchanan
    Title: Sales Manager
    City: London
    StateProvince:
    
  2. Açık Management Studiove örneğine bağlanmak SQL Server.

  3. Hedef veritabanını seçin ve yeni bir sorgu penceresi açar. Sorgu penceresinde hedef tablo oluşturmak için aşağıdaki kod yürütün:

    create table RowDelimitedData
    (
    FirstName varchar(32),
    LastName varchar(32),
    Title varchar(32),
    City varchar(32),
    StateProvince varchar(32)
    )
    
    create table RowDelimitedData
    (
    FirstName varchar(32),
    LastName varchar(32),
    Title varchar(32),
    City varchar(32),
    StateProvince varchar(32)
    )
    
  4. Açık SQL Server Veri Akışı Araçlarıve yeni Integration ServicesParseRowDelim.dtsx adlı paket.

  5. Düz dosya Bağlantı Yöneticisi eklemek için paketi RowDelimitedData adlandırın ve bir önceki adımda oluşturduğunuz rowdelimiteddata.txt dosyasına bağlanmak için yapılandırın.

  6. Bir ole db Bağlantı Yöneticisi paketi ekleyin ve örneğine bağlanmak için yapılandırın SQL Serverve hedef tablo oluşturduğunuz veritabanı.

  7. Veri akışı görev pakete eklemek ve tıklayın Veri akışı sekmesini SSIS Tasarımcısı.

  8. Düz dosya kaynağı veri akışı ekleyin ve RowDelimitedData Bağlantı Yöneticisi'ni kullanmak üzere yapılandırın. Tarih sütun sayfası Düz dosya kaynak Düzenleyici, tek kullanılabilir dış sütunu seçin.

  9. Veri akışı Script bileşeni ekleme ve bir dönüşümü yapılandırın. Düz dosya kaynağını çıkış için Script bileşeni bağlanın.

  10. Script bileşeni görüntülemek için çift Komut dosyası dönüşümü Düzenleyicisi.

  11. Tarih Giriş sütunları sayfası Komut dosyası dönüşümü Düzenleyicisi, tek kullanılabilir giriş sütun seçin.

  12. Tarih giriş ve çıkış sayfası Komut dosyası dönüşümü Düzenleyicisi, çıkış 0 seçmek ve ayarlamak onun SynchronousInputIDyok. Tüm uzunluğu 32 ile türü dize [dt_str] 5 çıktı sütunları oluşturun:

    • Ad

    • Soyad

    • Başlık

    • Şehir

    • StateProvince

  13. Tarih Script sayfası Komut dosyası dönüşümü Düzenleyicisi, tıklatın Komut dosyası Düzenle ve gösterilen kodu girin ScriptMainsınıf örnek. Komut dosyası geliştirme ortamı kapatın ve Komut dosyası dönüşümü Düzenleyicisi.

  14. SQL Server hedef veri akışı ekleyin. ole db Bağlantı Yöneticisi'ni ve RowDelimitedData tablo kullanmak üzere yapılandırın. Bu hedefe çıktı Script bileşeni bağlanın.

  15. Paketi çalıştırın. Paket bittikten sonra kayıtları incelemek SQL Serverhedef tablo.

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Dim columnName As String
        Dim columnValue As String

        ' Check for an empty row.
        If Row.Column0.Trim.Length > 0 Then
            columnName = Row.Column0.Substring(0, Row.Column0.IndexOf(":"))
            ' Check for an empty value after the colon.
            If Row.Column0.Substring(Row.Column0.IndexOf(":")).TrimEnd.Length > 1 Then
                ' Extract the column value from after the colon and space.
                columnValue = Row.Column0.Substring(Row.Column0.IndexOf(":") + 2)
                Select Case columnName
                    Case "FirstName"
                        ' The FirstName value indicates a new record.
                        Me.Output0Buffer.AddRow()
                        Me.Output0Buffer.FirstName = columnValue
                    Case "LastName"
                        Me.Output0Buffer.LastName = columnValue
                    Case "Title"
                        Me.Output0Buffer.Title = columnValue
                    Case "City"
                        Me.Output0Buffer.City = columnValue
                    Case "StateProvince"
                        Me.Output0Buffer.StateProvince = columnValue
                End Select
            End If
        End If

    End Sub
    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Dim columnName As String
        Dim columnValue As String

        ' Check for an empty row.
        If Row.Column0.Trim.Length > 0 Then
            columnName = Row.Column0.Substring(0, Row.Column0.IndexOf(":"))
            ' Check for an empty value after the colon.
            If Row.Column0.Substring(Row.Column0.IndexOf(":")).TrimEnd.Length > 1 Then
                ' Extract the column value from after the colon and space.
                columnValue = Row.Column0.Substring(Row.Column0.IndexOf(":") + 2)
                Select Case columnName
                    Case "FirstName"
                        ' The FirstName value indicates a new record.
                        Me.Output0Buffer.AddRow()
                        Me.Output0Buffer.FirstName = columnValue
                    Case "LastName"
                        Me.Output0Buffer.LastName = columnValue
                    Case "Title"
                        Me.Output0Buffer.Title = columnValue
                    Case "City"
                        Me.Output0Buffer.City = columnValue
                    Case "StateProvince"
                        Me.Output0Buffer.StateProvince = columnValue
                End Select
            End If
        End If

    End Sub
public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

        string columnName;
        string columnValue;

        // Check for an empty row.
        if (Row.Column0.Trim().Length > 0)
        {
            columnName = Row.Column0.Substring(0, Row.Column0.IndexOf(":"));
            // Check for an empty value after the colon.
            if (Row.Column0.Substring(Row.Column0.IndexOf(":")).TrimEnd().Length > 1)
            // Extract the column value from after the colon and space.
            {
                columnValue = Row.Column0.Substring(Row.Column0.IndexOf(":") + 2);
                switch (columnName)
                {
                    case "FirstName":
                        // The FirstName value indicates a new record.
                        this.Output0Buffer.AddRow();
                        this.Output0Buffer.FirstName = columnValue;
                        break;
                    case "LastName":
                        this.Output0Buffer.LastName = columnValue;
                        break;
                    case "Title":
                        this.Output0Buffer.Title = columnValue;
                        break;
                    case "City":
                        this.Output0Buffer.City = columnValue;
                        break;
                    case "StateProvince":
                        this.Output0Buffer.StateProvince = columnValue;
                        break;
                }
            }
        }

    }
public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

        string columnName;
        string columnValue;

        // Check for an empty row.
        if (Row.Column0.Trim().Length > 0)
        {
            columnName = Row.Column0.Substring(0, Row.Column0.IndexOf(":"));
            // Check for an empty value after the colon.
            if (Row.Column0.Substring(Row.Column0.IndexOf(":")).TrimEnd().Length > 1)
            // Extract the column value from after the colon and space.
            {
                columnValue = Row.Column0.Substring(Row.Column0.IndexOf(":") + 2);
                switch (columnName)
                {
                    case "FirstName":
                        // The FirstName value indicates a new record.
                        this.Output0Buffer.AddRow();
                        this.Output0Buffer.FirstName = columnValue;
                        break;
                    case "LastName":
                        this.Output0Buffer.LastName = columnValue;
                        break;
                    case "Title":
                        this.Output0Buffer.Title = columnValue;
                        break;
                    case "City":
                        this.Output0Buffer.City = columnValue;
                        break;
                    case "StateProvince":
                        this.Output0Buffer.StateProvince = columnValue;
                        break;
                }
            }
        }

    }

Örnek 2: Üst ve alt kayıtların bölme

Bu örnek, içinde bir belirsiz tarafından çocuk kayıt satır sayısı, ardından bir üst kayıt satır ayırıcı satırın önündeki metin dosyası almak ve Script bileşeni kullanarak düzgün normalleştirilmiş üst ve alt hedef tablolara ayrıştırmak gösterilmiştir. Başında ve sonunda her kaydın tanımlamak için bir yol var olduğu sürece bu basit örnek kolaylıkla birden fazla satır veya sütun üst ve alt her kayıt için kullandığınız kaynak dosyaları için adapte.

Dikkat notuDikkat

Bu örnek sadece gösteri amaçlı hazırlanmıştır. Örnek birden fazla kez çalıştırırsanız, yinelenen anahtar değerleri hedef tabloya ekler.

Veri akışı bir dönüşüm olarak kullanmak için Script bileşeni yapılandırma hakkında daha fazla bilgi için bkz: Zaman uyumlu dönüşümü Script bileşeni ile oluşturmave Uyumsuz bir dönüşümü Script bileşeni ile oluşturma.

Bu Script bileşeni örnek yapılandırmak için

  1. Oluşturup adlı bir metin dosyasına kaydetmeniz parentchilddata.txtAşağıdaki kaynak verileri içeren:

******** PARENT 1 DATA child 1 data child 2 data child 3 data child 4 data ******** PARENT 2 DATA child 5 data child 6 data child 7 data child 8 data ********

******** PARENT 1 DATA child 1 data child 2 data child 3 data child 4 data ******** PARENT 2 DATA child 5 data child 6 data child 7 data child 8 data ********

  1. Açık SQL Server Management Studiove örneğine bağlanmak SQL Server.

  2. Hedef veritabanını seçin ve yeni bir sorgu penceresi açar. Sorgu penceresinde hedef tablo oluşturmak için aşağıdaki kod yürütün:

    CREATE TABLE [dbo].[Parents](
    [ParentID] [int] NOT NULL,
    [ParentRecord] [varchar](32) NOT NULL,
     CONSTRAINT [PK_Parents] PRIMARY KEY CLUSTERED 
    ([ParentID] ASC)
    )
    GO
    CREATE TABLE [dbo].[Children](
    [ChildID] [int] NOT NULL,
    [ParentID] [int] NOT NULL,
    [ChildRecord] [varchar](32) NOT NULL,
     CONSTRAINT [PK_Children] PRIMARY KEY CLUSTERED 
    ([ChildID] ASC)
    )
    GO
    ALTER TABLE [dbo].[Children] ADD CONSTRAINT [FK_Children_Parents] FOREIGN KEY([ParentID])
    REFERENCES [dbo].[Parents] ([ParentID])
    
    CREATE TABLE [dbo].[Parents](
    [ParentID] [int] NOT NULL,
    [ParentRecord] [varchar](32) NOT NULL,
     CONSTRAINT [PK_Parents] PRIMARY KEY CLUSTERED 
    ([ParentID] ASC)
    )
    GO
    CREATE TABLE [dbo].[Children](
    [ChildID] [int] NOT NULL,
    [ParentID] [int] NOT NULL,
    [ChildRecord] [varchar](32) NOT NULL,
     CONSTRAINT [PK_Children] PRIMARY KEY CLUSTERED 
    ([ChildID] ASC)
    )
    GO
    ALTER TABLE [dbo].[Children] ADD CONSTRAINT [FK_Children_Parents] FOREIGN KEY([ParentID])
    REFERENCES [dbo].[Parents] ([ParentID])
    
  3. Açık SQL Server Veri Akışı Araçları (SSDT)ve yeni Integration ServicesSplitParentChild.dtsx adlı paket.

  4. Düz dosya Bağlantı Yöneticisi eklemek için paketi ParentChildData adlandırın ve bir önceki adımda oluşturduğunuz parentchilddata.txt dosyasına bağlanmak için yapılandırın.

  5. Bir ole db Bağlantı Yöneticisi paketi ekleyin ve örneğine bağlanmak için yapılandırın SQL Serverve hedef tabloları oluşturduğunuz veritabanı.

  6. Veri akışı görev pakete eklemek ve tıklayın Veri akışı sekmesini SSIS Tasarımcısı.

  7. Düz dosya kaynağı veri akışı ekleyin ve ParentChildData Bağlantı Yöneticisi'ni kullanmak üzere yapılandırın. Tarih sütun sayfası Düz dosya kaynak Düzenleyici, tek kullanılabilir dış sütunu seçin.

  8. Veri akışı Script bileşeni ekleme ve bir dönüşümü yapılandırın. Düz dosya kaynağını çıkış için Script bileşeni bağlanın.

  9. Script bileşeni görüntülemek için çift Komut dosyası dönüşümü Düzenleyicisi.

  10. Tarih Giriş sütunları sayfası Komut dosyası dönüşümü Düzenleyicisi, tek kullanılabilir giriş sütun seçin.

  11. Tarih girişleri ve çıkışları sayfası Komut dosyası dönüşümü Düzenleyicisi, çıkış 0 seçin, ParentRecords için yeniden adlandırın ve ayarlamak onun SynchronousInputIDyok. 2 Çıktı sütunları oluşturun:

    • Ve türü dört bayt işaretli tamsayı DT_I4 ParentID (birincil anahtar)

    • ParentRecord, 32 uzunluğundaki [dt_str] dize türünde.

  12. İkinci bir çıktı yaratmak ve belgeye ChildRecords adını verin. SynchronousInputIDYeni çıkış yok zaten ayarlanır. 3 Çıktı sütunları oluşturun:

    • ChildID (birincil anahtar) türü dört bayt işaretli tamsayı DT_I4

    • Ayrıca, türü dört bayt işaretli tamsayı DT_I4 ParentID (yabancı anahtar)

    • Uzunluğu 50 ile [dt_str] dize türünde ChildRecord

  13. Tarih Script sayfası Komut dosyası dönüşümü Düzenleyicisi, tıklayın Komut dosyası Düzenle. De ScriptMainsınıf, örnekte gösterilen kodu girin. Komut dosyası geliştirme ortamı kapatın ve Komut dosyası dönüşümü Düzenleyicisi.

  14. SQL Server hedef veri akışı ekleyin. Bu hedefe ParentRecords çıktı Script bileşeni bağlanın.ole db Bağlantı Yöneticisi'ni ve veliler tablo kullanmak üzere yapılandırın.

  15. Başka bir SQL Server hedef veri akışı ekleyin. Bu hedefe ChildRecords çıktı Script bileşeni bağlanın. ole db Bağlantı Yöneticisi'ni ve çocuk tablo kullanmak üzere yapılandırın.

  16. Paketi çalıştırın. Paket bittikten sonra iki üst ve alt kayıtları incelemek SQL Serverhedef tabloları.

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Static nextRowIsParent As Boolean = False
        Static parentCounter As Integer = 0
        Static childCounter As Integer = 0

        ' If current row starts with separator characters,
        '  then following row contains new parent record.
        If Row.Column0.StartsWith("***") Then
            nextRowIsParent = True
        Else
            If nextRowIsParent Then
                ' Current row contains parent record.
                parentCounter += 1
                Me.ParentRecordsBuffer.AddRow()
                Me.ParentRecordsBuffer.ParentID = parentCounter
                Me.ParentRecordsBuffer.ParentRecord = Row.Column0
                nextRowIsParent = False
            Else
                ' Current row contains child record.
                childCounter += 1
                Me.ChildRecordsBuffer.AddRow()
                Me.ChildRecordsBuffer.ChildID = childCounter
                Me.ChildRecordsBuffer.ParentID = parentCounter
                Me.ChildRecordsBuffer.ChildRecord = Row.Column0
            End If
        End If

    End Sub
    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Static nextRowIsParent As Boolean = False
        Static parentCounter As Integer = 0
        Static childCounter As Integer = 0

        ' If current row starts with separator characters,
        '  then following row contains new parent record.
        If Row.Column0.StartsWith("***") Then
            nextRowIsParent = True
        Else
            If nextRowIsParent Then
                ' Current row contains parent record.
                parentCounter += 1
                Me.ParentRecordsBuffer.AddRow()
                Me.ParentRecordsBuffer.ParentID = parentCounter
                Me.ParentRecordsBuffer.ParentRecord = Row.Column0
                nextRowIsParent = False
            Else
                ' Current row contains child record.
                childCounter += 1
                Me.ChildRecordsBuffer.AddRow()
                Me.ChildRecordsBuffer.ChildID = childCounter
                Me.ChildRecordsBuffer.ParentID = parentCounter
                Me.ChildRecordsBuffer.ChildRecord = Row.Column0
            End If
        End If

    End Sub
public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

    int static_Input0_ProcessInputRow_childCounter = 0;
    int static_Input0_ProcessInputRow_parentCounter = 0;
    bool static_Input0_ProcessInputRow_nextRowIsParent = false;

        // If current row starts with separator characters, 
        // then following row contains new parent record. 
        if (Row.Column0.StartsWith("***"))
        {
            static_Input0_ProcessInputRow_nextRowIsParent = true;
        }
        else
        {
            if (static_Input0_ProcessInputRow_nextRowIsParent)
            {
                // Current row contains parent record. 
                static_Input0_ProcessInputRow_parentCounter += 1;
                this.ParentRecordsBuffer.AddRow();
                this.ParentRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter;
                this.ParentRecordsBuffer.ParentRecord = Row.Column0;
                static_Input0_ProcessInputRow_nextRowIsParent = false;
            }
            else
            {
                // Current row contains child record. 
                static_Input0_ProcessInputRow_childCounter += 1;
                this.ChildRecordsBuffer.AddRow();
                this.ChildRecordsBuffer.ChildID = static_Input0_ProcessInputRow_childCounter;
                this.ChildRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter;
                this.ChildRecordsBuffer.ChildRecord = Row.Column0;
            }
        }

    }
public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

    int static_Input0_ProcessInputRow_childCounter = 0;
    int static_Input0_ProcessInputRow_parentCounter = 0;
    bool static_Input0_ProcessInputRow_nextRowIsParent = false;

        // If current row starts with separator characters, 
        // then following row contains new parent record. 
        if (Row.Column0.StartsWith("***"))
        {
            static_Input0_ProcessInputRow_nextRowIsParent = true;
        }
        else
        {
            if (static_Input0_ProcessInputRow_nextRowIsParent)
            {
                // Current row contains parent record. 
                static_Input0_ProcessInputRow_parentCounter += 1;
                this.ParentRecordsBuffer.AddRow();
                this.ParentRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter;
                this.ParentRecordsBuffer.ParentRecord = Row.Column0;
                static_Input0_ProcessInputRow_nextRowIsParent = false;
            }
            else
            {
                // Current row contains child record. 
                static_Input0_ProcessInputRow_childCounter += 1;
                this.ChildRecordsBuffer.AddRow();
                this.ChildRecordsBuffer.ChildID = static_Input0_ProcessInputRow_childCounter;
                this.ChildRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter;
                this.ChildRecordsBuffer.ChildRecord = Row.Column0;
            }
        }

    }
Integration Services simgesi (küçük) Integration Services ile güncel kalın

En son karşıdan yüklemeler, makaleler, örnekler ve Microsoft video yanı sıra topluluk seçili çözümleri için ziyaret Integration ServicesMSDN sayfası:


Bu güncelleştirmelerle ilgili otomatik bildirim almak için, sayfadaki RSS akışlarına abone olun.

Ayrıca bkz.

Kavramlar

Zaman uyumlu dönüşümü Script bileşeni ile oluşturma

Uyumsuz bir dönüşümü Script bileşeni ile oluşturma