如何通过Microsoft Azure查询从json文件中的数组中获取数据

时间:2018-06-04 12:40:43

标签: json indexing azure-stream-analytics

如何在我的一个输入中返回数组元素的值,其中索引所在的输入会不断变化?

我很确定我的查询结构是正确的。我有两个输入,我正在使用连接,并成功从两个表中获取一些数据。但是,我需要从表B获取RemoteIpAddress,但它是在json格式的数组中。

My Query

如果您想轻松复制,粘贴和/或编辑它,请使用文本:

SELECT  
A.context.data.eventTime as eventTime,
A.context.device.type as deviceType,
A.context.[user].anonId as userId,
A.context.device.roleInstance as machineName,
B.context.operation.name as eventName,
B.context.custom.dimensions[0],
--B.GetRecordPropertyValue(GetArrayElement(B.context.custom.dimensions,7), B.RemoteIpAddress) as remoteIpAddress,
--GetArrayElement(B.context.custom.dimensions,3),
--B.GetRecordPropertyValue(GetArrayElement(B.context.custom.dimensions,3), B.userName) as userName,
DATEDIFF(minute,A.context.data.eventTime,B.context.data.eventTime) as durationInMinutes



INTO DevUserlgnsOutput

FROM DevUserlgnsInput A TIMESTAMP BY A.context.data.eventTime

JOIN DevUserlgnsInput2 B TIMESTAMP BY B.context.data.eventTime
ON DATEDIFF(minute,A,B) BETWEEN 0 AND 5

注释掉的行不起作用,所以我已将它们评论出来。

我看了这个,看到了使用GetRecordPropertyValue和GetArrayElement的建议,所以我做到了。我没有错误,但它返回null。

我还发现,如果我执行B.context.custom.dimensions [0],则返回包含我想看的元素的完整数组。

为了使事情更复杂,我意识到我想要的元素在数组中的位置并不总是相同的。在一些样本数据中,它是7,其他是3。

提前致谢。

阅读答案后更新:

我的新查询:

SELECT 
Events.context.data.eventTime as eventTime,
Events.context.device.type as deviceType,
mDim.ArrayValue.MachineName as machineName,
mDim.ArrayValue.UserId as userID,
mDim.ArrayValue.RemoteIpAddress as remoteIpAddress,
mDim.ArrayValue.UserName as userName,
mDim.ArrayValue.EventName as eventName

INTO DevUserlgnsOutput

FROM DevUserlgnsInput2 Events

CROSS APPLY GetArrayElements(Events.context.custom.dimensions) AS mDim

问题:我现在有多个行用于单个事件,每个行显示1个我想要跟踪的属性(每行中与该数组相关的其余列都是NULL)。有关如何解决这个问题的想法吗?

3 个答案:

答案 0 :(得分:0)

我的解决方案:

    WITH Events AS

(

SELECT

  context.data.EventTime as eventTime,

  context.device.type as deviceType,

  GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 7), 'MachineName') AS machineName,

  GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 8), 'UserName') AS userName,

  GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 2), 'remoteIpAddress') AS remoteIpAddress,

  GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 0), 'EventName') AS eventName,

  CASE WHEN GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 12), 'UserId') is NULL THEN GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 11), 'UserId') ELSE GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 12), 'UserId') END as userId


FROM ProdUserlgnsInput

)


SELECT eventTime, deviceType, MachineName, UserId, UserName, remoteIpAddress, eventName  INTO ProdUserlgnsOutput FROM Events

但是,我必须将EventName属性移动到主数组,因为我试图用来从2个独立数组中获取信息的WITH语句不允许我将结果放在单个输出中。另外,由于UserId的索引大多是12,但有时是11.因此,为了显示所有记录的实际UserId,我使用" Case When"语法。

我为解决这个问题付出了很多努力,所以如果有人想了解更多细节,请随时提出。

答案 1 :(得分:0)

以下查询适合您最新的阵列结构,请尝试一下:

if (new HashSet<>(Arrays.asList('a', 'b', 'c')).contains(txt.charAt(i)){
   ...
}

答案 2 :(得分:0)

您可以使用UDF

function arraygetvaluebyname(arg, name) {
    var z = arg;
    for(var i=0;i<z.length;i++){
        if(name === Object.keys(z[i])[0])
        {
            return z[i][name];
        }
    }
    return null;
}