USQL Array of Array to flat CSV

时间:2017-09-26 13:00:32

标签: azure-data-lake u-sql

我有一个返回

的程序集
SQL.ARRAY<SQL.ARRAY<string>>

我需要将其扁平化为csv,而不是获得正确的结果。

以下是U-SQL中的内容,如果我在其中创建数组数组:

DECLARE @boardA1 SQL.ARRAY<string> = new SQL.ARRAY<string>{"111", "PASS", "0"};
DECLARE @boardA2 SQL.ARRAY<string> = new SQL.ARRAY<string>{"222", "PASS", "0"};
DECLARE @boardA3 SQL.ARRAY<string> = new SQL.ARRAY<string>{"333", "PASS", "0"};

@boards =
    SELECT *
    FROM(
        VALUES
        (
            new SQL.ARRAY<SQL.ARRAY<string>>{@boardA1, @boardA2, @boardA3}
        )
             ) AS t(boards);

@result =
    SELECT n.value AS Board
    FROM @boards
         CROSS APPLY
             EXPLODE(boards) AS c(value)
         CROSS APPLY
             EXPLODE(c.value) AS n(value);


OUTPUT @result
TO "result1.csv"
USING Outputters.Csv();

我得到了这个输出:

"111"
"PASS"
"0"
"222"
"PASS"
"0"
"333"
"PASS"
"0"

但我想要这个输出:

"111","PASS","0"
"222","PASS","0"
"333","PASS","0"

我做错了什么?任何意见都赞赏!

2 个答案:

答案 0 :(得分:1)

@result2 = SELECT "\"" + string.Join(",", c.board).Replace(",", "\",\"") + "\"" AS Boards FROM @boards CROSS APPLY EXPLODE(boards) AS c(board);

OUTPUT @result2 TO "result2.csv" USING Outputters.Csv(quoting: false);

查看Explode (U-SQL)

ARRAY<ARRAY<string>>下的示例

答案 1 :(得分:0)

我会使用你想要的数组的序数来做到这一点,例如

@result =
    SELECT 
        x.board[0] AS a,
        x.board[1] AS b,
        x.board[2] AS c        
    FROM @boards
         CROSS APPLY
             EXPLODE(boards) AS x(board);

使用此方法,您可以明确指定列名称,quoting的行为与预期一致。这种方法的缺点是你必须知道数组中有多少“列”。