Nested JSON Objects in Keen.io

怎甘沉沦 提交于 2019-12-01 01:32:56

Josh from Keen IO here.

There isn't currently a way to query properties of objects contained in lists via the workbench or API. There are, however, a few ways to work around this.

Indexed Properties

You can copy properties in the object list out to a set of indexed properties before you send the event. This results in properties like pour_1_amount, pour_2_amount up to pour_n_amount.

If you do this, you should add a number_of_pours property that represents the size of the pours list. When you're ready to analyze, first query to get the maximum number_of_pours over the range of events. This will be an input into a loop you will run to analyze each indexed property.

Let's say you want to find the total amount of all pours. Here's how'd do that using Ruby and keen-gem, though the concept applies generally.

# within an irb session
require 'keen'   

# get the maximum number of pours to check
maximum_pours_length = 
  Keen.maximum('collection', target_property: 'number_of_pours')

# total pour amount
total_pour_amount = 0

for n in maximum_pours_length
  total_pour_amount = total_pour_amount + 
    Keen.sum('collection', target_property: "pour_#{n}_amount")
end

# print the total
puts total_pour_amount

total_pour_amount will contain the sum of all pour amounts for each list item of each event.

Pre-aggregate Properties

You can aggregate (a.k.a. reduce) all instances of the property down to one: e.g. total_pour_amount, maximum_pour_end_time, etc. This requires you to know what analysis you want to do in advance, but you can then use Keen for all the querying.

Extraction w/ client side processing

You can perform an extraction, and do manual processing on your side. To make the extraction faster you can limit it to certain properties. This option keeps the data model the cleanest, but it does require more work at query time because Keen won't be doing the aggregation. Projects like Miso Dataset can make some of that work easier.

With the ruby gem it seems like you are now able to run something like this to query the nested attribute:

Keen.select_unique(
    'sessions',
    target_property: 'user.id',
    :timeframe => 'this_8_weeks'
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!