I'm looking for some help to increase my jq-foo, and could use a little guidance. I have some etl jobs that I script using JQ and I want to see if there is a way to make the runs more efficient in terms of CPU utilization and or speed.
Heres a sample payload:
{
"timestamp": 1636601959,
"uniqueId": "Foo",
"value": 10
},
{
"timestamp": 1636601859,
"uniqueId": "Bar",
"value": 13
}
and I want to do something like (pseudo-code):
if [ (epoch 15 minutes ago) -le timestamp ]; then
name=uniqueId; value_total=value(total); uniqueId_count=(uniqueId(count_total))
fi
Right now I do something like (pseudo-code):
for jq[timestamps] in $(json); do
if [ (epoch 15 minutes ago) -le timestamp ]; then
name=uniqueId; value_total=(value_total value); uniqueId_count=(uniqueId_count 1).
fi
done
Is there a way to simplify this without using a for loop to iterate over each object by calling jq 'select() ...'
over and over?
Thanks in advanced
CodePudding user response:
If you either get rid of the commas between the objects or enclose the whole input with array brackets (in order to get proper JSON), then you can do (use the -s
option for {}{}{}
style, or drop it for [{],{},{}]
style):
jq -s --argjson delta $((15*60)) --argjson addvalue 10 '
(now - $delta) as $pivot
| map(select(.timestamp >= $pivot))
| group_by(.uniqueId)
| map({
name: first.uniqueId,
value_total: map(.value) | (add $addvalue),
uniqueId_count: length
})
'