如何使用U-SQL自定义提取器动态输出多个文件?

时间:2017-07-19 12:46:21

标签: c# azure azure-data-lake u-sql

我创建了一个U-SQL查询,它从多个文件中读取数据。现在我必须在其他文件夹的多个文件中输出数据。我有以下例子,

  

U-SQL Output in Azure Data Lake

使用以下脚本

        DECLARE @storagePath string = @"E:\";
        DECLARE @inputFileSetName string = @"dbo.file{*}.tsv"; 
        DECLARE @outputFileName string = @"dbo.files.csv";

        DECLARE @input string = String.Concat(@storagePath, 
        @inputFileSetName);
        DECLARE @output = string.Concat(@storagePath, @outputFileName);
        @searchlog =
        EXTRACT Id string,
        Name string,
        Address string

        FROM @input
        USING new USQLApplication3.SampleExtractor(Encoding.UTF8);
        @transactions =
        SELECT *,
        ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Id DESC) AS 
        RowNumber_12345
        FROM @searchlog;

       @result =
       SELECT EXTRACT Id,
        Name,
        Address 
       FROM @transactions
       WHERE RowNumber_12345 == 1;

       OUTPUT @result 
       TO @output
       USING Outputters.Csv(encoding : Encoding.UTF8, quoting : false);

以下是自定义提取器的C#代码,

public class SampleExtractor : IExtractor
{
    private Encoding _encoding;
    private byte[] _row_delim;
    private char _col_delim;

    public SampleExtractor(Encoding encoding, string row_delim = "\n", char col_delim = '\t')
    {
        this._encoding = ((encoding == null) ? Encoding.UTF8 : encoding);
        this._row_delim = this._encoding.GetBytes(row_delim);
        this._col_delim = col_delim;
    }

    public override IEnumerable<IRow> Extract(IUnstructuredReader input, IUpdatableRow output)
    {
        string line;
        //Read the input line by line
        foreach (Stream current in input.Split(_encoding.GetBytes("\n")))
        {
            using (StreamReader streamReader = new StreamReader(current, this._encoding))
            {
                line = streamReader.ReadToEnd().Trim();
                //Split the input by the column delimiter
                string[] parts = line.Split(this._col_delim);
                foreach (string part in parts)
                {
                    string res = part;
                    if (res != null)
                    {
                        res = "\"" + res.Replace("\"", "\"\"") + "\"";
                    }
                    output.Set<string>(count, res);
                }

            }
            yield return output.AsReadOnly();

        }
       yield break;
    }
}

如何在输出上动态创建多个文件?截至目前,它只创建一个文件。

编辑:

我也尝试在单个u-sql脚本中使用多个输出,

      @x = SELECT * FROM (VALUES( "A", 10, 20), ("A", 11, 21), ("B", 10, 30), ("B", 100, 200)) AS T(name, value1, value2);

     // Generate the script to do partitioned output based on name column:
     DECLARE @out string ="E:/genscript.usql";
     @stmts = 
         SELECT "@res=SELECT value1, value2 FROM @x WHERE name == 
         \""+name+"\"; 
     OUTPUT 
          @res TO \"E:/"+name+".csv\" USING Outputters.Csv();" AS output 
          FROM (SELECT DISTINCT name FROM @x) AS x;

     OUTPUT @stmts TO @out
           USING Outputters.Text(delimiter:' ', quoting:false);

     @path = EXTRACT path string FROM @out USING Extractors.Text() ;
     OUTPUT @path  TO "E:/{*}.usql" 
           USING Outputters.Text(delimiter:' ', quoting:false);

但它显示以下错误,

  

严重级代码描述项目文件行抑制状态   错误E_CSC_USER_READFROMPREVIOUSOUTPUT:EXTRACT语句从位置E:\ genscript.usql读取/引用,该位置是先前OUTPUT语句的目标。   描述:   编译器不支持OUTPUT,然后从单个作业中的同一文件读取/引用。   解析度:   请将一个作业输出到文件,另一个作业读取/引用同一个文件。

我现在应该怎么做才能从单个u-sql输出多个文件或者如何从当前的u-sql脚本访问另一个u-sql脚本文件?

2 个答案:

答案 0 :(得分:1)

您可以简单地使用多个OUTPUT命令,例如

OUTPUT @result1
TO @output1
USING Outputters.Csv(encoding : Encoding.UTF8, quoting : false);

OUTPUT @result2
TO @output2
USING Outputters.Csv(encoding : Encoding.UTF8, quoting : false);
...

U-SQL不直接支持动态U-SQL,但有办法实现。这样做的两种示例方式是herehere

答案 1 :(得分:1)

从2020年开始,此功能在功能预览标志DataPartitionedOutput:on后面发布

SET @@FeaturePreviews = "DataPartitionedOutput:on";


@rows =
EXTRACT 
Firstname string,
Lastname string,
Score string
FROM "/input/a.csv"
USING Extractors.Csv(skipFirstNRows:1);


OUTPUT @rows
TO @"output/{Score}.csv"
USING Outputters.Csv();