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

How to get data from an array in a json file through a Microsoft Azure Query

如何 return 我的输入之一中数组元素的值,其中它所在的索引不断变化?

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

My Query

如果您想轻松复制、粘贴、and/or 编辑它,它在文本中:

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 的建议,所以我这样做了。我没有收到任何错误,但它 return 为 null。

我还发现,如果我这样做 B.context.custom.dimensions[0],包含我想要查看的元素的完整数组是 returned。

更复杂的是,我意识到我想要的元素在数组中的位置并不总是相同的。在一些示例数据中,它是 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 个属性(每行中与数组相关的其余列是无效的)。关于如何解决这个问题有什么想法吗?

我的解决方案:

    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" 语法。

我做了很多工作来解决这个问题,所以如果有人需要更多细节,请随时询问。

下面的查询符合你最新的数组结构,试试看:

SELECT   
context.data.EventTime as eventTime,
context.device.type as deviceType,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 4), 'MachineName') AS machineName,  
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 5), 'UserId') AS userId,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 9), 'UserName') AS userName,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 11), 'remoteIpAddress') AS remoteIpAddress,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 13), 'EventName') AS eventName     
INTO output1
FROM input1

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