This module provides Apache Druid aggregators based on Theta sketch from Apache DataSketches library. Note that sketch algorithms are approximate; see details in the “Accuracy” section of the datasketches doc. At ingestion time, this aggregator creates the Theta sketch objects which get stored in Druid segments. Logically speaking, a Theta sketch object can be thought of as a Set data structure. At query time, sketches are read and aggregated (set unioned) together. In the end, by default, you receive the estimate of the number of unique entries in the sketch object. Also, you can use post aggregators to do union, intersection or difference on sketch columns in the same row. Note that you can use thetaSketch aggregator on columns which were not ingested using the same. It will return estimated cardinality of the column. It is recommended to use it at ingestion time as well to make querying faster.

To use this aggregator, make sure you include the extension in your config file:

  1. druid.extensions.loadList=["druid-datasketches"]

Aggregators

  1. {
  2. "type" : "thetaSketch",
  3. "name" : <output_name>,
  4. "fieldName" : <metric_name>,
  5. "isInputThetaSketch": false,
  6. "size": 16384
  7. }
propertydescriptionrequired?
typeThis String should always be “thetaSketch”yes
nameA String for the output (result) name of the calculation.yes
fieldNameA String for the name of the aggregator used at ingestion time.yes
isInputThetaSketchThis should only be used at indexing time if your input data contains theta sketch objects. This would be the case if you use datasketches library outside of Druid, say with Pig/Hive, to produce the data that you are ingesting into Druidno, defaults to false
sizeMust be a power of 2. Internally, size refers to the maximum number of entries sketch object will retain. Higher size means higher accuracy but more space to store sketches. Note that after you index with a particular size, druid will persist sketch in segments and you will use size greater or equal to that at query time. See the DataSketches site for details. In general, We recommend just sticking to default size.no, defaults to 16384

Post Aggregators

Sketch Estimator

  1. {
  2. "type" : "thetaSketchEstimate",
  3. "name": <output name>,
  4. "field" : <post aggregator of type fieldAccess that refers to a thetaSketch aggregator or that of type thetaSketchSetOp>
  5. }

Sketch Operations

  1. {
  2. "type" : "thetaSketchSetOp",
  3. "name": <output name>,
  4. "func": <UNION|INTERSECT|NOT>,
  5. "fields" : <array of fieldAccess type post aggregators to access the thetaSketch aggregators or thetaSketchSetOp type post aggregators to allow arbitrary combination of set operations>,
  6. "size": <16384 by default, must be max of size from sketches in fields input>
  7. }

Sketch Summary

This returns a summary of the sketch that can be used for debugging. This is the result of calling toString() method.

  1. {
  2. "type" : "thetaSketchToString",
  3. "name": <output name>,
  4. "field" : <post aggregator that refers to a Theta sketch (fieldAccess or another post aggregator)>
  5. }

Examples

Assuming, you have a dataset containing (timestamp, product, user_id). You want to answer questions like

How many unique users visited product A? How many unique users visited both product A and product B?

to answer above questions, you would index your data using following aggregator.

  1. { "type": "thetaSketch", "name": "user_id_sketch", "fieldName": "user_id" }

then, sample query for, How many unique users visited product A?

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test_datasource",
  4. "granularity": "ALL",
  5. "dimensions": [],
  6. "aggregations": [
  7. { "type": "thetaSketch", "name": "unique_users", "fieldName": "user_id_sketch" }
  8. ],
  9. "filter": { "type": "selector", "dimension": "product", "value": "A" },
  10. "intervals": [ "2014-10-19T00:00:00.000Z/2014-10-22T00:00:00.000Z" ]
  11. }

