0

这个问题是关于流分析的。我想将 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 表中,所以我应该为每个不同的对象设置一个不同的列,所以我想将它保存到一个字符串中,然后在我的应用程序中解析它。

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

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

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

'"available_bytes":"value":994164736.0},"categoryName":"Memory","instanceName":""'

或这个

"{"percentage_processor_total":{"value":0.0123466420918703},"categoryName":"Processor","instanceName":"_Total"}"

或者

"{"percentage_processor_time":"value":0.0},"categoryName":"Process","instanceName":"w3wp"}"

我怎样才能像字符串一样转换数组?我试过 CAST(GetArrayElement(A.performanceCounter,0) as nvarchar(max)) 但我不能。

请一些好的帮助将得到回报

4

2 回答 2

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'

也可以进行为每个计数器类型提供一列的查询,您必须为每个计数器执行一个 join 或 group by with 'case' 语句。

于 2016-02-29T17:38:20.013 回答
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'

无论如何,如果有人发现如何在分析中用纯文本编写对象,仍然会得到奖励和赞赏

于 2016-02-26T17:07:49.880 回答