通过数组中的元素加入流分析

时间:2019-11-08 07:51:24

标签: azure azure-stream-analytics

我正在尝试使用参考数据加入流分析。

以下是流数据的输入。

[{
               "id":"111111101",
               "basetime":0,
               "xyz":
               [
                              {
                                             "xxx":1,
                                             "yyy":2631,
                                             "aaa":"470A01",
                                             "id":1
                              },
                              {
                                             "xxx":0,
                                             "yyy":0,
                                             "aaa":"000000",
                                             "id":61
                              }
               ]

},
{
               "id":"111111102",
               "basetime":0,
               "xyz":
               [
                              {
                                             "xxx":1,
                                             "yyy":2631,
                                             "aaa":"03F4EB",
                                             "id":1
                              }
               ]
},
{
               "id":"111111103",
               "basetime":0,
               "xyz":
               [
                              {
                                             "xxx":1,
                                             "yyy":2631,
                                             "aaa":"6706",
                                             "id":1
                              }
              ]
}
]

下面是参考主数据。

[
{
    "aaa": "470A01"
  },
  {
    "aaa": "03F4EB"
  },
  {
    "aaa": "710211"
  }
]

编写的SAQL如下所示。

WITH INPUT1 AS (
    SELECT
    input.id.dateTime AS ID,
    flatArrayElement as ABC,
    FROM [signals2] as input
    CROSS APPLY GetArrayElements(input.xyz) AS flatArrayElement
    )

我已经使用CROSS APPLY将xyz中的每个元素都分配为不同的行。

INPUT1的输出如下所示。

+----------+------------------------------------------------------------------------+
|       ID |   ABC                                                                  |
+----------+------------------------------------------------------------------------+
| 111111101| {"ArrayValue":{"xxx":1,"yyy":2631,"aaa":470A01,"id":1},"ArrayIndex":0} |
| 111111101| {"ArrayValue":{"xxx":0,"yyy":0,"aaa":000000,"id":61},"ArrayIndex":1}   |
| 111111102| {"ArrayValue":{"xxx":1,"yyy":2631,"aaa":03F4EB,"id":1},"ArrayIndex":0} |
| 111111103| {"ArrayValue":{"xxx":1,"yyy":2631,"aaa":6706,"id":1},"ArrayIndex":0}   |
+-------------------+---------------------------------------------------------------+

现在,我正尝试将数据xyz.aaa与以下参考数据合并,其中master是参考数据。

SIGNALS AS (
 SELECT * FROM INPUT1 I JOIN master M ON I.ABC.ArrayValue.aaa = M.aaa

我的输出低于输出,但问题是 xyz ,输出中有多个元素重复。

+-------------------------------+------------------------------------------------------------------------------------------+--------+
| i___timestamp                 |   i                                                                                      |  m     |     
+-------------------------------+------------------------------------------------------------------------------------------+--------+
| "2019-11-13T03:36:22.4636494Z"| "id": "111111101",{"ArrayValue":{"xxx":1,"yyy":2631,"aaa":470A01,"id":1},"ArrayIndex":0} | 470A01 |
| "2019-11-13T03:36:22.4636494Z"| "id": "111111101",{"ArrayValue":{"xxx":1,"yyy":2631,"aaa":470A01,"id":1},"ArrayIndex":0} | 470A01 |
| "2019-11-13T03:36:22.4636494Z"| "id": "111111102",{"ArrayValue":{"xxx":1,"yyy":2631,"aaa":03F4EB,"id":1},"ArrayIndex":0} | 03F4EB |
+-------------------------------+------------------------------------------------------------------------------------------+--------+

我很困惑为什么前两行重复,它应该只是一个条目。 xyz 中的两个元素中,一个有效,一个无效。但是,这里的有效元素重复了两次。 可能是什么原因?如何解决呢?

1 个答案:

答案 0 :(得分:1)

请参阅我的查询sql:

<div className="input-group mb-3">
    <InputGroup className="mb-3">
        <input
            type="checkbox"
            value="checkAll"
            defaultChecked={this.state.selectAll}
            onChange={this.toggleAllChange.bind(this)}/>
        Check/Uncheck All
    </InputGroup>
</div>
<Table responsive>
    <thead className="text-primary">
    <tr>
        <th>Data name</th>
        <th>Data status</th>
        <th>Check</th>
    </tr>
    </thead>
    <tbody>
    {this.state.datas.map(data => (
        <tr>
            <td>{data.name}</td>
            <td>{data.status}</td>
            <td>
                <input
                    className="chbx"
                    type="checkbox"
                    id={"chbx" + data.name}
                    name={"chbx" + data.name}
                    defaultChecked={this.state.selectAll}
                    onChange={this.toggleOneChange.bind(this)}
                />
            </td>
        </tr>
    ))}
    </tbody>
</Table>

jayrefer是您的主要参考文献:

enter image description here

输入是您的输入:

enter image description here

输出:

enter image description here


仅出于摘要目的,问题是由WITH INPUT1 AS ( SELECT input.name as name, flatArrayElement as ABC FROM [YourInputAlias] as input CROSS APPLY GetArrayElements(input.xyz) AS flatArrayElement ) SELECT INPUT1.ABC.ArrayValue.aaa FROM INPUT1 JOIN jayrefer on INPUT1.ABC.ArrayValue.aaa = jayrefer.item 中的重复原始数据引起的,因此最终结果是重复的。

最终的sql是:

reference data