在Stream Azure Analytics

时间:2016-02-26 14:45:37

标签: json casting azure-sql-database azure-stream-analytics cortana-intelligence

此问题与Stream Analytics有关。我想将blob导出到SQL中。我知道这个过程,我的问题是我必须使用的查询。

{"performanceCounter":[{"available_bytes":{"value":994164736.0},"categoryName":"Memory","instanceName":""}],"internal":{"data":{"id":"459bf840-d259-11e5-a640-1df0b6342362","documentVersion":"1.61"}},"context":{"device":{"type":"PC","network":"Ethernet","screenResolution":{},"locale":"en-US","id":"RD0003FF73B748","roleName":"Sdm.MyGovId.Static.Web","roleInstance":"Sdm.MyGovId.Static.Web_IN_1","oemName":"Microsoft Corporation","deviceName":"Virtual Machine","deviceModel":"Virtual Machine"},"application":{"version":"R2.0_20160205.5"},"location":{"continent":"North America","country":"United States","clientip":"104.41.209.0","province":"Washington","city":"Redmond"},"data":{"isSynthetic":false,"samplingRate":100.0,"eventTime":"2016-02-13T13:53:44.2667669Z"},"user":{"isAuthenticated":false,"anonAcquisitionDate":"0001-01-01T00:00:00Z","authAcquisitionDate":"0001-01-01T00:00:00Z","accountAcquisitionDate":"0001-01-01T00:00:00Z"},"operation":{},"cloud":{},"serverDevice":{},"custom":{"dimensions":[],"metrics":[]},"session":{}}}
{"performanceCounter":[{"percentage_processor_total":{"value":0.0123466420918703},"categoryName":"Processor","instanceName":"_Total"}],"internal":{"data":{"id":"459bf841-d259-11e5-a640-1df0b6342362","documentVersion":"1.61"}},"context":{"device":{"type":"PC","network":"Ethernet","screenResolution":{},"locale":"en-US","id":"RD0003FF73B748","roleName":"Sdm.MyGovId.Static.Web","roleInstance":"Sdm.MyGovId.Static.Web_IN_1","oemName":"Microsoft Corporation","deviceName":"Virtual Machine","deviceModel":"Virtual Machine"},"application":{"version":"R2.0_20160205.5"},"location":{"continent":"North America","country":"United States","clientip":"104.41.209.0","province":"Washington","city":"Redmond"},"data":{"isSynthetic":false,"samplingRate":100.0,"eventTime":"2016-02-13T13:53:44.2668221Z"},"user":{"isAuthenticated":false,"anonAcquisitionDate":"0001-01-01T00:00:00Z","authAcquisitionDate":"0001-01-01T00:00:00Z","accountAcquisitionDate":"0001-01-01T00:00:00Z"},"operation":{},"cloud":{},"serverDevice":{},"custom":{"dimensions":[],"metrics":[]},"session":{}}}
{"performanceCounter":[{"percentage_processor_time":{"value":0.0},"categoryName":"Process","instanceName":"w3wp"}],"internal":{"data":{"id":"459bf842-d259-11e5-a640-1df0b6342362","documentVersion":"1.61"}},"context":{"device":{"type":"PC","network":"Ethernet","screenResolution":{},"locale":"en-US","id":"RD0003FF73B748","roleName":"Sdm.MyGovId.Static.Web","roleInstance":"Sdm.MyGovId.Static.Web_IN_1","oemName":"Microsoft Corporation","deviceName":"Virtual Machine","deviceModel":"Virtual Machine"},"application":{"version":"R2.0_20160205.5"},"location":{"continent":"North America","country":"United States","clientip":"104.41.209.0","province":"Washington","city":"Redmond"},"data":{"isSynthetic":false,"samplingRate":100.0,"eventTime":"2016-02-13T13:53:44.2668342Z"},"user":{"isAuthenticated":false,"anonAcquisitionDate":"0001-01-01T00:00:00Z","authAcquisitionDate":"0001-01-01T00:00:00Z","accountAcquisitionDate":"0001-01-01T00:00:00Z"},"operation":{},"cloud":{},"serverDevice":{},"custom":{"dimensions":[],"metrics":[]},"session":{}}}

那么你可以看到3个json对象,它们对于数组performanceCounter中的对象有不同的字段。基本上是每个对象的第一个对象。第一个是available_bytes,第二个是percentage_processor_total,第三个是percentage_processor_time。

因为我将它导出到一个名为performaceCounter的sql表中,我应该为每个不同的对象都有一个不同的列,所以我想将它保存到一个字符串中然后我将在我的应用程序中解析它。 / p>

作为起点,我有这个查询读取输入(blob)并写入输出(SQL)

  Select GetArrayElement(A.performanceCounter,0) as a
INTO
  PerformanceCounterOutput
FROM PerformanceCounterInput A

此GetArrayElement在performanceCounter中获取数组的索引0,但随后为每个对象中找到的每个不同字段写入不同的列。所以我应该拥有所有不同的计数器并为每个计数器创建一个列,但我的想法更像是一个列调用performanceCounterData并保存字符串,如

'" available_bytes":"值":994164736.0}"类别名称":"存储""实例名":""'

或者

" {" percentage_processor_total" {"值":0.0123466420918703}"类别名称":"处理器"&# 34;实例名":" _Total"}"

" {" percentage_processor_time":"值":0.0}"类别名称":"处理"&#34 ;实例名":" W3WP"}"

如何像String一样转换数组? 我尝试过CAST(GetArrayElement(A.performanceCounter,0)作为nvarchar(max)),但我不能。

请给予一些好的帮助

2 个答案:

答案 0 :(得分:1)

使用以下解决方案,我获得了2列属性的名称,另一列带有属性的值,这是我最初的目的

With pc as
    (
        Select 
        GetArrayElement(A.[performanceCounter],0) as counter
        ,A.context.data.eventTime as eventTime
          ,A.context.location.clientip as clientIp
          ,A.context.location.continent as continent
          ,A.context.location.country as country
          ,A.context.location.province as province
          ,A.context.location.city as city
        FROM PerformanceCounterInput A
    )
        select 
    props.propertyName,
    props.propertyValue,
    pc.counter.categoryName,
    pc.counter.instanceName,
    pc.eventTime,
    pc.clientIp,
    pc.continent,
    pc.country,
    pc.province,
    pc.city
    from pc
    cross apply  GetRecordProperties(pc.counter) as props
    where props.propertyname<>'categoryname' and props.propertyname<>'instancename'

无论如何,如果有人发现如何在分析中以纯文本形式写一个对象,那么仍然会得到奖励和赞赏

答案 1 :(得分:1)

您可以执行类似下面的操作,这会将计数器作为(propertyName,propertyValue)对。

with T1 as
(
select 
  GetArrayElement(iotInput.performanceCounter, 0) Counter,
  System.Timestamp [EventTime]
from 
    iotInput timestamp by context.data.eventTime
)

 select
  [EventTime],
  Counter.categoryName,
  Counter.available_bytes [Value]    
 from 
  T1
 where
  Counter.categoryName = 'Memory'

  union all

 select
   [EventTime],
  Counter.categoryName,
  Counter.percentage_processor_time [Value]    
 from 
  T1
 where
  Counter.categoryName = 'Process'

也可以为每个计数器类型提供一列的查询,您必须为每个计数器使用'case'语句进行连接或分组。