sample query for, How many unique users visited both product A and B?

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test_datasource",
  4. "granularity": "ALL",
  5. "dimensions": [],
  6. "filter": {
  7. "type": "or",
  8. "fields": [
  9. {"type": "selector", "dimension": "product", "value": "A"},
  10. {"type": "selector", "dimension": "product", "value": "B"}
  11. ]
  12. },
  13. "aggregations": [
  14. {
  15. "type" : "filtered",
  16. "filter" : {
  17. "type" : "selector",
  18. "dimension" : "product",
  19. "value" : "A"
  20. },
  21. "aggregator" : {
  22. "type": "thetaSketch", "name": "A_unique_users", "fieldName": "user_id_sketch"
  23. }
  24. },
  25. {
  26. "type" : "filtered",
  27. "filter" : {
  28. "type" : "selector",
  29. "dimension" : "product",
  30. "value" : "B"
  31. },
  32. "aggregator" : {
  33. "type": "thetaSketch", "name": "B_unique_users", "fieldName": "user_id_sketch"
  34. }
  35. }
  36. ],
  37. "postAggregations": [
  38. {
  39. "type": "thetaSketchEstimate",
  40. "name": "final_unique_users",
  41. "field":
  42. {
  43. "type": "thetaSketchSetOp",
  44. "name": "final_unique_users_sketch",
  45. "func": "INTERSECT",
  46. "fields": [
  47. {
  48. "type": "fieldAccess",
  49. "fieldName": "A_unique_users"
  50. },
  51. {
  52. "type": "fieldAccess",
  53. "fieldName": "B_unique_users"
  54. }
  55. ]
  56. }
  57. }
  58. ],
  59. "intervals": [
  60. "2014-10-19T00:00:00.000Z/2014-10-22T00:00:00.000Z"
  61. ]
  62. }

Retention Analysis Example

Suppose you want to answer a question like, “How many unique users performed a specific action in a particular time period and also performed another specific action in a different time period?”

e.g., “How many unique users signed up in week 1, and purchased something in week 2?”

Using the (timestamp, product, user_id) example dataset, data would be indexed with the following aggregator, like in the example above:

  1. { "type": "thetaSketch", "name": "user_id_sketch", "fieldName": "user_id" }

The following query expresses:

“Out of the unique users who visited Product A between 10/01/2014 and 10/07/2014, how many visited Product A again in the week of 10/08/2014 to 10/14/2014?”

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test_datasource",
  4. "granularity": "ALL",
  5. "dimensions": [],
  6. "filter": {
  7. "type": "or",
  8. "fields": [
  9. {"type": "selector", "dimension": "product", "value": "A"}
  10. ]
  11. },
  12. "aggregations": [
  13. {
  14. "type" : "filtered",
  15. "filter" : {
  16. "type" : "and",
  17. "fields" : [
  18. {
  19. "type" : "selector",
  20. "dimension" : "product",
  21. "value" : "A"
  22. },
  23. {
  24. "type" : "interval",
  25. "dimension" : "__time",
  26. "intervals" : ["2014-10-01T00:00:00.000Z/2014-10-07T00:00:00.000Z"]
  27. }
  28. ]
  29. },
  30. "aggregator" : {
  31. "type": "thetaSketch", "name": "A_unique_users_week_1", "fieldName": "user_id_sketch"
  32. }
  33. },
  34. {
  35. "type" : "filtered",
  36. "filter" : {
  37. "type" : "and",
  38. "fields" : [
  39. {
  40. "type" : "selector",
  41. "dimension" : "product",
  42. "value" : "A"
  43. },
  44. {
  45. "type" : "interval",
  46. "dimension" : "__time",
  47. "intervals" : ["2014-10-08T00:00:00.000Z/2014-10-14T00:00:00.000Z"]
  48. }
  49. ]
  50. },
  51. "aggregator" : {
  52. "type": "thetaSketch", "name": "A_unique_users_week_2", "fieldName": "user_id_sketch"
  53. }
  54. },
  55. ],
  56. "postAggregations": [
  57. {
  58. "type": "thetaSketchEstimate",
  59. "name": "final_unique_users",
  60. "field":
  61. {
  62. "type": "thetaSketchSetOp",
  63. "name": "final_unique_users_sketch",
  64. "func": "INTERSECT",
  65. "fields": [
  66. {
  67. "type": "fieldAccess",
  68. "fieldName": "A_unique_users_week_1"
  69. },
  70. {
  71. "type": "fieldAccess",
  72. "fieldName": "A_unique_users_week_2"
  73. }
  74. ]
  75. }
  76. }
  77. ],
  78. "intervals": ["2014-10-01T00:00:00.000Z/2014-10-14T00:00:00.000Z"]
  79. }