从记录到顶级提升已知属性会导致流分析查询

时间:2017-10-15 16:03:42

标签: azure azure-stream-analytics stream-analytics

我有一系列由Stream Analytics作业处理的动态数据。我可以显式查询一些统一属性,但是大部分有效负载在查询时是未知类型。我的目标是获取此未知数据(记录)并将所有属性提升到生成的查询中写入Azure表的顶级字段。

我能够压缩记录的属性,它总是作为子对象添加到查询中。 GetRecordProperties()没有帮助,因为我不想为每个房产返回单独的记录。

我的查询如下:

WITH 
[custom_events_temp] AS
(
    SELECT 
        [magellan].[context].[data].[eventTime] as [event_time],
        [flat_event].ArrayValue.name as [event_name],
        udf.FlattenCustomDimensions([magellan].[context].[custom].[dimensions]) as [flat_custom_dim]
    FROM [Magellan--AI-CustomEvents] magellan
    TIMESTAMP BY [magellan].[context].[data].[eventTime]
    CROSS APPLY GetElements([magellan].[event]) as [flat_event]
),
-- create table with extracted webhook data
[all_webhooks] AS
(
    SELECT
        [flat_custom_dim].[hook_event_source] as PartitionKey,
        udf.CreateGuid('') as RowKey,
        -- event data
        [custom_events_temp].[event_time],
        [custom_events_temp].[flat_custom_dim].[hook_event_name] as [event_name],
        -- webhook payload data        
        udf.FlattenWebhookPayload(udf.ExtractJsonWebhookPayload([custom_events_temp].[flat_custom_dim].[webhook_payload])) AS [payload]
    FROM [custom_events_temp]
)
SELECT * INTO [TrashTableOut] FROM [all_webhooks]

我得到的记录看起来像这样。我们的想法是不要嵌套payload嵌套对象中的所有内容,以便每个属性在Azure表中都有自己的列。

{
  "partitionkey": "zzzzzzzzz",
  "rowkey": "8beeb783-b07f-8a98-ef56-71c43378a5fc",
  "event_time": "2017-10-15T05:37:06.3240000Z",
  "event_name": "subscriber.updated_lead_score",
  "payload": {
    "event": "subscriber.updated_custom_field",
    "data.subscriber.id": "...",
    "occurred_at": "2017-10-15T05:36:57.000Z",
    "data.account_id": "11111",
    "data.subscriber.status": "active",
    "data.subscriber.custom_fields.coupon": "xxxxxxx",
    "data.subscriber.custom_fields.coupon_discounted_price": "11111",
    "data.subscriber.custom_fields.coupon_pre_discount_price": "11111",
    "data.subscriber.custom_fields.name": "John Doe",
    "data.subscriber.custom_fields.first_name": "John",
    "data.subscriber.custom_fields.ip_address": "0.0.0.0",
    "data.subscriber.tags": "tag1,tag2,tag3",
    "data.subscriber.time_zone": "Europe/Berlin",
    "data.subscriber.utc_offset": 120,
    "data.subscriber.created_at": "2017-03-27T18:19:35.000Z"
  }
}

这可能吗? UDF FlattenCustomDimensions获取一系列项目并将它们公开为属性。 UDF ExtractJsonWebhookPayload采用字符串&将它转换为JSON,而UDF FlattenWebhookPayload获取复杂的JSON对象&创建您在结果中的payload对象中看到的点语法。

我的最终目标是获得一个看起来像的结果集:

{
  "partitionkey": "zzzzzzzzz",
  "rowkey": "8beeb783-b07f-8a98-ef56-71c43378a5fc",
  "event_time": "2017-10-15T05:37:06.3240000Z",
  "event_name": "subscriber.updated_lead_score",
  "payload.event": "subscriber.updated_custom_field",
  "payload.data.subscriber.id": "...",
  "payload.occurred_at": "2017-10-15T05:36:57.000Z",
  "payload.data.account_id": "11111",
  "payload.data.subscriber.status": "active",
  "payload.data.subscriber.custom_fields.coupon": "xxxxxxx",
  "payload.data.subscriber.custom_fields.coupon_discounted_price": "11111",
  "payload.data.subscriber.custom_fields.coupon_pre_discount_price": "11111",
  "payload.data.subscriber.custom_fields.name": "John Doe",
  "payload.data.subscriber.custom_fields.first_name": "John",
  "payload.data.subscriber.custom_fields.ip_address": "0.0.0.0",
  "payload.data.subscriber.tags": "tag1,tag2,tag3",
  "payload.data.subscriber.time_zone": "Europe/Berlin",
  "payload.data.subscriber.utc_offset": 120,
  "payload.data.subscriber.created_at": "2017-03-27T18:19:35.000Z"
}

除非有人有更好的主意/选择。

1 个答案:

答案 0 :(得分:0)

如果您将点语法添加到您在最后一行中选择*的查询中,那么您可以将您在temp中展开的列查询到主表中的特定列