【发布时间】:2019-05-01 12:26:38
【问题描述】:
好的,我正在编写一个使用 SSIS 脚本组件作为目标的 avro 文件。由于 AVRO 也需要模式,因此我需要定义模式。当我手动定义架构时它工作正常。但是我有 10-12 个数据流任务,我不想明确地编写模式。我正在尝试查看是否可以使用自动生成的 BufferWrapper 来查看我是否可以从那里读取但我不能并且它总是返回空白。
我已经尝试了here 发布的解决方案,还阅读了this。 但一切都返回空白。
我也遇到过this。这可能是原因吗?如果发布的答案中的解释是正确的,这不可能吗?
所以,在我的 public override void PreExecute(),我有这样的东西:
Schema = @"{
""type"":""record"",
""name"":""Microsoft.Hadoop.Avro.Specifications.Counterparts"",
""fields"":
[
{ ""name"":""CounterpartID"", ""type"":""int"" },
{ ""name"":""CounterpartFirstDepositDate"", ""type"":[""string"",""null""] },
{ ""name"":""CounterpartFirstTradeDate"",""type"":[""string"",""null""] },
{ ""name"":""ClientSegmentReportingID"",""type"":[""int"",""null""] },
{ ""name"":""ClientSegmentReportingName"", ""type"":[""string"",""null""] },
{ ""name"":""ContractID"", ""type"":[""int"",""null""] },
{ ""name"":""ContractFirstDepositDate"", ""type"":[""string"",""null""] },
{ ""name"":""ContractFirstTradeDate"",""type"":[""string"",""null""] },
{ ""name"":""ContractClosingOffice"",""type"":[""string"",""null""] },
{ ""name"":""LeadCreationDate"", ""type"":[""string"",""null""] },
{ ""name"":""ContractCountryOfResidence"", ""type"":[""string"",""null""] }
]
}";
}
我没有手动定义所有这些模式,而是检查是否可以从 BufferWrapper 生成它,但这会返回空白:
var fields = typeof(Input0Buffer).GetFields().Select(m => new
{
Name = m.Name,
Type = m.FieldType
}).ToList();
另外,如果我这样做,也会返回空白
Type myType = typeof(Input0Buffer);
// Get the fields of the specified class.
FieldInfo[] myField = myType.GetFields();
之前我将这些新方法放在 Pre-Execute 中,但后来想,可能缓冲区还没有初始化,所以我把它移到 Input0_ProcessInputRow 方法并确保它只被触发一次使用计数器变量并制作此代码仅在 counter=0 时运行,但即使返回空白。
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (counter == 0)
{
Type myType = typeof(Input0Buffer);
// Get the fields of the specified class.
FieldInfo[] myField = myType.GetFields();
}
//Processing logic
}
难道不是因为this吗?
因为它谈到它受到保护并且也无法从该自动生成的类外部访问。
【问题讨论】:
-
如果您只创建一次表,我会在数据库中而不是在 c# 中编写代码。您可以使用 SQL Server Management Studio 并使用资源管理器右键单击数据库/表,然后使用选项创建表定义以查看代码示例。
-
嗨,我没有在数据库中创建表。我正在编写一个需要模式的 AVRO 文件。我有 OLEDB 源,它从 SQL 服务器获取数据,目的是从中写入 AVRO 文件。正如我所说,我正在尝试使这个动态,因为这不是我要创建的唯一 AVRO 文件。还有20人。所以,我不想要具体的定义。
-
模式格式是 JSON,因此任何可以生成 JSON 的 Net 类/库都可以使用。见维基:en.wikipedia.org/wiki/Apache_Avro
-
问题不在于 JSON 或 AVRO。问题是关于如何从 SSIS InputBuffer 中的脚本组件动态获取列列表并使用它为 AVRO 生成架构。兴趣点是动态确定来自 SSIS 的 InputBuffer 的输入列。无论如何,我已经找到了答案并会发布它。
-
您可以从 c# 获取信息,而不是使用带有 GetSchema 方法的脚本:docs.microsoft.com/en-us/dotnet/api/…