1

我在变量中有 SELECT 查询的结果,现在我想逐行遍历查询结果以进行一些处理,例如查找特定模式。例如,模式可能如下:

a, b, c, d, e
b, c, d, e, f
c, d, e, f, g

CSV 中 SELECT 查询的结果可能是:

1, 2, 3, 4, 5
3, 4, 5, 6, 7
a, b, c, d, e
b, c, d, e, f
c, d, e, f, g
5, 6, 7, 8, 9

我已经看到有关使用自定义提取器的 PROCESS 语句的一些内容,但这是这样做的方法吗?我不确定这个过程和提取器是如何工作的。

https://msdn.microsoft.com/en-us/library/azure/mt621322.aspx

谢谢你的帮助。

4

1 回答 1

1

我认为你不需要迭代。更加基于集合的方法对您有用吗?试试我创建的这个示例 U-SQL 脚本。基本上,如果匹配,则结果将在文件中,如果不匹配,则文件将为空。

// Set the search pattern
@pattern = 
    SELECT *
    FROM ( VALUES 
            ( "a", "b", "c", "d", "e" ),
            ( "b", "c", "d", "e", "f" ),
            ( "c", "d", "e", "f", "g" )
           ) AS t (col1, col2, col3, col4, col5 );


// Get the file to search
@input =
    EXTRACT col1 string,
            col2 string,
            col3 string,
            col4 string,
            col5 string
    FROM "/input/input.csv"
    USING Extractors.Csv();


// Add rowIds
@pattern =
    SELECT ROW_NUMBER() OVER() AS rowId, *
    FROM @pattern;

@input =
    SELECT ROW_NUMBER() OVER() AS rowId, *
    FROM @input;


// Check the same rows appear in the same order
@temp =
    SELECT i.rowId,
           p.rowId == null ? 0 : ROW_NUMBER() OVER( ORDER BY p.rowId ) AS rowId2    // Restarts the row numbering when there is a match
    FROM @input AS i
         LEFT OUTER JOIN
             @pattern AS p
         ON i.col1 == p.col1
            AND i.col2 == p.col2
            AND i.col3 == p.col3
            AND i.col4 == p.col4
            AND i.col5 == p.col5;


@output =
    SELECT p.*
    FROM @pattern AS p
         INNER JOIN
             @temp AS t
         ON p.rowId == t.rowId2;


@pattenRecords =
    SELECT COUNT( * ) AS records
    FROM @pattern;


@records =
    SELECT COUNT( * ) AS records
    FROM @output;


// Join criteria mean output file will be empty if there has not been a match
@output =
    SELECT o.*
    FROM @output AS o
       CROSS JOIN @records AS r
       INNER JOIN
          (
          SELECT *
          FROM @pattenRecords
          INTERSECT
          SELECT *
          FROM @records
          ) AS t ON r.records == t.records;



// Output results
OUTPUT @output
    TO "/output/output.csv"
USING Outputters.Csv();

也许有更简单的方法。

于 2016-10-09T22:11:12.837 回答