c# - SSIS:通过SSIS执行Ironpython或Ironruby脚本

标签 c# python ssis ironpython

问题!

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

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

提示!

我仔细查看了 SSIS 工具,发现有一个名为“Execute Process Task”的控制流,可以让您执行 Win32 可执行文件。

但问题在于如何调用我的 python 脚本,因为它不可执行并且需要由 python 解释器解释(如果您愿意重复)。因此,我很容易最终构建一个调用 python 脚本和解释器的简单“.bat”文件。然后通过 SSIS“执行进程任务”执行该文件。

问题!

还有其他方法可以实现吗? (整洁的方式)

编辑 #1

用法

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

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

提前致谢!

最佳答案

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

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

先决条件

这篇文章让我了解了如何完成这项工作。 Hosting IronPython in a C# 4.0 program我强烈建议您创建一个 C#/VB.NET 控制台应用程序,并首先让您的 IronPython 集成在那里工作,因为 SSIS 将为所有内容添加一个额外的层。

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

全局程序集缓存,简称GAC。它是 Windows 中一个特殊的地方,签名的程序集可以在其中生存。 SSIS 可能能够使用不在 GAC 中的程序集,但我没有运气这样做。这个案子也不异常(exception)。我的控制台应用程序运行良好,但是当我将该代码复制到 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

我的 SSIS 项目,我已将 Run64bitRuntime 设置为 False,但在重新测试时,这并不重要。默认为 True,这似乎工作正常。

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

设置

我有一个文件位于 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)

将脚本任务添加到数据流后,我将其配置为在输出缓冲区 (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;
    }
}

快速了解我的引用资料的样子

References

点击运行按钮,大功告成

Data flow

关于c# - SSIS:通过SSIS执行Ironpython或Ironruby脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10304549/

相关文章:

c# - 域驱动设计中 IoC Autowiring 的选项

c# - 枚举应该包含 None 吗?

c# - 具有变量名称、QueueTriggerAttribute 限制的 Azure 函数 QueueTrigger

python - SQLAlchemy Joined Inheritance 子对象的快速批量删除

ssis - 有人可以解释一下数据挖掘,SSIS,BI,ETL和其他相关技术吗?

c# - 匹配 HTML 和 Javascript 文件中的文件路径

python - 字符串和日期时间对象互换

python - Networkx 传播节点和短缺标签

sql-server - 执行 SSIS 的 SQL 作业 - 无法完成游标操作,因为声明游标后表架构发生了更改

sql - SSIS : Using a variable as the name of a flat file destination