首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过Microsoft Azure查询从json文件中的数组中获取数据

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

Stack Overflow用户
提问于 2018-06-04 20:40:43
回答 3查看 422关注 0票数 1

如何在输入中返回数组元素的值,该元素所在的索引不断变化?

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

My Query

如果您想要轻松地复制、粘贴和/或编辑它,可以在这里使用文本形式:

代码语言:javascript
复制
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,将返回包含我想要查看的元素的完整数组。

更复杂的是,我意识到我想要的元素在数组中的位置并不总是相同的。在一些示例数据中,它是7,而在其他示例数据中,它是3。

提前谢谢。

阅读答案后更新

我的新问题:

代码语言:javascript
复制
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

问题:我现在对单个事件有多个行,每行显示一个我想要跟踪的属性(每行中与数组相关的其余列为NULL)。你有什么想法来解决这个问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-06 22:43:18

我的解决方案:

代码语言:javascript
复制
    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属性移到主数组中,因为我试图使用WITH语句从两个独立的数组中获取信息,但它不允许我将结果放在单个输出中。此外,由于UserId的索引大部分是12,但有时是11。因此,为了显示所有记录的实际UserId,我使用了"Case When“语法。

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-15 04:23:23

下面的查询符合您最新的数组结构,请尝试一下:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-05-20 22:16:17

您可以使用UDF

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50680891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档