在流分析查询中生成倒置时间戳
Posted
技术标签:
【中文标题】在流分析查询中生成倒置时间戳【英文标题】:Generate inverted timestamp in stream analytics query 【发布时间】:2018-07-24 01:46:40 【问题描述】:我想将流分析作业的输出通过管道传输到表存储。对于行键,我想使用倒置时间戳。在 C# 中,我只需执行以下操作:
var invertedTimestamp = DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks;
我正在尝试编写 ASA 工作查询,但不知道如何获得类似的内容:
SELECT
CONCAT(deviceId, '_', sessionId) as DeviceSession,
DATEDIFF ( ms , NOW(), MAX_DATE() ) //pseudocode
INTO
tablestorage
FROM
iothub
【问题讨论】:
【参考方案1】:您可以使用User Defined Functions
带功能:
/ There are 10000 ticks in a millisecond.
// And there are 621355968000000000 ticks between 1st Jan 0001 and 1st Jan 1970.
function tics_to_max()
var convertToTicks = function (date)
return ((date.getTime() * 10000) + 621355968000000000);
;
var max = new Date('9999-12-31T23:59:59.999Z');
var maxTics = convertToTicks(max);
var currentTics = convertToTicks(new Date());
return maxTics - currentTics;
您的查询可能如下所示:
SELECT
CONCAT(deviceId, '_', sessionId) as DeviceSession,
udf.tics_to_max() AS TicsToMax
INTO
tablestorage
FROM
iothub
【讨论】:
【参考方案2】:试试下面的代码,它会工作的。
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss",
Locale.ENGLISH);
try
String datestart="June 14 2018 16:02:37";
cal.setTime(sdf.parse(datestart));// all done
Calendar cal1=Calendar.getInstance();
String formatted = sdf.format(cal1.getTime());//formatted date as i want
cal1.setTime(sdf.parse(formatted));// all done
long msDiff = cal1.getTimeInMillis() - cal.getTimeInMillis();
long daysDiff = TimeUnit.MILLISECONDS.toDays(msDiff);
Toast.makeText(this, "days="+daysDiff, Toast.LENGTH_SHORT).show();
catch (ParseException e)
e.printStackTrace();
【讨论】:
以上是关于在流分析查询中生成倒置时间戳的主要内容,如果未能解决你的问题,请参考以下文章
在流分析中将时间戳拆分为单独的列,以便在 Power BI 中进行进一步筛选