我正在尝试设置一个SSIS作业,该作业将从MailChimp中提取JSON编码的邮件列表,将其与CRM数据库(SQL Server)中的客户列表进行比较,并通过JSON上传任何尚未提供的新客户那里。除了编写脚本任务之外,我似乎无法在SSIS中查找序列化/反序列化JSON,并且似乎无法将.Net序列化库导入到脚本中。有什么建议?提前谢谢!
答案 0 :(得分:17)
在这里要解决的问题:
首先,您在脚本组件中添加新库的问题。我假设您正在使用VS 2008进行SSIS开发,并希望使用.net 3.5库来执行此操作。你去项目,添加引用,你没有看到任何你需要的DLL。这可能部分是因为您使用的是Windows 7和紧凑的3.5框架。 .net 3.5.1自带Windows 7,您只需启用它即可。转到控制面板,程序和功能。在该屏幕中,您将看到打开或关闭Windows功能,单击它。在该窗口中检查Microsoft .NET Framework 3.5.1,这种方式需要几分钟才能运行。完成后,查找类似于这些C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v3.5 \ Profile \ Client和C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \的目录框架\ v3.5版本。在这两个目录之间,您将找到JSON序列化/反序列化所需的任何DLL。这些可以通过转到Project - > Add Reference - > Browse选项卡添加到项目中,然后导航到v3.5目录并选择所需的dll(System.Web.Extensions.dll(v3.5.30729)在这个例子中使用了.5446)。
要从Web服务获取JSON,对其进行反序列化并将数据发送到CRM数据库,您必须使用脚本组件作为数据流中的源,并将列添加到将用于的输出缓冲区中保存来自JSON提要的数据(在“输入和输出”屏幕上)。在代码中,您需要覆盖CreateNewOutputRows方法。以下是如何执行此操作的示例:
说你的JSON看起来像这样...... [{"CN":"ALL","IN":"Test1","CO":0,"CA":0,"AB":0},{"CN":"ALL","IN":"Test2","CO":1,"CA":1,"AB":0}]
我会定义一个类来镜像这个JSON提要属性(以及你在输入和输出屏幕上定义的列),一旦你反序列化,它们最终将保存这些值......如下:
class WorkGroupMetric
{
public string CN { get; set; }
public string IN { get; set; }
public int CO { get; set; }
public int CA { get; set; }
public int AB { get; set; }
}
现在您需要使用HttpWebRequest和Stream来调用您的Web服务并获取JSON提要:
string wUrl = "YOUR WEB SERVICE URI";
string jsonString;
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
Stream responseStream = httpWResp.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd();
reader.Close();
}
现在我们将json反序列化为WorkGroupMetric数组
JavaScriptSerializer sr = new JavaScriptSerializer();
WorkGroupMetric[] jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString);
反序列化后,我们现在可以将行输出到输出缓冲区:
foreach (var metric in jsonResponse)
{
Output0Buffer.AddRow();
Output0Buffer.CN = metric.CN;
Output0Buffer.IN = metric.IN;
Output0Buffer.CO = metric.CO;
Output0Buffer.CA = metric.CA;
Output0Buffer.AB = metric.AB;
}
这是所有代码放在一起的样子(我有一步一步的例子here):
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Web.Script.Serialization;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
string wUrl = "YOUR WEB SERVICE URI";
try
{
WorkGroupMetric[] outPutMetrics = getWebServiceResult(wUrl);
foreach (var metric in outPutMetrics)
{
Output0Buffer.AddRow();
Output0Buffer.CN = metric.CN;
Output0Buffer.IN = metric.IN;
Output0Buffer.CO = metric.CO;
Output0Buffer.CA = metric.CA;
Output0Buffer.AB = metric.AB;
}
}
catch (Exception e)
{
failComponent(e.ToString());
}
}
private WorkGroupMetric[] getWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
WorkGroupMetric[] jsonResponse = null;
try
{
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
string jsonString;
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd();
reader.Close();
}
JavaScriptSerializer sr = new JavaScriptSerializer();
jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString);
}
else
{
failComponent(httpWResp.StatusCode.ToString());
}
}
catch (Exception e)
{
failComponent(e.ToString());
}
return jsonResponse;
}
private void failComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
class WorkGroupMetric
{
public string CN { get; set; }
public string IN { get; set; }
public int CO { get; set; }
public int CA { get; set; }
public int AB { get; set; }
}
现在可以将其用作数据目标(您的CRM数据库)的输入。在那里,您可以使用SQL来比较数据并查找不匹配,将数据发送到另一个脚本组件以进行序列化,并将您需要的任何更新发送回Web服务。
OR
您可以在脚本组件中执行所有操作,而不是将数据输出到输出缓冲区。在这种情况下,您仍然需要反序列化JSON,但将数据放入某种集合中。然后使用实体框架和LINQ来查询您的数据库和集合。确定不匹配的内容,将其序列化,然后将其发送到同一脚本组件中的Web服务。