clickhouse嵌套Map:用total指定

mu0hgdu0  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(2)|浏览(882)

带嵌套Map的clickhouse表:

  1. CREATE TABLE IF NOT EXISTS Test.NestedTest2
  2. (
  3. -- slices
  4. idx1 UInt8, -- just some index 1
  5. idx2 UInt8, -- just some index 2
  6. -- metrics
  7. totalCnt UInt64, -- total http response count
  8. codeMap Nested(
  9. key UInt16, -- http response code
  10. value UInt64 -- http response count
  11. )
  12. ) Engine MergeTree()
  13. PARTITION BY (idx1)
  14. ORDER BY (idx1, idx2)
  15. INSERT INTO Test.NestedTest2 (idx1, idx2, totalCnt, codeMap.key, codeMap.value)
  16. VALUES
  17. (1, 1, 5, array(200), array(5))
  18. (1, 1, 5, array(204), array(5))
  19. (1, 1, 15, array(404, 502), array(5, 10))
  20. (1, 2, 45, array(404, 502), array(20, 25))
  21. (2, 1, 20, array(404, 502, 200), array(5, 5, 10))

哪里 totalCnt 只是作为例子。同样地,它也可能与总价值相关,例如 memoryUsage .
我需要在同一个select中的总响应中获得特定的代码响应计数:

  1. -- where idx1 = 1
  2. /*
  3. ┌─code─┬─count─┬─totalCnt─┐
  4. │ 200 │ 5 │ 70 │
  5. │ 204 │ 5 │ 70 │
  6. │ 404 │ 25 │ 70 │
  7. │ 502 │ 35 │ 70 │
  8. └──────┴───────┴──────────┘
  9. * /
dxpyg8gm

dxpyg8gm1#

  1. SELECT
  2. idx1,
  3. (arrayJoin(arrayMap((x, y) -> (x, y), (sumMap(codeMap.key, codeMap.value) AS a).1, a.2)) AS arr).1 AS code,
  4. arr.2 AS cnt,
  5. sum(totalCnt) AS total
  6. FROM Test.NestedTest2
  7. WHERE idx1 = 1
  8. GROUP BY idx1
  9. ┌─idx1─┬─code─┬─cnt─┬─total─┐
  10. 1 200 5 70
  11. 1 204 5 70
  12. 1 404 25 70
  13. 1 502 35 70
  14. └──────┴──────┴─────┴───────┘

顺便说一句,你可以计算 totalcodeMap.value -- arrayReduce('sum', groupArrayArray ```
SELECT
idx1,
(arrayJoin(arrayMap((x, y) -> (x, y), (sumMap(codeMap.key, codeMap.value) AS a).1, a.2)) AS arr).1 AS code,
arr.2 AS cnt,
arrayReduce('sum', groupArrayArray(codeMap.value)) AS total
FROM Test.NestedTest2
WHERE idx1 = 1
GROUP BY idx1

┌─idx1─┬─code─┬─cnt─┬─total─┐
│ 1 │ 200 │ 5 │ 70 │
│ 1 │ 204 │ 5 │ 70 │
│ 1 │ 404 │ 25 │ 70 │
│ 1 │ 502 │ 35 │ 70 │
└──────┴──────┴─────┴───────┘

展开查看全部
wnrlj8wa

wnrlj8wa2#

这种方式并不完美,因为 ARRAY JOIN (grafana临时过滤器不适用于此类查询)。

  1. SELECT idx1, code, val, total
  2. FROM (
  3. SELECT
  4. idx1,
  5. sumMap(codeMap.key, codeMap.value) as arr,
  6. sum(totalCnt) as total
  7. FROM Test.NestedTest2
  8. WHERE idx1 = 1
  9. GROUP BY idx1
  10. )
  11. ARRAY JOIN arr.1 as code, arr.2 as val
  12. ORDER BY idx1, code
  13. /*
  14. ┌─idx1─┬─code─┬─val─┬─total─┐
  15. │ 1 │ 200 │ 5 │ 70 │
  16. │ 1 │ 204 │ 5 │ 70 │
  17. │ 1 │ 404 │ 25 │ 70 │
  18. │ 1 │ 502 │ 35 │ 70 │
  19. └──────┴──────┴─────┴───────┘
  20. * /
展开查看全部

相关问题