SQL:
create table mytable (moment timestamp with timezone, ...)
select create_hypertable('mytable', 'moment', chunk_time_interval => INTERVAL '5 minute');
用 jooq 选择:
import org.jooq.types.YearToSecond
import java.time.Duration
import org.jooq.impl.DSL.count
import org.jooq.impl.DSL.field
// custom function to combine arguments Duration and OffsetDateTime for TimeBucket2
private fun timeBucket(d: Duration, field: Field<OffsetDateTime>): Field<OffsetDateTime> {
val f = TimeBucket2()
f.setBucketWidth(YearToSecond.valueOf(d))
f.setTs(field)
return f.asField()
}
// field name alias
val BUCKET = field("bucket", OffsetDateTime::class.java)
val COUNT = field("count", Int::class.java)
val ctx = DSL.using(conn, SQLDialect.POSTGRES, settings)
val recs = ctx.select(
timeBucket(Duration.ofMinutes(5), MYTABLE.MOMENT).`as`(BUCKET),
count().`as`(COUNT)
).from(MYTABLE)
.groupBy(BUCKET)
.fetch()
for (rec in recs) {
println(rec[BUCKET] + " => " + rec[COUNT])
}