我有几个 SSIS (.dtsx) 文件,我需要将它们的功能重写为 C#。
它们被配置为简单地从一个 db1.table1 中获取数据,并通过一些自定义映射将数据传输到 db2.table2(列名不完全匹配)。
是否有一些关于 SSIS xml 格式的文档?在手动解析 XML 格式以检索源目标表名和列名时,有什么我应该注意的吗?
最佳答案
以下代码是为 SSIS 包设计的 PackageFormatVersion=3
它不是很漂亮,但它可以用于一种 XML 转换方式。
解析源码
private static Mapping<ColumnMapping> ParseSourceComponent(XElement source)
{
var table = source.XPathSelectElement("properties/property[@name='OpenRowset']").Value;
var nonErrorOutput = source.XPathSelectElement("outputs").Elements().First(x => !((string)x.Attribute("name")).Contains("Error"));
var outputColumns = nonErrorOutput.XPathSelectElement("outputColumns").Elements().Select(x =>
new ColumnMapping
{
Id = (int)x.Attribute("id"),
Name = (string)x.Attribute("name")
}).ToList();
return new Mapping<ColumnMapping>
{
TableName = NormalizeTableNames(table),
Columns = outputColumns
};
}
static readonly Regex tableNameRegex = new Regex("\\[dbo\\]\\.\\[(.*)\\]");
private static string NormalizeTableNames(string rawTableName)
{
var matches = tableNameRegex.Match(rawTableName);
if (matches.Success)
return matches.Groups[1].Value;
return rawTableName;
}
解析目标
private static Mapping<InputColumnMapping> ParseDestinationComponent(string ssisName,XElement source)
{
var table = source.XPathSelectElement("properties/property[@name='OpenRowset']").Value;
var nonErrorOutput = source.XPathSelectElement("inputs").Elements().First(x => !((string)x.Attribute("name")).Contains("Error"));
var inputColumns = nonErrorOutput.XPathSelectElement("inputColumns").Elements().Select(x =>
new
{
lineageId = (int)x.Attribute("lineageId"),
externalMetadataColumnId = (int)x.Attribute("externalMetadataColumnId")
}).ToList();
var externalMetadataColumns = nonErrorOutput.XPathSelectElement("externalMetadataColumns").Elements().Select(x =>
new InputColumnMapping
{
Id = (int)x.Attribute("id"),
Name = (string)x.Attribute("name")
}).ToList();
foreach (var externalMetadataColumn in externalMetadataColumns.ToList())
{
var inputMapping =
inputColumns.FirstOrDefault(x => x.externalMetadataColumnId == externalMetadataColumn.Id);
if (inputMapping == null)
{
Console.WriteLine("{0} | destination external column {1} with id {2} was not found in input mappings", ssisName, externalMetadataColumn.Name, externalMetadataColumn.Id);
externalMetadataColumns.Remove(externalMetadataColumn);
continue;
}
externalMetadataColumn.MappsToId = inputMapping.lineageId;
}
return new Mapping<InputColumnMapping>
{
TableName = NormalizeTableNames(table),
Columns = externalMetadataColumns
};
}
处理整个 .dtsx 文件
private static RemoteMappingFile ParseDtsx(string ssisName)
{
var xml = XDocument.Load(@"ssis/"+ssisName);
if (xml.Root == null)
{
throw new Exception("Root is null");
}
var mappings = new List<RemoteMapping>();
XNamespace ns = "www.microsoft.com/SqlServer/Dts";
XmlNamespaceManager man = new XmlNamespaceManager(new NameTable());
man.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts");
var executables = xml.Root.Descendants(ns + "Executable").Select(x => x).ToList();
foreach (var executable in executables)
{
var components = executable.Descendants(ns + "ObjectData").First().XPathSelectElement("pipeline/components").Elements().ToList();
if (components.Count != 2)
{
Console.WriteLine("{0} | WARN - 2 components expected. Found {1} with names: {2}", ssisName, components.Count, string.Join(",",components.Select(x=>((string)x.Attribute("name"))).ToList()));
}
var source = components.First(x => ((string)x.Attribute("name")).Contains("Source"));
var destination = components.First(x => ((string)x.Attribute("name")).Contains("Destination"));
var sourceMapping = ParseSourceComponent(source);
var destinationMapping = ParseDestinationComponent(ssisName,destination);
var remoteMapping = new RemoteMapping
{
TableNames = new Column { Source = sourceMapping.TableName, Destination = destinationMapping.TableName },
Columns = new List<Column>()
};
foreach (var sourceItem in sourceMapping.Columns)
{
var foundMatchingDestinationColumn =
destinationMapping.Columns.FirstOrDefault(x => x.MappsToId == sourceItem.Id);
if (foundMatchingDestinationColumn == null)
{
Console.WriteLine("{0} | input mapping {1} with id {2} was not found in destination mappings",
ssisName, sourceItem.Name, sourceItem.Id);
continue;
}
remoteMapping.Columns.Add(new Column
{
Destination = foundMatchingDestinationColumn.Name,
Source = sourceItem.Name
});
}
mappings.Add(remoteMapping);
}
return new RemoteMappingFile
{
RemoteMappings = mappings,
SSISName = ssisName
};
}
需要的数据结构
public class ColumnMapping
{
public int Id { get; set; }
public string Name { get; set; }
}
public class InputColumnMapping : ColumnMapping
{
public int MappsToId { get; set; }
}
public class Mapping<T> where T : ColumnMapping
{
[XmlAttribute]
public string TableName { get; set; }
public List<T> Columns { get; set; }
}
public class RemoteMapping
{
public Column TableNames { get; set; }
public List<Column> Columns { get; set; }
}
public class Column
{
[XmlAttribute]
public string Source { get; set; }
[XmlAttribute]
public string Destination { get; set; }
}
public class RemoteMappingFile
{
[XmlAttribute]
public string SSISName { get; set; }
public List<RemoteMapping> RemoteMappings { get; set; }
}
public class MappingsXml
{
public List<RemoteMappingFile> Mappings { get; set; }
}
main 方法获取 ssis 文件夹中的所有 .dtsx 文件
internal class Program
{
private static void Main()
{
//var mappings = Directory.EnumerateFiles("ssis","*.dtsx").Select(x=>ParseDtsx(Path.GetFileName(x).ToString())).ToList();
var list = new MappingsXml
{
Mappings =
Directory.EnumerateFiles("ssis", "*.dtsx")
.Select(x => ParseDtsx((Path.GetFileName(x) ?? "").ToString()))
.ToList()
};
var xsSubmit = new XmlSerializer(typeof (MappingsXml));
using (var file = new StreamWriter(
@"AutoRemoteMappingXmls.xml"))
{
xsSubmit.Serialize(file, list);
}
}
}
最终输出:
<?xml version="1.0" encoding="utf-8"?>
<MappingsXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Mappings>
<RemoteMappingFile SSISName="ssis1.dtsx">
<RemoteMappings>
<RemoteMapping>
<TableNames Source="sourceTable1" Destination="destinationTable1" />
<Columns>
<Column Source="sourceColumn1" Destination="destinationColumn1" />
<Column Source="sourceColumn2" Destination="destinationColumn2" />
</Columns>
</RemoteMapping>
<RemoteMapping>
<TableNames Source="sourceTable2" Destination="destinationTable2" />
<Columns>
<Column Source="sourceColumn3" Destination="destinationColumn3" />
<Column Source="sourceColumn4" Destination="destinationColumn4" />
</Columns>
</RemoteMapping>
</RemoteMappings>
</RemoteMappingFile>
</Mappings>
</MappingsXml>
如果出现以下情况,它还会写入控制台:
关于c# - 解析 SSIS .xml 源以检索表映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513566/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token