SSIS:通过 SSIS 执行 Ironpython 或 Ironruby 脚本

Ede*_*der 5 c# python ironpython ssis

问题!

我有一个小的 python 脚本,它遍历一个网页(http-crawling)。此网页托管在 Intranet 内,并使用 NTLM 身份验证来收集对其的访问权限。

所以,我发现这个任务(检索 http 内容)可以使用 python 轻松编程,而不是尝试将整个 python 脚本重新编写为 C#,然后通过 SSIS 上的“脚本任务”使用它,以完成任务。

暗示!

我仔细查看了 SSIS 工具,发现有一个名为“执行进程任务”的控制流,它允许您执行 Win32 可执行文件。

但问题在于如何调用我的 python 脚本,因为它不是可执行的,需要由 python 解释器解释(如果你能原谅重复的话)。因此,我可以轻松地构建一个简单的“.bat”文件,该文件同时调用 python 脚本和解释器。然后通过SSIS“执行进程任务”执行该文件。

题!

有没有其他方法可以实现这一点?(整洁的方式)

编辑 #1

用法

从脚本中检索到的信息将把该信息从数据库存储到一个表中,以便从另一个 SSIS 进程通过数据库表访问该信息。

我正在从不同来源(平面文件、数据库表、http 请求等)检索信息,以便将该信息存档到可以发布在 Web 服务中然后从 Excel 项目访问的数据库中。

提前致谢!

bil*_*nkc 4

至少在我看来,在 SSIS 范围内使用 IronPython 的最简单机制是调用外部进程并转储到文件,然后将其用作数据流的源。

也就是说,我能够通过 C# 托管 IronPython 应用程序,并使用返回的数据填充输出缓冲区并与管道中的数据进行交互。我只有一台机器来执行此操作,因此我列出了我记得在包裹变绿之前所做的所有事情。

先决条件

这篇文章为我指明了如何开展这项工作的道路。在 C# 4.0 程序中托管 IronPython 我强烈建议您创建一个 C#/VB.NET 控制台应用程序,并首先让您的 IronPython 集成在那里工作,因为 SSIS 将为所有内容添加一个附加层。

也许能够在 C# 中托管旧版本的 IronPython,而不需要 4.0 框架,但这远远超出了我的能力范围。我能说的是,要使用 4.0 框架,您需要查看 SQL Server 2012。2008 包最多可以针对 3.5 框架(默认为 2.0)。

全局程序集缓存,简称GAC。它是 Windows 中一个特殊的地方,签名的程序集可以驻留在其中。SSIS 或许能够使用 GAC 中没有的程序集,但我没有这样做的运气。这个案例也不例外。我的控制台应用程序工作正常,但当我将该代码复制到 SSIS 时,它会出现Could not load file or assembly 'Microsoft.Scripting...错误消息。幸运的是,IronPython-2.7.2.1(可能还有以前的版本)是强签名的 dll。这意味着您可以而且必须将它们添加到 GAC 中。

在 Visual Studio 目录中,查找 Visual Studio 命令提示符 (2010)。假设您的 IronPython 安装文件夹是C:\tmp\IronPython-2.7.2.1\IronPython-2.7.2.1您将键入的cd C:\tmp\IronPython-2.7.2.1\IronPython-2.7.2.1然后我注册了以下 3 个程序集

C:\tmp\IronPython-2.7.2.1\IronPython-2.7.2.1>gacutil -if Microsoft.Dynamic.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

C:\tmp\IronPython-2.7.2.1\IronPython-2.7.2.1>gacutil -if IronPython.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

C:\tmp\IronPython-2.7.2.1\IronPython-2.7.2.1>gacutil -if Microsoft.Scripting.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache
Run Code Online (Sandbox Code Playgroud)

我的 SSIS 项目,我已将 Run64bitRuntime 设置为 False,但在重新测试中,这并不重要。默认为 True,看起来效果很好。

Python 脚本 - 我没有足够的背景来使 C# 和 .NET DLR 语言之间的集成更加优雅。最好提供一个字符串或包含我想要执行的脚本的内容,也许这就是脚本块的用途,但我没有时间进行调查。因此,此解决方案需要一个位于磁盘上某处的脚本文件。我在从托管脚本进行导入时遇到了问题(没有名为 X 的模块例外)。毫无疑问,类路径和所有需要提供给主机以使其正常工作的东西有一些魔力。顺便说一句,这可能是一个不同的问题。

设置

我有一个文件位于 C:\ssisdata\simplePy.py

# could not get a simple import to work from hosted
# works fine from "not hosted"
#import os

def GetIPData():
    #os.listdir(r'C:\\')
    return range(0,100)
Run Code Online (Sandbox Code Playgroud)

将脚本任务添加到数据流后,我将其配置为在输出缓冲区 (wstr 1000) 上有一个列。然后我用它作为我的源代码。

using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

/// <summary>
/// Attempt to use IP script as a source
/// http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    /// <summary>
    /// Create data rows and fill those buckets
    /// </summary>
    public override void CreateNewOutputRows()
    {
        foreach (var item in this.GetData())
        {
            Output0Buffer.AddRow();
            Output0Buffer.Content = item;
        }

    }

    /// <summary>
    /// I've written plenty of code, but I'm quite certain this is some of the ugliest.
    /// There certainly must be more graceful means of 
    /// * feeding your source code to the ironpython run-time than a file
    /// * processing the output of the code the method call
    /// * sucking less at life
    /// </summary>
    /// <returns>A list of strings</returns>
    public List<string> GetData()
    {
        List<string> output = null;
        var ipy = Python.CreateRuntime();
        dynamic test = ipy.UseFile(@"C:\ssisdata\simplePy.py");
        output = new List<string>();
        var pythonData = test.GetIPData();
        foreach (var item in pythonData)
        {
            output.Add(item.ToString());
        }

        return output;
    }
}
Run Code Online (Sandbox Code Playgroud)

快速查看我的参考资料

参考

点击运行按钮,巨大成功

数据流