在流分析查询中生成倒置时间戳

Generate inverted timestamp in stream analytics query

我想将流分析作业的输出通过管道传输到 table 存储。对于行键,我想使用倒置时间戳。在 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

您可以使用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

试试下面的代码,它会起作用。

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();
    }