我正在尝试使用 SSIS 将 JSON 文件导入到我的数据库中。
由于我使用的是 SQL Server 2016,因此我可以将文件放在一行中并使用 OPENJSON 读取它们。
我的问题是不想只引入 JSON 文本。我还想需要从文件名和当前目录中获取一些信息。
我的脚本组件看起来像这样:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string type = "NA";
string ChassisSN = "NA";
string CartridgeSN = "NA";
string filepath = Variables.File;
string filename = Path.GetFileNameWithoutExtension(filepath);
string filenamefull = Path.GetFileName(filepath);
string Parent = new DirectoryInfo(Path.GetDirectoryName(filepath)).Name;
Char splitDelim = '_';
String[] FileNameSplit = filename.Split(splitDelim);
String[] ParentSplit = Parent.Split(splitDelim);
CartridgeSN = FileNameSplit[2];
ChassisSN = ParentSplit[2];
type = FileNameSplit[3];
if (ChassisSN != oldChassisSN)
{
oldChassisSN = ChassisSN;
}
Output0Buffer.AddRow();
Output0Buffer.Filename = filenamefull.Trim();
Output0Buffer.ChassisSN = ChassisSN.Trim();
Output0Buffer.CartridgeSN = CartridgeSN.Trim();
Output0Buffer.Type = type.Trim();
Output0Buffer.JSON = Row.Column0.ToString;
}
我的问题是,当我尝试将 JSON 数据放回新列时,它说它是只读的,因为我选择了文本流数据类型。由于文件很大,因此字符数很可能会超过 4000 个。
我还尝试同时使用平面文件源和脚本作为源并合并数据,但它们不断出现在不同的行上。
它循环的每个文件仅产生一行数据,可以在我的代码中看到。那么我怎样才能在一行中获取我需要的全部信息呢?
有点晚了,但对于其他寻找这个问题答案的人来说可能仍然相关。
当您将输出列定义为DT_(N)TEXT
,它从值类型转变为斑点列 https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.dts.pipeline.blobcolumn?view=sqlserver-2017对象,并且不能直接为其赋值。相反,AddBlobData()
应该使用方法来提供一个值:
MainBuffer.AddRow();
MainBuffer.Id = item.Id;
MainBuffer.LargeCol.AddBlobData(Encoding.UTF8.GetBytes(item.JsonData));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)