有没有办法在U-SQL WHERE条件下使用DateTime.TryParse?我正在Azure U-SQL目录中创建一个存储过程。
简单的U-SQL脚本支持C#函数。
对于存储过程,脚本不会向我的自定义C#代码添加任何程序集引用,或者如果在调用存储过程时添加了没有使用的程序集引用。
发布于 2018-02-08 23:53:52
是的,使用内联函数可以做到这一点。This article展示了如何做到这一点。下面是一个简单的例子:
@input = SELECT *
FROM (
VALUES
( (int)1, (string)"1/1/2017" ),
( (int)2, (string)"1/2/2017" ),
( (int)3, (string)"bad date" )
) AS x ( rn, someString );
// inline function example
@output =
SELECT *
FROM
(
SELECT *,
(
(Func<string, DateTime?>)
(dateString => // input_paramater
{
DateTime dateValue;
return DateTime.TryParse(dateString, out dateValue) ? (DateTime?)dateValue : (DateTime?)null;
}
)
) (someString) AS someDate
FROM @input
) AS x
WHERE someDate IS NOT NULL;
OUTPUT @output TO "/output/output.csv"
USING Outputters.Csv(quoting:false);如果要使用作为U-SQL类项目创建并注册的程序集,则只需使用REFERENCE ASSEMBLY在存储过程中引用它
DROP PROCEDURE IF EXISTS dbo.usp_testCleanDate;
CREATE PROCEDURE dbo.usp_testCleanDate()
AS
BEGIN
REFERENCE ASSEMBLY USQLCSharpProject1;
@input = SELECT *
FROM (
VALUES
( (int)1, (string)"1/1/2017" ),
( (int)2, (string)"1/2/2017" ),
( (int)3, (string)"bad date" )
) AS x ( rn, someString );
@output =
SELECT *,
USQLCSharpProject1.Class1.tryParseDate(someString) AS x
FROM @input;
OUTPUT @output
TO "/output/output.csv"
USING Outputters.Csv(quoting : false);
END;发布于 2018-02-08 23:57:31
Using Function dt_TryParse_USQL
Using above code-behind and calling function. Function consumes a string and attempts to convert the string to a DateTime value using DateTime.TryParse. Using the Code-Behind above.
@employees =
SELECT * FROM
( VALUES
(1, "Noah", "2/16/2008"),
(2, "Sophia", "2/16/2008 12:15:12 PM"),
(3, "Liam", "16/02/2008 12:15:12"),
(4, "Amy", "2017-01-11T16:52:07"),
(5, "Justin", "")
) AS T(EmpID, EmpName, StartDate);
@result =
SELECT
EmpID,
EmpName,
ReferenceGuide_Examples.MyClass.dt_TryParse_USQL(StartDate) AS validated_StartDate
FROM @employees;
OUTPUT @result
TO "/Output/ReferenceGuide/DDL/Functions/dt_TryParse_USQL.csv"
USING Outputters.Csv(outputHeader: true);
https://msdn.microsoft.com/en-us/azure/data-lake-analytics/u-sql/u-sql-functions
更多信息:Click here
发布于 2018-02-09 15:31:12
要回答问题的这一部分:
对于存储过程,脚本不会向我的自定义C#代码添加任何程序集引用,或者如果在调用存储过程时添加了没有使用的程序集引用。
U-SQL函数和过程具有自己的静态上下文,并且不从调用上下文继承静态名称上下文。这允许理解函数/过程的语义,独立于它被调用的位置。
因此,为了引用程序集代码,您需要在目录中注册程序集,并使用REFERENCE ASSEMBLY在过程中显式引用它。代码隐藏并不意味着在过程中使用(因为该过程是为将来使用而存储的)。
https://stackoverflow.com/questions/48687049
复制相似问题