简体   繁体   中英

Rollup a List of data the most efficient way using java

I have a List of data like this.

start: 1455262571155, seconds: 40, name: "quant1"
start: 1455263331610, seconds: 120, name: "quant2"
start: 1455263331610, seconds: 120, name: "quant2"
start: 1455263331610, seconds: 120, name: "quant3"

the list will go on and on. it always has a timestamp when a sensor came online and how long it lasts. You seem a value is not unique and can appear the same time. Also the same time, another value can appear.

Now I want to retransfer these data into such a format.

time: 12:50, value: "quant1", amount: 1
time: 12:51, value: "quant2", amount: 2
time: 12:52, value: "quant2", amount: 1

(Given that the start timestamps would represent 12:50...)

Now I see different problems as the algorithm needs to check if there are already values for the given timestamp. What's the most efficient way to do this in Java?

These data are stored within a PostgreSQL database.

Something along these lines will give you what you want:

SELECT TO_CHAR(TIMESTAMP 'epoch' + (start / 1000) * INTERVAL '1 second', 'HH:MI') AS time,
    name AS value, COUNT(*) AS amount
FROM yourTable
GROUP BY TO_CHAR(TIMESTAMP 'epoch' + (start / 1000) * INTERVAL '1 second', 'HH:MI'),
    name

The complexity here is in converting seconds since epoch to a TIMESTAMP , and then converting that timestamp into HH:MI format.

The code

TIMESTAMP 'epoch' + (start / 1000) * INTERVAL '1 second'

converts your start field, in milliseconds, into a TIMESTAMP type. The division by 1000 is necessary to convert milliseconds into seconds. The TO_CHAR() function converts that timestamp into the HH:MI format you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM