Rhino ETL合并表与Web服务结果

时间:2014-03-23 23:26:53

标签: c# rhino-etl

我在SQL Server中有一个从Web服务填充的表。我希望它能按计划刷新。我希望有类似于SQL Merge操作的东西。

这就是我定义我的源(Web服务)和我的目标(SQL表),我定义了如何处理从目标和匹配中丢失的源丢失。

让我们考虑这样一种情况:表格中只有两个字段描述已删除,而网络服务仅提供描述。< / p>

  • 如果表和Web服务中都有描述,那么我只是更新(或不更新)。

  • 如果Web服务中存在描述但不在表中,我希望将其插入

  • 如果Web服务器中不再出现描述,我希望将其标记为Deleted = true

我目前拥有的是:

public class WebServiceResults: AbstractOperation
{
    public WebServiceResults()
    {
        var WebService = new WebService();
        WSResults = WebService.GetResults();
    }
    IEnumerable<WSResult> WSResults  { get; set; }
    public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
    {
        foreach(var obj in WSResults)
            yield return Row.FromObject(obj);
    }

}

class SQLTableResults : AbstractOperation
{
    public SQLTableResults()
    {
        SQLResults = data.MyTable.Select(x=>new {x.Description,x.Deletet});
    }
    Data data = new Data();
    IEnumerable<SQLResult> SQLResults  { get; set; }
    public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
    {
        foreach (var obj in SQLResults)
            yield return Row.FromObject(obj);
        }
    }
    public override void Dispose()
    {
        data.Dispose();
        base.Dispose();
    }
}

class JoinTables : JoinOperation
{

    protected override Row MergeRows(Row leftRow, Row rightRow)
    {
        Row row = leftRow.Clone();
        row["Description2"] = rightRow["Description"];
        return row;
    }

    protected override void SetupJoinConditions()
    {
        FullOuterJoin
            .Left("Description")
            .Right("Description");
    }
}

class MergeTables : AbstractOperation
{
    Data data = new Data();
    public MergeTables()
    { }

    public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
    {
        foreach (var obj in rows)
        {
            if (String.IsNullOrEmpty((string)obj["Description2"]))
            {
                //Code for not matched at target
                yield return Row.FromObject(obj);
            }
            if (String.IsNullOrEmpty((string)obj["Description"]))
            {
                //Code for not matched at source
                yield return Row.FromObject(obj);
            }

            {
                //Code for matched
                yield return Row.FromObject(obj);
            }
        }
    }
    public override void Dispose()
    {
        data.Dispose();
        base.Dispose();
    }
}

protected override void Initialize()
{
    Register(
           new JoinTables()
           .Left(new SQLTableResults())
           .Right(new WebServiceResults())
       );
    Register(new MergeTables());
    foreach (var error in GetAllErrors())
        Console.Write(error.Message);
}

这是要走的路吗?我会想象更多的步骤,比如

Register(new NotMatchedAtSourceOperation());
Register(new NotMatchedAtTargetOperation());
Register(new MatchedOperation());

但据我了解,每个寄存器将其行返回到下一个,所以如果我过滤掉不匹配的,那么其他两个将无效。

我应该为每个案例创建一个新流程吗?

顺便说一句,我正在寻找有关RhinoEtl的文档。你知道有什么链接吗?任何教程?

1 个答案:

答案 0 :(得分:1)

使用完全外部联接确定单个操作中的合并操作。您可以看到示例here。这不完全是你所需要的,所以我会尝试根据你的情况调整它:

protected override Row MergeRows(Row wsRow, Row dbRow) {

    Row row;

    // if the db row doesn't exist, then the ws row is new, and it should be inserted
    if (dbRow["id"] == null) {
        row = wsRow.Clone();
        row["action"] = "Insert";
        row["deleted"] = false;
        return row;
    }

    // if the ws row doesn't exist, it should be marked as deleted in the database (if not already)
    if (wsRow["id"] == null) {
        row = dbRow.Clone();
        row["deleted"] = true;
        row["action"] = dbRow["deleted"].Equals(true) ? "None" : "Update";
        return row;
    }

    // ws and db descriptions match, but check and make sure it's not marked as deleted in database
    row = wsRow.Clone();
    row["deleted"] = false;
    row["action"] = dbRow["deleted"].Equals(true) ? "Update" : "None";
    return row;

}

protected override void SetupJoinConditions() {
    FullOuterJoin.Left("description").Right("description");
}

运行此操作后,每一行都将执行“插入”,“更新”或“无”操作。基于此操作,您可以为要执行的SqlBatchOperation组合插入和更新语句。